Anton Bulakh
2024-12-27 05e6f05a5067ca1e6f5d5f793353182d8804c860
feat(backlinks): hide by default when empty (#1674)

Co-authored-by: Aaron Pham <Aaronpham0103@gmail.com>
2 files modified
19 ■■■■■ changed files
docs/features/backlinks.md 1 ●●●● patch | view | raw | blame | history
quartz/components/Backlinks.tsx 18 ●●●●● patch | view | raw | blame | history
docs/features/backlinks.md
@@ -9,6 +9,7 @@
## 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`
quartz/components/Backlinks.tsx
@@ -4,6 +4,17 @@
import { i18n } from "../i18n"
import { classNames } from "../util/lang"
interface BacklinksOptions {
  hideWhenEmpty: boolean
}
const defaultOptions: BacklinksOptions = {
  hideWhenEmpty: true,
}
export default ((opts?: Partial<BacklinksOptions>) => {
  const options: BacklinksOptions = { ...defaultOptions, ...opts }
const Backlinks: QuartzComponent = ({
  fileData,
  allFiles,
@@ -12,6 +23,9 @@
}: QuartzComponentProps) => {
  const slug = simplifySlug(fileData.slug!)
  const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug))
    if (options.hideWhenEmpty && backlinkFiles.length == 0) {
      return null
    }
  return (
    <div class={classNames(displayClass, "backlinks")}>
      <h3>{i18n(cfg.locale).components.backlinks.title}</h3>
@@ -33,4 +47,6 @@
}
Backlinks.css = style
export default (() => Backlinks) satisfies QuartzComponentConstructor
  return Backlinks
}) satisfies QuartzComponentConstructor