mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
fix(links): don't decode link targets twice under slugs.stripDiacritics
slugOptionsOnLinkPath decoded and stripped the target's path before transformLink, which decodes it again and (utils 1.3) strips diacritics itself. It changed nothing for any link except one with a percent-encoded % (`%2520`), which it turned into a space, so the link pointed at the wrong slug and was marked broken. Removed; the stripDiacritics link tests still pass without it.
This commit is contained in:
2 files changed
+19
-49
No files matched your search
@@ -464,30 +464,24 @@ describe("links built-in", () => {
|
||||
}
|
||||
})
|
||||
|
||||
test(
|
||||
"slugs.stripDiacritics: a percent-encoded % in a link target is decoded once",
|
||||
{
|
||||
todo: "slugOptionsOnLinkPath decodes the path and transformLink decodes it again, so %2520 becomes a space",
|
||||
},
|
||||
() => {
|
||||
setSlugOptions({ stripDiacritics: true })
|
||||
try {
|
||||
// `Kódování/Mezera je %20.md`, linked as Obsidian writes a Markdown link to it
|
||||
const target = slugifyFilePath("Kódování/Mezera je %20.md" as FilePath)
|
||||
assert.strictEqual(target, "kodovani/mezera-je--percent20")
|
||||
const { els } = run(
|
||||
[a("K%C3%B3dov%C3%A1n%C3%AD/Mezera%20je%20%2520.md")],
|
||||
"index",
|
||||
{ markdownLinkResolution: "shortest" },
|
||||
["index", target],
|
||||
)
|
||||
assert.strictEqual(els[0].properties.href, "./kodovani/mezera-je--percent20")
|
||||
assert.ok(!(els[0].properties.className as string[]).includes("broken"))
|
||||
} finally {
|
||||
setSlugOptions(undefined)
|
||||
}
|
||||
},
|
||||
)
|
||||
test("slugs.stripDiacritics: a percent-encoded % in a link target is decoded once", () => {
|
||||
setSlugOptions({ stripDiacritics: true })
|
||||
try {
|
||||
// `Kódování/Mezera je %20.md`, linked as Obsidian writes a Markdown link to it
|
||||
const target = slugifyFilePath("Kódování/Mezera je %20.md" as FilePath)
|
||||
assert.strictEqual(target, "kodovani/mezera-je--percent20")
|
||||
const { els } = run(
|
||||
[a("K%C3%B3dov%C3%A1n%C3%AD/Mezera%20je%20%2520.md")],
|
||||
"index",
|
||||
{ markdownLinkResolution: "shortest" },
|
||||
["index", target],
|
||||
)
|
||||
assert.strictEqual(els[0].properties.href, "./kodovani/mezera-je--percent20")
|
||||
assert.ok(!(els[0].properties.className as string[]).includes("broken"))
|
||||
} finally {
|
||||
setSlugOptions(undefined)
|
||||
}
|
||||
})
|
||||
|
||||
test("without slugs.stripDiacritics link targets keep their diacritics", () => {
|
||||
const { els } = run([a("Příliš")], "page", { markdownLinkResolution: "shortest" }, [
|
||||
|
||||
@@ -15,8 +15,6 @@ import {
|
||||
resolveRelative,
|
||||
simplifySlug,
|
||||
slugifyFilePath,
|
||||
applySlugOptions,
|
||||
getSlugOptions,
|
||||
splitAnchor,
|
||||
stripSlashes,
|
||||
transformLink,
|
||||
@@ -235,28 +233,6 @@ export function markLinksToRemovedPages(
|
||||
/** The Links instance marks links to removed pages after filtering (see build.ts). */
|
||||
export type LinksInstance = QuartzTransformerPluginInstance & { marksBrokenLinks: boolean }
|
||||
|
||||
/**
|
||||
* Link targets are written as in the vault (`[[Čeština]]`, `../české/x.png`). With
|
||||
* `configuration.slugs.stripDiacritics` the pages and files they point at have stripped slugs,
|
||||
* so the path part of the target is stripped the same way before it is resolved (wikilinks
|
||||
* from obsidian-flavored-markdown are slugged with the plain rules first). The `#anchor` is
|
||||
* left alone: heading ids keep their diacritics.
|
||||
*/
|
||||
function slugOptionsOnLinkPath(raw: string): string {
|
||||
if (!getSlugOptions().stripDiacritics) return raw
|
||||
const hash = raw.indexOf("#")
|
||||
const linkPath = hash === -1 ? raw : raw.slice(0, hash)
|
||||
const anchor = hash === -1 ? "" : raw.slice(hash)
|
||||
let decoded = linkPath
|
||||
try {
|
||||
decoded = decodeURI(linkPath)
|
||||
} catch {
|
||||
// not percent-encoded (or malformed): strip as written
|
||||
}
|
||||
const stripped = applySlugOptions(decoded)
|
||||
return stripped === decoded ? raw : stripped + anchor
|
||||
}
|
||||
|
||||
export const Links: QuartzTransformerPlugin<LinksConfig> = (userOpts) => {
|
||||
const opts = { ...linksDefaults, ...userOpts }
|
||||
const instance: LinksInstance = {
|
||||
@@ -283,7 +259,7 @@ export const Links: QuartzTransformerPlugin<LinksConfig> = (userOpts) => {
|
||||
: fileSlug
|
||||
const rebase = targets.size > 0 || pathSlug !== fileSlug
|
||||
const resolveLink = (raw: string): RelativeURL => {
|
||||
const rel = transformLink(pathSlug, slugOptionsOnLinkPath(raw), transformOptions)
|
||||
const rel = transformLink(pathSlug, raw, transformOptions)
|
||||
if (!rebase) return rel
|
||||
const hash = rel.indexOf("#")
|
||||
const anchor = hash === -1 ? "" : rel.slice(hash)
|
||||
|
||||
Reference in new issue
Block a user