fix(links): undecodable link paths are external, NFC paths, missing images show their path (Obsidian 1.15.0)

This commit is contained in:
saberzero1 committed 2026-11-09 15:57:06 +01:00
1 parent 3c7cba46ae
commit fc865525b7
4 files changed
+64 -3

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
---
"@jackyzha0/quartz": patch
---
Links as in Obsidian 1.15.0:
- A Markdown link whose path is not valid percent-encoding (`[x](%zz)`, `[x](a%E2.md)`) is an external link. Before, it failed the whole build with `URI malformed`.
- Link paths are NFC-normalized, so a link spelled with a decomposed `é` reaches `café.md`.
- An image of a file the site doesn't have, with no alt text, gets its percent-decoded path as alt (`![](missing%20pic.png)` → “missing pic.png”), as Obsidian names a missing embed.
- A wikilink's `^id` no longer names its paragraph (Obsidian Flavored Markdown).
+35
View File
@@ -361,6 +361,41 @@ describe("links built-in", () => {
assert.strictEqual(els[1].properties.src, "../../media/a.mp4")
})
// Obsidian 1.15.0 (remark-obsidian 2.2.1's triage, re-probed): a path that is not valid
// percent-encoding names no file, so the link is external; `x:y` has a scheme, `../x:y` doesn't
test("links Obsidian reads as external", () => {
const { els } = run([a("%zz"), a("a%E2.md"), a("x:y"), a("../x:y")], "sub/note", {}, ["xy"])
const classes = els.filter((e) => e.tagName === "a").map((e) => e.properties.className)
assert.deepStrictEqual(
classes.map((c) => (c as string[])[0]),
["external", "external", "external", "internal"],
)
})
test("an image of a missing file without alt text shows its decoded path", () => {
const img = (src: string, alt?: string): Element => ({
type: "element",
tagName: "img",
properties: alt === undefined ? { src } : { src, alt },
children: [],
})
const { els } = run(
[
img("missing%20pic.png"),
img("missing%20image.png", "alt"),
img("pic.png"),
img("/static/x.png"),
],
"page",
{ markdownLinkResolution: "absolute" },
["page", "pic.png"],
)
assert.deepStrictEqual(
els.map((e) => e.properties.alt),
["missing pic.png", "alt", undefined, undefined],
)
})
test("disableBrokenWikilinks adds a broken class to missing targets", () => {
const { els } = run([a("missing")], "page", { disableBrokenWikilinks: true }, ["page"])
assert.ok((els[0].properties.className as string[]).includes("broken"))
+18 -3
View File
@@ -8,6 +8,7 @@ import { LinksConfig } from "../../cfg"
import {
FullSlug,
RelativeURL,
linkPathDecodes,
SimpleSlug,
TransformOptions,
resolveRelative,
@@ -46,7 +47,9 @@ export const legacyCrawlLinksDefaults: Partial<LinksConfig> = {
}
const isAbsolute = isAbsoluteUrl as (url: string, options?: { httpOnly?: boolean }) => boolean
const isExternalUrl = (url: string) => isAbsolute(url, { httpOnly: false })
// As in Obsidian: a URL with a scheme (`x:y`, but not `../x:y`), or a link whose path is not valid
// percent-encoding (`%zz`, `a%E2.md`), which names no file
const isExternalUrl = (url: string) => isAbsolute(url, { httpOnly: false }) || !linkPathDecodes(url)
const EXTERNAL_ICON_PATH =
"M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"
@@ -175,8 +178,20 @@ export const Links: QuartzTransformerPlugin<LinksConfig> = (userOpts) => {
typeof node.properties.src === "string"
) {
if (opts.lazyLoad) node.properties.loading = "lazy"
if (!isExternalUrl(node.properties.src)) {
node.properties.src = resolveLink(node.properties.src as RelativeURL)
const src = node.properties.src
if (!isExternalUrl(src)) {
node.properties.src = resolveLink(src as RelativeURL)
// An embed of a file the site doesn't have: Obsidian shows its path, percent-decoded
// (`![](missing%20pic.png)` → “missing pic.png”). The image shows its alt text, so
// an image without one gets the path; an author's alt text is kept.
const target =
node.tagName === "img" && !node.properties.alt && !src.startsWith("#")
? canonicalSlug(node.properties.src as string, curSlug)
: undefined
// (quartz/static is served at static/ but holds no content files)
if (target && !target.startsWith("static/") && !ctx.allSlugs.includes(target)) {
node.properties.alt = decodeURI(splitAnchor(src)[0])
}
}
}
+1
View File
@@ -20,6 +20,7 @@ export {
splitAnchor,
slugTag,
transformInternalLink,
linkPathDecodes,
transformLink,
normalizeHastElement,
} from "@quartz-community/utils"