feat(backlinks): hide by default when empty (#1674)
Co-authored-by: Aaron Pham <Aaronpham0103@gmail.com>
This commit is contained in:
parent
7533d504dc
commit
05e6f05a50
2 changed files with 45 additions and 28 deletions
|
@ -9,6 +9,7 @@ A backlink for a note is a link from another note to that note. Links in the bac
|
||||||
## Customization
|
## Customization
|
||||||
|
|
||||||
- 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 })`.
|
||||||
- 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`
|
||||||
|
|
|
@ -4,14 +4,28 @@ import { resolveRelative, simplifySlug } from "../util/path"
|
||||||
import { i18n } from "../i18n"
|
import { i18n } from "../i18n"
|
||||||
import { classNames } from "../util/lang"
|
import { classNames } from "../util/lang"
|
||||||
|
|
||||||
const Backlinks: QuartzComponent = ({
|
interface BacklinksOptions {
|
||||||
|
hideWhenEmpty: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultOptions: BacklinksOptions = {
|
||||||
|
hideWhenEmpty: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ((opts?: Partial<BacklinksOptions>) => {
|
||||||
|
const options: BacklinksOptions = { ...defaultOptions, ...opts }
|
||||||
|
|
||||||
|
const Backlinks: QuartzComponent = ({
|
||||||
fileData,
|
fileData,
|
||||||
allFiles,
|
allFiles,
|
||||||
displayClass,
|
displayClass,
|
||||||
cfg,
|
cfg,
|
||||||
}: QuartzComponentProps) => {
|
}: QuartzComponentProps) => {
|
||||||
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) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div class={classNames(displayClass, "backlinks")}>
|
<div class={classNames(displayClass, "backlinks")}>
|
||||||
<h3>{i18n(cfg.locale).components.backlinks.title}</h3>
|
<h3>{i18n(cfg.locale).components.backlinks.title}</h3>
|
||||||
|
@ -30,7 +44,9 @@ const Backlinks: QuartzComponent = ({
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Backlinks.css = style
|
Backlinks.css = style
|
||||||
export default (() => Backlinks) satisfies QuartzComponentConstructor
|
|
||||||
|
return Backlinks
|
||||||
|
}) satisfies QuartzComponentConstructor
|
||||||
|
|
Loading…
Add table
Reference in a new issue