Feat: Adds logic to look for links in frontmatter

- Adds outgoing/backlinks to the graph
- Adds backlinks to the backlink pane
This commit is contained in:
Putzwasser 2024-02-28 12:08:07 +01:00
parent f200a0be22
commit 162cf61135

View File

@ -6,9 +6,11 @@ import {
TransformOptions, TransformOptions,
stripSlashes, stripSlashes,
simplifySlug, simplifySlug,
slugifyFilePath,
splitAnchor, splitAnchor,
transformLink, transformLink,
joinSegments, joinSegments,
FilePath,
} from "../../util/path" } from "../../util/path"
import path from "path" import path from "path"
import { visit } from "unist-util-visit" import { visit } from "unist-util-visit"
@ -23,6 +25,7 @@ interface Options {
openLinksInNewTab: boolean openLinksInNewTab: boolean
lazyLoad: boolean lazyLoad: boolean
externalLinkIcon: boolean externalLinkIcon: boolean
includeFrontmatterLinks: boolean
} }
const defaultOptions: Options = { const defaultOptions: Options = {
@ -31,6 +34,7 @@ const defaultOptions: Options = {
openLinksInNewTab: false, openLinksInNewTab: false,
lazyLoad: false, lazyLoad: false,
externalLinkIcon: true, externalLinkIcon: true,
includeFrontmatterLinks: true,
} }
export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => { export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
@ -49,6 +53,18 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
allSlugs: ctx.allSlugs, allSlugs: ctx.allSlugs,
} }
// Add frontmatter links to outgoing links
if (opts.includeFrontmatterLinks && file.data.frontmatter) {
for (const [fmKey, fmValue] of Object.entries(file.data.frontmatter)) {
if (fmValue && typeof fmValue === "string") {
const dest = fmValue.match(/\[\[(.*)\]\]/)?.[1] as FilePath ?? null
if (dest) {
outgoing.add(simplifySlug(slugifyFilePath(dest)))
}
}
}
}
visit(tree, "element", (node, _index, _parent) => { visit(tree, "element", (node, _index, _parent) => {
// rewrite all links // rewrite all links
if ( if (