mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-05-18 22:44:14 +02:00
Compare commits
3 Commits
3cbe6d87ff
...
2167bc11ae
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2167bc11ae | ||
![]() |
32ec711767 | ||
![]() |
f99f05dc38 |
@ -19,6 +19,7 @@ This plugin accepts the following configuration options:
|
||||
- `openLinksInNewTab`: If `true`, configures external links to open in a new tab. Defaults to `false`.
|
||||
- `lazyLoad`: If `true`, adds lazy loading to resource elements (`img`, `video`, etc.) to improve page load performance. Defaults to `false`.
|
||||
- `externalLinkIcon`: Adds an icon next to external links when `true` (default) to visually distinguishing them from internal links.
|
||||
- `indexFrontmatterWikilinks`: If `true`, parses Obsidian-style wikilinks in the frontmatter and adds them to the graph (including things like backlinks) as if they were part of the note content. Defaults to `false`.
|
||||
|
||||
> [!warning]
|
||||
> Removing this plugin is _not_ recommended and will likely break the page.
|
||||
|
8
package-lock.json
generated
8
package-lock.json
generated
@ -79,7 +79,7 @@
|
||||
"@types/d3": "^7.4.3",
|
||||
"@types/hast": "^3.0.4",
|
||||
"@types/js-yaml": "^4.0.9",
|
||||
"@types/node": "^22.12.0",
|
||||
"@types/node": "^22.13.0",
|
||||
"@types/pretty-time": "^1.1.5",
|
||||
"@types/source-map-support": "^0.5.10",
|
||||
"@types/ws": "^8.5.14",
|
||||
@ -1914,9 +1914,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "22.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.12.0.tgz",
|
||||
"integrity": "sha512-Fll2FZ1riMjNmlmJOdAyY5pUbkftXslB5DgEzlIuNaiWhXd00FhWxVC/r4yV/4wBb9JfImTu+jiSvXTkJ7F/gA==",
|
||||
"version": "22.13.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.0.tgz",
|
||||
"integrity": "sha512-ClIbNe36lawluuvq3+YYhnIN2CELi+6q8NpnM7PYp4hBn/TatfboPgVSm2rwKRfnV2M+Ty9GWDFI64KEe+kysA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
|
@ -102,7 +102,7 @@
|
||||
"@types/d3": "^7.4.3",
|
||||
"@types/hast": "^3.0.4",
|
||||
"@types/js-yaml": "^4.0.9",
|
||||
"@types/node": "^22.12.0",
|
||||
"@types/node": "^22.13.0",
|
||||
"@types/pretty-time": "^1.1.5",
|
||||
"@types/source-map-support": "^0.5.10",
|
||||
"@types/ws": "^8.5.14",
|
||||
|
@ -13,6 +13,7 @@ import path from "path"
|
||||
import { visit } from "unist-util-visit"
|
||||
import isAbsoluteUrl from "is-absolute-url"
|
||||
import { Root } from "hast"
|
||||
import { wikilinkRegex } from "./ofm"
|
||||
|
||||
interface Options {
|
||||
/** How to resolve Markdown paths */
|
||||
@ -22,6 +23,7 @@ interface Options {
|
||||
openLinksInNewTab: boolean
|
||||
lazyLoad: boolean
|
||||
externalLinkIcon: boolean
|
||||
indexFrontmatterWikilinks: boolean
|
||||
}
|
||||
|
||||
const defaultOptions: Options = {
|
||||
@ -30,6 +32,20 @@ const defaultOptions: Options = {
|
||||
openLinksInNewTab: false,
|
||||
lazyLoad: false,
|
||||
externalLinkIcon: true,
|
||||
indexFrontmatterWikilinks: false,
|
||||
}
|
||||
|
||||
function getFullInternalLink(dest: RelativeURL, fileSlug: SimpleSlug): FullSlug {
|
||||
// url.resolve is considered legacy
|
||||
// WHATWG equivalent https://nodejs.dev/en/api/v18/url/#urlresolvefrom-to
|
||||
const url = new URL(dest, "https://base.com/" + stripSlashes(fileSlug, true))
|
||||
const canonicalDest = url.pathname
|
||||
let [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
|
||||
if (destCanonical.endsWith("/")) {
|
||||
destCanonical += "index"
|
||||
}
|
||||
// need to decodeURIComponent here as WHATWG URL percent-encodes everything
|
||||
return decodeURIComponent(stripSlashes(destCanonical, true)) as FullSlug
|
||||
}
|
||||
|
||||
export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts) => {
|
||||
@ -107,17 +123,7 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
|
||||
transformOptions,
|
||||
)
|
||||
|
||||
// url.resolve is considered legacy
|
||||
// WHATWG equivalent https://nodejs.dev/en/api/v18/url/#urlresolvefrom-to
|
||||
const url = new URL(dest, "https://base.com/" + stripSlashes(curSlug, true))
|
||||
const canonicalDest = url.pathname
|
||||
let [destCanonical, _destAnchor] = splitAnchor(canonicalDest)
|
||||
if (destCanonical.endsWith("/")) {
|
||||
destCanonical += "index"
|
||||
}
|
||||
|
||||
// need to decodeURIComponent here as WHATWG URL percent-encodes everything
|
||||
const full = decodeURIComponent(stripSlashes(destCanonical, true)) as FullSlug
|
||||
const full = getFullInternalLink(dest, curSlug)
|
||||
const simple = simplifySlug(full)
|
||||
outgoing.add(simple)
|
||||
node.properties["data-slug"] = full
|
||||
@ -157,6 +163,31 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options>> = (userOpts)
|
||||
}
|
||||
})
|
||||
|
||||
if (opts.indexFrontmatterWikilinks) {
|
||||
const strings = Object.values(file.data.frontmatter ?? {})
|
||||
.flatMap((vs) => (Array.isArray(vs) ? vs : [vs]))
|
||||
.filter((v) => typeof v === "string")
|
||||
|
||||
for (const string of strings) {
|
||||
// the regex is /g so we have to do this to get the captures
|
||||
// exec doesn't work because it's stateful and so returns null every other time (very bad)
|
||||
// we do all of that to reuse the wikilinkRegex from ofm
|
||||
const [captures] = [...string.matchAll(wikilinkRegex)]
|
||||
if (!captures || captures[0] != string || string.startsWith("!")) {
|
||||
// not matched, or didn't match the whole string, or is the embed syntax for some reason,
|
||||
// which doesn't make sense to support in frontmatter
|
||||
continue
|
||||
}
|
||||
const [_, rawFp, rawHeader] = captures
|
||||
const fp = rawFp?.trim() ?? ""
|
||||
const anchor = rawHeader?.trim() ?? ""
|
||||
const dest = transformLink(file.data.slug!, fp + anchor, transformOptions)
|
||||
const full = getFullInternalLink(dest, curSlug)
|
||||
const simple = simplifySlug(full)
|
||||
outgoing.add(simple)
|
||||
}
|
||||
}
|
||||
|
||||
file.data.links = [...outgoing]
|
||||
}
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user