From 528ea07b180ce0717f39eb6f93e0352cf7b8e5d0 Mon Sep 17 00:00:00 2001 From: Anton Bulakh Date: Tue, 24 Dec 2024 02:03:09 +0200 Subject: [PATCH] feat(backlinks): Add an option to hide backlinks component when empty I moved backlinks to `afterBody` and felt it looks better when the component is fully hidden whenever there's no backlinks, so I added that as an option. --- 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 f558f4a5d..6a521410a 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: use `Component.Backlinks({ hideWhenEmpty: true })` to hide the backlinks component completely when there are no backlinks. - 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 aa412a2e0..85bd35b80 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}

- -
- ) +interface BacklinksOptions { + hideWhenEmpty: boolean } -Backlinks.css = style -export default (() => Backlinks) satisfies QuartzComponentConstructor +const defaultOptions: BacklinksOptions = { + hideWhenEmpty: false, +} + +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