Skip to content

Commit

Permalink
feat(backlinks): hide by default when empty (#1674)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Pham <[email protected]>
  • Loading branch information
necauqua and aarnphm authored Dec 27, 2024
1 parent 7533d50 commit 05e6f05
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/features/backlinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
72 changes: 44 additions & 28 deletions quartz/components/Backlinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div class={classNames(displayClass, "backlinks")}>
<h3>{i18n(cfg.locale).components.backlinks.title}</h3>
<ul class="overflow">
{backlinkFiles.length > 0 ? (
backlinkFiles.map((f) => (
<li>
<a href={resolveRelative(fileData.slug!, f.slug!)} class="internal">
{f.frontmatter?.title}
</a>
</li>
))
) : (
<li>{i18n(cfg.locale).components.backlinks.noBacklinksFound}</li>
)}
</ul>
</div>
)
interface BacklinksOptions {
hideWhenEmpty: boolean
}

Backlinks.css = style
export default (() => Backlinks) satisfies QuartzComponentConstructor
const defaultOptions: BacklinksOptions = {
hideWhenEmpty: true,
}

export default ((opts?: Partial<BacklinksOptions>) => {
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 (
<div class={classNames(displayClass, "backlinks")}>
<h3>{i18n(cfg.locale).components.backlinks.title}</h3>
<ul class="overflow">
{backlinkFiles.length > 0 ? (
backlinkFiles.map((f) => (
<li>
<a href={resolveRelative(fileData.slug!, f.slug!)} class="internal">
{f.frontmatter?.title}
</a>
</li>
))
) : (
<li>{i18n(cfg.locale).components.backlinks.noBacklinksFound}</li>
)}
</ul>
</div>
)
}

Backlinks.css = style

return Backlinks
}) satisfies QuartzComponentConstructor

0 comments on commit 05e6f05

Please sign in to comment.