From 162cf611350cdb6807b52c5c02cec9fba5737385 Mon Sep 17 00:00:00 2001 From: Putzwasser <26040044+putzwasser@users.noreply.github.com> Date: Wed, 28 Feb 2024 12:08:07 +0100 Subject: [PATCH] Feat: Adds logic to look for links in frontmatter - Adds outgoing/backlinks to the graph - Adds backlinks to the backlink pane --- quartz/plugins/transformers/links.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/quartz/plugins/transformers/links.ts b/quartz/plugins/transformers/links.ts index f89d367d7..ce9f15a3f 100644 --- a/quartz/plugins/transformers/links.ts +++ b/quartz/plugins/transformers/links.ts @@ -6,9 +6,11 @@ import { TransformOptions, stripSlashes, simplifySlug, + slugifyFilePath, splitAnchor, transformLink, joinSegments, + FilePath, } from "../../util/path" import path from "path" import { visit } from "unist-util-visit" @@ -23,6 +25,7 @@ interface Options { openLinksInNewTab: boolean lazyLoad: boolean externalLinkIcon: boolean + includeFrontmatterLinks: boolean } const defaultOptions: Options = { @@ -31,6 +34,7 @@ const defaultOptions: Options = { openLinksInNewTab: false, lazyLoad: false, externalLinkIcon: true, + includeFrontmatterLinks: true, } export const CrawlLinks: QuartzTransformerPlugin | undefined> = (userOpts) => { @@ -49,6 +53,18 @@ export const CrawlLinks: QuartzTransformerPlugin | undefined> = 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) => { // rewrite all links if (