mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-18 14:34:23 +02:00
Merge 06a9337ad11ed2b8b09590753e7c0048eb295272 into 7be47742a6dc86f22d148ca9d304f7a9eea318cf
This commit is contained in:
commit
6bc1515a41
@ -1,6 +1,6 @@
|
|||||||
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
||||||
import style from "./styles/backlinks.scss"
|
import style from "./styles/backlinks.scss"
|
||||||
import { resolveRelative, simplifySlug } from "../util/path"
|
import { FullSlug, resolveRelative, SimpleSlug, simplifySlug } from "../util/path"
|
||||||
import { i18n } from "../i18n"
|
import { i18n } from "../i18n"
|
||||||
import { classNames } from "../util/lang"
|
import { classNames } from "../util/lang"
|
||||||
|
|
||||||
@ -15,14 +15,44 @@ const defaultOptions: BacklinksOptions = {
|
|||||||
export default ((opts?: Partial<BacklinksOptions>) => {
|
export default ((opts?: Partial<BacklinksOptions>) => {
|
||||||
const options: BacklinksOptions = { ...defaultOptions, ...opts }
|
const options: BacklinksOptions = { ...defaultOptions, ...opts }
|
||||||
|
|
||||||
|
let backlinks: Map<SimpleSlug, Array<{ slug: FullSlug; title: string }>> | undefined
|
||||||
|
|
||||||
const Backlinks: QuartzComponent = ({
|
const Backlinks: QuartzComponent = ({
|
||||||
fileData,
|
fileData,
|
||||||
allFiles,
|
allFiles,
|
||||||
displayClass,
|
displayClass,
|
||||||
cfg,
|
cfg,
|
||||||
}: QuartzComponentProps) => {
|
}: QuartzComponentProps) => {
|
||||||
const slug = simplifySlug(fileData.slug!)
|
if (!backlinks) {
|
||||||
const backlinkFiles = allFiles.filter((file) => file.links?.includes(slug))
|
backlinks = new Map()
|
||||||
|
|
||||||
|
const aliasMap = new Map<SimpleSlug, SimpleSlug>()
|
||||||
|
for (const file of allFiles) {
|
||||||
|
for (const alias of file.aliases ?? []) {
|
||||||
|
aliasMap.set(simplifySlug(alias), simplifySlug(file.slug!))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const file of allFiles) {
|
||||||
|
const seen = new Set<SimpleSlug>()
|
||||||
|
for (let link of file.links ?? []) {
|
||||||
|
link = aliasMap.get(link) ?? link
|
||||||
|
// avoid aliased duplicates
|
||||||
|
if (seen.has(link)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen.add(link)
|
||||||
|
let ref = backlinks.get(link)
|
||||||
|
if (!ref) {
|
||||||
|
backlinks.set(link, (ref = []))
|
||||||
|
}
|
||||||
|
ref.push({ slug: file.slug!, title: file.frontmatter?.title! })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const backlinkFiles = backlinks.get(simplifySlug(fileData.slug!)) ?? []
|
||||||
|
|
||||||
if (options.hideWhenEmpty && backlinkFiles.length == 0) {
|
if (options.hideWhenEmpty && backlinkFiles.length == 0) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@ -33,8 +63,8 @@ export default ((opts?: Partial<BacklinksOptions>) => {
|
|||||||
{backlinkFiles.length > 0 ? (
|
{backlinkFiles.length > 0 ? (
|
||||||
backlinkFiles.map((f) => (
|
backlinkFiles.map((f) => (
|
||||||
<li>
|
<li>
|
||||||
<a href={resolveRelative(fileData.slug!, f.slug!)} class="internal">
|
<a href={resolveRelative(fileData.slug!, f.slug)} class="internal">
|
||||||
{f.frontmatter?.title}
|
{f.title}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
))
|
))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user