mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-19 06:54:18 +02:00
Compare commits
3 Commits
62394fdf6e
...
dead8fcbcf
Author | SHA1 | Date | |
---|---|---|---|
![]() |
dead8fcbcf | ||
![]() |
32ec711767 | ||
![]() |
1bfad5f0d0 |
8
package-lock.json
generated
8
package-lock.json
generated
@ -79,7 +79,7 @@
|
|||||||
"@types/d3": "^7.4.3",
|
"@types/d3": "^7.4.3",
|
||||||
"@types/hast": "^3.0.4",
|
"@types/hast": "^3.0.4",
|
||||||
"@types/js-yaml": "^4.0.9",
|
"@types/js-yaml": "^4.0.9",
|
||||||
"@types/node": "^22.12.0",
|
"@types/node": "^22.13.0",
|
||||||
"@types/pretty-time": "^1.1.5",
|
"@types/pretty-time": "^1.1.5",
|
||||||
"@types/source-map-support": "^0.5.10",
|
"@types/source-map-support": "^0.5.10",
|
||||||
"@types/ws": "^8.5.14",
|
"@types/ws": "^8.5.14",
|
||||||
@ -1914,9 +1914,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@types/node": {
|
"node_modules/@types/node": {
|
||||||
"version": "22.12.0",
|
"version": "22.13.0",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.12.0.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.0.tgz",
|
||||||
"integrity": "sha512-Fll2FZ1riMjNmlmJOdAyY5pUbkftXslB5DgEzlIuNaiWhXd00FhWxVC/r4yV/4wBb9JfImTu+jiSvXTkJ7F/gA==",
|
"integrity": "sha512-ClIbNe36lawluuvq3+YYhnIN2CELi+6q8NpnM7PYp4hBn/TatfboPgVSm2rwKRfnV2M+Ty9GWDFI64KEe+kysA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -102,7 +102,7 @@
|
|||||||
"@types/d3": "^7.4.3",
|
"@types/d3": "^7.4.3",
|
||||||
"@types/hast": "^3.0.4",
|
"@types/hast": "^3.0.4",
|
||||||
"@types/js-yaml": "^4.0.9",
|
"@types/js-yaml": "^4.0.9",
|
||||||
"@types/node": "^22.12.0",
|
"@types/node": "^22.13.0",
|
||||||
"@types/pretty-time": "^1.1.5",
|
"@types/pretty-time": "^1.1.5",
|
||||||
"@types/source-map-support": "^0.5.10",
|
"@types/source-map-support": "^0.5.10",
|
||||||
"@types/ws": "^8.5.14",
|
"@types/ws": "^8.5.14",
|
||||||
|
@ -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