Add hideOnRoot option to Component.Backlinks

This commit is contained in:
josh-sanders 2025-03-13 22:00:47 +10:00
parent fac62575ae
commit 1df9e9564f
3 changed files with 9 additions and 0 deletions

View File

@ -137,6 +137,7 @@ The home page is the main web page of your Quartz. The content for the home page
Customization of the home page can be achieved by conditionally hiding or unhiding components; by passing in a boolean value to the `hideOnRoot:` options in `quartz.config.ts`. The components that support this feature are: Customization of the home page can be achieved by conditionally hiding or unhiding components; by passing in a boolean value to the `hideOnRoot:` options in `quartz.config.ts`. The components that support this feature are:
- [[Backlinks]] are shown by default, to hide them the configuration looks like: `Component.Backlinks({ hideOnRoot: true }){:ts}`
- [[Breadcrumbs]] are hidden by default, to unhide them the configuration looks like: `Component.Breadcrumbs({ hideOnRoot: false }){:ts}` - [[Breadcrumbs]] are hidden by default, to unhide them the configuration looks like: `Component.Breadcrumbs({ hideOnRoot: false }){:ts}`
- Date and reading time are shown by default, to hide them the configuration looks like: `Component.ContentMeta({ hideOnRoot: true }){:ts}` - Date and reading time are shown by default, to hide them the configuration looks like: `Component.ContentMeta({ hideOnRoot: true }){:ts}`

View File

@ -10,6 +10,7 @@ A backlink for a note is a link from another note to that note. Links in the bac
- Removing backlinks: delete all usages of `Component.Backlinks()` from `quartz.layout.ts`. - 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 })`. - Hide when empty: hide `Backlinks` if given page doesn't contain any backlinks (default to `true`). To disable this, use `Component.Backlinks({ hideWhenEmpty: false })`.
- Hide on root: hide `Backlinks` on the home page `content/index.md` (default to `false`). To enable this, use `Component.Backlinks({ hideOnRoot: true })`.
- Component: `quartz/components/Backlinks.tsx` - Component: `quartz/components/Backlinks.tsx`
- Style: `quartz/components/styles/backlinks.scss` - Style: `quartz/components/styles/backlinks.scss`
- Script: `quartz/components/scripts/search.inline.ts` - Script: `quartz/components/scripts/search.inline.ts`

View File

@ -7,10 +7,12 @@ import OverflowListFactory from "./OverflowList"
interface BacklinksOptions { interface BacklinksOptions {
hideWhenEmpty: boolean hideWhenEmpty: boolean
hideOnRoot: boolean
} }
const defaultOptions: BacklinksOptions = { const defaultOptions: BacklinksOptions = {
hideWhenEmpty: true, hideWhenEmpty: true,
hideOnRoot: false
} }
export default ((opts?: Partial<BacklinksOptions>) => { export default ((opts?: Partial<BacklinksOptions>) => {
@ -23,6 +25,11 @@ export default ((opts?: Partial<BacklinksOptions>) => {
displayClass, displayClass,
cfg, cfg,
}: QuartzComponentProps) => { }: QuartzComponentProps) => {
// Hide backlinks on root if enabled
if (options.hideOnRoot && fileData.slug === "index") {
return null
}
const slug = simplifySlug(fileData.slug!) const slug = simplifySlug(fileData.slug!)
const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug)) const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug))
if (options.hideWhenEmpty && backlinkFiles.length == 0) { if (options.hideWhenEmpty && backlinkFiles.length == 0) {