From 7930d4ca4782dd08eee6a5a08134e121b1333bd1 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Tue, 16 Dec 2025 22:44:01 +0800 Subject: [PATCH 1/8] chore: set `trailingSlash` to `always` --- astro.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astro.config.mjs b/astro.config.mjs index 103fef5..0f47e9f 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -10,7 +10,7 @@ import starlightHeadingBadges from "starlight-heading-badges"; export default defineConfig({ site: process.env.CPPDOC_SITE, base: process.env.CPPDOC_BASE, - trailingSlash: "never", + trailingSlash: "always", integrations: [ starlight({ title: "CppDoc", From 36a557c6875f5d97c153d9faa5ab56bfe4d0b787 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Tue, 16 Dec 2025 22:47:41 +0800 Subject: [PATCH 2/8] chore: rewrite `DocLink`'s logic --- src/components/DocLink.astro | 7 +++---- src/lib/doc-link.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/lib/doc-link.ts diff --git a/src/components/DocLink.astro b/src/components/DocLink.astro index 29e316c..2a16018 100644 --- a/src/components/DocLink.astro +++ b/src/components/DocLink.astro @@ -1,5 +1,5 @@ --- -import { getLinkToKey } from "../lib/doc-index"; +import { isLinkMissing, normalizeLink } from "../lib/doc-link"; interface Props { src: string; @@ -7,10 +7,9 @@ interface Props { } const { src, section } = Astro.props; -const rawLink = await getLinkToKey(src); -const missing = rawLink === undefined; -const link = rawLink ? rawLink + (section ? "#" + section : "") : ""; +const missing = await isLinkMissing(src); +const link = normalizeLink(src) + (section ? "#" + section : ""); --- { diff --git a/src/lib/doc-link.ts b/src/lib/doc-link.ts new file mode 100644 index 0000000..bebcce8 --- /dev/null +++ b/src/lib/doc-link.ts @@ -0,0 +1,27 @@ +import { getEntry } from "astro:content"; + +function getIdFromLink(link: string): string { + if (link.startsWith("/")) { + link = link.slice(1); + } + if (link.endsWith("/")) { + link = link.slice(0, -1); + } + return link; +} + +export function normalizeLink(link: string): string { + if (!link.startsWith("/")) { + link = "/" + link; + } + if (!link.endsWith("/")) { + link = link + "/"; + } + return link; +} + +export async function isLinkMissing(link: string): Promise { + const id = getIdFromLink(link); + const entry = await getEntry("docs", id); + return entry === undefined; +} From ac53299a188ace53eea77daf61cbef10f7085b7e Mon Sep 17 00:00:00 2001 From: Sudrut Date: Tue, 16 Dec 2025 22:48:16 +0800 Subject: [PATCH 3/8] chore: remove `doc_index.ts` --- src/lib/doc-index.ts | 80 -------------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 src/lib/doc-index.ts diff --git a/src/lib/doc-index.ts b/src/lib/doc-index.ts deleted file mode 100644 index cb82b72..0000000 --- a/src/lib/doc-index.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { getCollection } from "astro:content"; - -/** - * Build an index mapping keys to their associated pages. - * - * @returns A map from keys to page slugs. - */ -async function buildKeyIndex(): Promise> { - const docs = await getCollection("docs"); - const index = new Map(); - - for (const doc of docs) { - const keys = doc.data.cppdoc?.keys; - if (keys === undefined) continue; - if (!Array.isArray(keys)) { - console.warn("Frontmatter 'cppdoc.keys' should be an array"); - continue; - } - - if (!keys.every((item) => typeof item === "string")) - console.warn("Elements in frontmatter 'cppdoc.keys' should be strings"); - - for (const key of keys) { - if (typeof key !== "string") continue; - - if (index.has(key)) { - console.warn( - `Key "${key}" is already assigned to "${index.get(key)}", cannot reassign it to "${doc.id}"` - ); - continue; - } - - index.set(key, doc.id); - } - } - - return index; -} - -let KEY_INDEX: Map | undefined = undefined; - -/** - * Build and cache an index mapping keys to their associated pages. If the index - * has already been built, return the cached one. - * - * @returns A map from keys to page slugs. - */ -async function getOrBuildKeyIndex(): Promise> { - if (KEY_INDEX === undefined) KEY_INDEX = await buildKeyIndex(); - return KEY_INDEX; -} - -/** - * Get the link to the page associated with the given key. - * - * @returns The link to the page, or undefined if the key is not found. - */ -export async function getLinkToKey(key: string): Promise { - const index = await getOrBuildKeyIndex(); - if (key?.startsWith("/")) { - if ( - index.values().some((slug) => `/${slug}/` === key || `/${slug}` === key) - ) - return key; - else return undefined; - } - const slug = index.get(key); - if (!slug) return undefined; - return `/${slug}/`; -} - -/** - * Get a list of all keys in the key index. - * - * @returns An array of all keys. - */ -export async function getKeys(): Promise { - const index = await getOrBuildKeyIndex(); - return Array.from(index.keys()); -} From 2c5cff0952438ce7407b340207da7416cc6cf812 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Tue, 16 Dec 2025 22:55:30 +0800 Subject: [PATCH 4/8] chore: use `DocLink` in Migration Guidelines --- src/content/docs/development/migration/guideline.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/content/docs/development/migration/guideline.mdx b/src/content/docs/development/migration/guideline.mdx index 1947450..b60c1af 100644 --- a/src/content/docs/development/migration/guideline.mdx +++ b/src/content/docs/development/migration/guideline.mdx @@ -5,11 +5,12 @@ sidebar: --- import { Aside } from "@astrojs/starlight/components"; +import DocLink from "@components/DocLink.astro" This document gives guidelines to migrate content from cppreference to CppDoc.