From 05e6f05a5067ca1e6f5d5f793353182d8804c860 Mon Sep 17 00:00:00 2001 From: Anton Bulakh Date: Fri, 27 Dec 2024 06:05:35 +0200 Subject: [PATCH] feat(backlinks): hide by default when empty (#1674) Co-authored-by: Aaron Pham --- docs/features/backlinks.md | 1 + quartz/components/Backlinks.tsx | 72 ++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/docs/features/backlinks.md b/docs/features/backlinks.md index f558f4a..6862720 100644 --- a/docs/features/backlinks.md +++ b/docs/features/backlinks.md @@ -9,6 +9,7 @@ A backlink for a note is a link from another note to that note. Links in the bac ## Customization - Removing backlinks: delete all usages of `Component.Backlinks()` from `quartz.layout.ts`. +- Hide when empty: hide `Backlinks` if given page doesn't contain any backlinks (default to `true`). To disable this, use `Component.Backlinks({ hideWhenEmpty: false })`. - Component: `quartz/components/Backlinks.tsx` - Style: `quartz/components/styles/backlinks.scss` - Script: `quartz/components/scripts/search.inline.ts` diff --git a/quartz/components/Backlinks.tsx b/quartz/components/Backlinks.tsx index aa412a2..e99055e 100644 --- a/quartz/components/Backlinks.tsx +++ b/quartz/components/Backlinks.tsx @@ -4,33 +4,49 @@ import { resolveRelative, simplifySlug } from "../util/path" import { i18n } from "../i18n" import { classNames } from "../util/lang" -const Backlinks: QuartzComponent = ({ - fileData, - allFiles, - displayClass, - cfg, -}: QuartzComponentProps) => { - const slug = simplifySlug(fileData.slug!) - const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug)) - return ( -
-

{i18n(cfg.locale).components.backlinks.title}

-
    - {backlinkFiles.length > 0 ? ( - backlinkFiles.map((f) => ( -
  • - - {f.frontmatter?.title} - -
  • - )) - ) : ( -
  • {i18n(cfg.locale).components.backlinks.noBacklinksFound}
  • - )} -
-
- ) +interface BacklinksOptions { + hideWhenEmpty: boolean } -Backlinks.css = style -export default (() => Backlinks) satisfies QuartzComponentConstructor +const defaultOptions: BacklinksOptions = { + hideWhenEmpty: true, +} + +export default ((opts?: Partial) => { + const options: BacklinksOptions = { ...defaultOptions, ...opts } + + const Backlinks: QuartzComponent = ({ + fileData, + allFiles, + displayClass, + cfg, + }: QuartzComponentProps) => { + const slug = simplifySlug(fileData.slug!) + const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug)) + if (options.hideWhenEmpty && backlinkFiles.length == 0) { + return null + } + return ( +
+

{i18n(cfg.locale).components.backlinks.title}

+
    + {backlinkFiles.length > 0 ? ( + backlinkFiles.map((f) => ( +
  • + + {f.frontmatter?.title} + +
  • + )) + ) : ( +
  • {i18n(cfg.locale).components.backlinks.noBacklinksFound}
  • + )} +
+
+ ) + } + + Backlinks.css = style + + return Backlinks +}) satisfies QuartzComponentConstructor