feat(slugs): redirect from addresses that slugs.stripDiacritics changed

This commit is contained in:
saberzero1 committed 2026-11-26 23:25:25 +01:00
1 parent e0f1e03913
commit 0346dc6149
5 files changed
+164

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": minor
---
With `slugs.stripDiacritics`, the old address of every page, folder and alias whose name has diacritics (`české/žluťoučký-kůň/poznámka`) redirects to the new one (`ceske/zlutoucky-kun/poznamka`), so turning the option on doesn't break links and bookmarks to the site.
+1
View File
@@ -1,3 +1,4 @@
export { Assets } from "./assets"
export { Static } from "./static"
export { ComponentResources } from "./componentResources"
export { SlugRedirects } from "./slugRedirects"
@@ -0,0 +1,52 @@
import assert from "node:assert/strict"
import test, { describe } from "node:test"
import { VFile } from "vfile"
import { Root } from "hast"
import { ProcessedContent } from "../vfile"
import { FilePath, FullSlug, setSlugOptions, slugifyFilePath } from "../../util/path"
import { strippedAddressMoves } from "./slugRedirects"
const page = (
relativePath: string,
frontmatter: Record<string, unknown> = {},
): ProcessedContent => {
const file = new VFile("")
file.data = {
relativePath: relativePath as FilePath,
slug: slugifyFilePath(relativePath as FilePath),
frontmatter: { title: relativePath, ...frontmatter },
} as VFile["data"]
return [{ type: "root", children: [] } as Root, file]
}
describe("redirects from addresses slugs.stripDiacritics changed", () => {
test("pages, their folders and aliases redirect from the unstripped address", () => {
setSlugOptions({ stripDiacritics: true })
try {
const content = [
page("české/Žluťoučký kůň/poznámka.md", { aliases: ["stará"] }),
page("plain/note.md"),
]
const live = new Set(content.map(([, f]) => f.data.slug!))
assert.deepEqual(Object.fromEntries(strippedAddressMoves(content, live)), {
"české/žluťoučký-kůň/poznámka": "ceske/zlutoucky-kun/poznamka",
"české/index": "ceske/index",
"české/žluťoučký-kůň/index": "ceske/zlutoucky-kun/index",
stará: "ceske/zlutoucky-kun/poznamka",
})
} finally {
setSlugOptions(undefined)
}
})
test("an old address that is a page of the site is left alone", () => {
setSlugOptions({ stripDiacritics: true })
try {
const content = [page("café.md")]
const live = new Set(["café" as FullSlug, "cafe" as FullSlug])
assert.equal(strippedAddressMoves(content, live).size, 0)
} finally {
setSlugOptions(undefined)
}
})
})
+105
View File
@@ -0,0 +1,105 @@
import { QuartzEmitterPlugin } from "../types"
import { ProcessedContent } from "../vfile"
import {
FilePath,
FullSlug,
getSlugOptions,
resolveRelative,
setSlugOptions,
simplifySlug,
slugifyFilePath,
} from "../../util/path"
import { write } from "./helpers"
import { BuildCtx } from "../../util/ctx"
/** The slug `fp` had without `slugs.stripDiacritics` (lowercased, diacritics kept). */
function unstrippedSlug(fp: string): FullSlug {
const options = getSlugOptions()
setSlugOptions({ ...options, stripDiacritics: false })
try {
return slugifyFilePath(fp as FilePath)
} finally {
setSlugOptions(options)
}
}
/**
* The addresses `slugs.stripDiacritics` changed → where they moved: every published page's
* address as it was without the option (`české/žluťoučký-kůň/poznámka` →
* `ceske/zlutoucky-kun/poznamka`), its folders' (`české/` → `ceske/`) and its aliases'. An old
* address that is a page of the site, or another old address's, is left alone.
*/
export function strippedAddressMoves(
content: ProcessedContent[],
live: ReadonlySet<string>,
): Map<FullSlug, FullSlug> {
const moves = new Map<FullSlug, FullSlug>()
const add = (from: FullSlug, to: FullSlug) => {
if (from === to || live.has(from) || moves.has(from)) return
moves.set(from, to)
}
for (const [, file] of content) {
const { slug, relativePath } = file.data
if (!slug || !relativePath) continue
const old = unstrippedSlug(relativePath)
const now = slugifyFilePath(relativePath as FilePath)
// a page at its file path (not a permalink) moved with it
if (now === slug) add(old, slug)
// its folders: `české/žluťoučký-kůň/x` → `české/index`, `české/žluťoučký-kůň/index`
const oldParts = old.split("/")
const nowParts = now.split("/")
if (oldParts.length === nowParts.length) {
for (let i = 1; i < oldParts.length; i++) {
add(
[...oldParts.slice(0, i), "index"].join("/") as FullSlug,
[...nowParts.slice(0, i), "index"].join("/") as FullSlug,
)
}
}
for (const alias of (file.data.frontmatter?.aliases as unknown[] | undefined) ?? []) {
if (typeof alias !== "string") continue
const fp = alias.endsWith(".md") ? alias : `${alias}.md`
add(unstrippedSlug(fp), slug)
}
}
return moves
}
function redirectHtml(to: string, url: string): string {
return `<!DOCTYPE html>
<html lang="en-us">
<head>
<title>${to}</title>
<link rel="canonical" href="${url}">
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=${url}">
</head>
</html>
`
}
async function* emitMoves(ctx: BuildCtx, content: ProcessedContent[]) {
if (!getSlugOptions().stripDiacritics) return
const live = new Set<string>()
for (const [, file] of content) {
if (file.data.slug) live.add(file.data.slug)
for (const alias of file.data.aliases ?? []) live.add(alias)
}
for (const [from, to] of strippedAddressMoves(content, live)) {
const url = resolveRelative(from, simplifySlug(to) as unknown as FullSlug)
yield write({ ctx, slug: from, ext: ".html", content: redirectHtml(simplifySlug(to), url) })
}
}
/**
* With `configuration.slugs.stripDiacritics`, pages, folders and aliases whose names have
* diacritics get new addresses. This writes a redirect at each old address (as Quartz 5 slugged
* it: lowercased, diacritics kept) to the new one, so links and bookmarks to the old site keep
* working. alias-redirects' case redirects only cover the address as spelled in the vault.
*/
export const SlugRedirects: QuartzEmitterPlugin = () => ({
name: "SlugRedirects",
emit: (ctx, content) => emitMoves(ctx, content),
partialEmit: (ctx, content) => emitMoves(ctx, content),
})
+1
View File
@@ -749,6 +749,7 @@ export async function loadQuartzConfig(
builtinPlugins.ComponentResources(),
builtinPlugins.Assets(),
builtinPlugins.Static(),
builtinPlugins.SlugRedirects(),
]
const builtinPageTypes = [builtinPlugins.PageTypes.NotFoundPageType()]