fix(links): don't mark links to emitted files (feeds, sitemap, static/) broken

This commit is contained in:
saberzero1 committed 2026-11-26 22:29:20 +01:00
1 parent 0d05ef72d8
commit 2b061ab9de
4 files changed
+68 -2

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Links: links to files Quartz emits, such as `[feed](/index.xml)`, `/sitemap.xml`, `/static/icon.png` or `/404.html`, are no longer marked `broken`. A link is only marked when its target is known to be missing: a page that doesn't exist, or a vault file (image, audio, video, PDF, canvas, base) the content doesn't have. Other files may come from an emitter, so they are left unmarked.
+1 -1
View File
@@ -32,7 +32,7 @@ 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.
- `disableBrokenWikilinks`: If `true`, internal links whose resolved slug is not present in the site (i.e. no matching file under `markdownLinkResolution`) gain an additional `broken` CSS class alongside `internal`, so they can be styled distinctly. Defaults to `true`, as Obsidian marks unresolved links. Applies to both wikilinks and regular Markdown links, since both are indistinguishable `<a>` elements by the time links are processed. Links to pages that a filter removes (e.g. [[RemoveDrafts|drafts]] or pages without `publish: true` under [[ExplicitPublish]]) are marked `broken` too, since they 404; they also get `data-filtered="<filter name>"`. A folder link stays unbroken when only the folder's `index.md` is removed, because the folder still gets a generated folder page.
- `disableBrokenWikilinks`: If `true`, internal links whose resolved slug is not present in the site (i.e. no matching file under `markdownLinkResolution`) gain an additional `broken` CSS class alongside `internal`, so they can be styled distinctly. Defaults to `true`, as Obsidian marks unresolved links. Applies to both wikilinks and regular Markdown links, since both are indistinguishable `<a>` elements by the time links are processed. Only targets Quartz can check are marked: pages, and files of the types a vault holds (images, audio, video, PDFs, canvases, bases) that the content doesn't have. Links to other files, which Quartz or a plugin may emit (`index.xml`, `sitemap.xml`, `robots.txt`, anything under `static/`), are never marked. Links to pages that a filter removes (e.g. [[RemoveDrafts|drafts]] or pages without `publish: true` under [[ExplicitPublish]]) are marked `broken` too, since they 404; they also get `data-filtered="<filter name>"`. A folder link stays unbroken when only the folder's `index.md` is removed, because the folder still gets a generated folder page.
- `enabled`: set to `false` to turn link processing off (not recommended — links break on nested pages and backlinks/graph are empty).
- `order`: position among transformers. Defaults to `60`, after the Markdown flavour plugins (e.g. [[ObsidianFlavoredMarkdown]] at `40`) that turn wikilinks into links.
+36
View File
@@ -532,6 +532,42 @@ describe("links built-in", () => {
assert.deepStrictEqual(broken, [false, false, true])
})
test("links to files Quartz emits are not broken; missing vault files are", () => {
const targets = [
"/index.xml",
"/sitemap.xml",
"/static/icon.png",
"/robots.txt",
"/404.html",
"/page.html",
"/pic.png",
"/missing.png",
"/missing.pdf",
"/missing.html",
"/v1.2",
]
const { els } = run(
targets.map((t) => a(t)),
"page",
{},
["page", "pic.png"],
)
const broken = els.map((e) => (e.properties.className as string[]).includes("broken"))
assert.deepStrictEqual(broken, [
false,
false,
false,
false,
false,
false,
false,
true,
true,
true,
true,
])
})
test("disableBrokenWikilinks: false leaves missing targets unmarked", () => {
const { els } = run([a("missing")], "page", { disableBrokenWikilinks: false }, ["page"])
assert.ok(!(els[0].properties.className as string[]).includes("broken"))
+26 -1
View File
@@ -89,6 +89,31 @@ export function pageExists(allSlugs: readonly string[], full: string): boolean {
return false
}
/** File types Obsidian opens from the vault (embeds and attachments): a link to one that the content doesn't have names a missing file. */
const VAULT_FILE_TYPES = new Set(
"avif bmp gif jpeg jpg png svg webp flac m4a mp3 ogg wav 3gp mkv mov mp4 ogv webm pdf canvas base".split(
" ",
),
)
/**
* Whether an internal link target is known to be missing, so it gets the `broken` class.
* Only targets Quartz can check are: pages (see pageExists) and files of the types a vault
* holds (images, audio, video, PDFs, canvases, bases), which the content copies as assets.
* Anything else may be a file Quartz or a plugin emits and the transformers can't know about
* (`index.xml`, `sitemap.xml`, `robots.txt`, `static/…`, og images), so it is not marked.
*/
export function linkTargetMissing(allSlugs: readonly string[], full: string): boolean {
// the not-found page is always emitted (`.html` is dropped from links like `.md`)
if (full === "404" || pageExists(allSlugs, full)) return false
// quartz/static and plugin assets are served at static/
if (full.startsWith("static/")) return false
const name = full.slice(full.lastIndexOf("/") + 1)
const ext = /\.([a-z][a-z0-9]{0,9})$/i.exec(name)?.[1]?.toLowerCase()
if (ext === undefined) return true
return VAULT_FILE_TYPES.has(ext)
}
/** The `broken` mark added after filtering, so a later rebuild can take it off again. */
const FILTERED_ATTR = "data-filtered"
@@ -278,7 +303,7 @@ export const Links: QuartzTransformerPlugin<LinksConfig> = (userOpts) => {
outgoing.add(simplifySlug(full))
node.properties["data-slug"] = full
if (opts.disableBrokenWikilinks && !pageExists(ctx.allSlugs, full)) {
if (opts.disableBrokenWikilinks && linkTargetMissing(ctx.allSlugs, full)) {
classes.push("broken")
node.properties.className = classes
}