{page.data.title} {page.data.description} @@ -39,6 +56,7 @@ export default async function Page(props: PageProps<"/docs/[[...slug]]">) { components={getMDXComponents({ // this allows you to link to other pages with relative file paths a: createRelativeLink(source, page), + pre: DocsPre, })} /> diff --git a/src/app/global.css b/src/app/global.css index 2610a70..48ee178 100644 --- a/src/app/global.css +++ b/src/app/global.css @@ -26,8 +26,12 @@ } /* Shiki comment gray #6A737D is 3.65:1 on the dark code background #191919. - The token sets the variable inline, so the override has to win that declaration. */ -.dark .shiki span[style*="--shiki-dark:#6A737D"] { + The token sets the variable inline, so the override has to win that declaration. + Server HTML omits the space after the colon; the client render inserts one. */ +.dark .shiki span[style*="--shiki-dark:#6A737D"], +.dark .shiki span[style*="--shiki-dark: #6A737D"], +.dark .shiki span[style*="--shiki-dark:#6a737d"], +.dark .shiki span[style*="--shiki-dark: #6a737d"] { --shiki-dark: #8b949e !important; } diff --git a/src/components/docs-code.tsx b/src/components/docs-code.tsx new file mode 100644 index 0000000..1eed61b --- /dev/null +++ b/src/components/docs-code.tsx @@ -0,0 +1,13 @@ +"use client"; + +import { CodeBlock, Pre } from "fumadocs-ui/components/codeblock"; +import { type ComponentProps, useId } from "react"; + +export function DocsPre({ children, ...props }: ComponentProps<"pre">) { + const id = useId(); + return ( + +
{children}
+
+ ); +} diff --git a/src/components/docs-landmarks.tsx b/src/components/docs-landmarks.tsx index 4e1ecf8..5c10e95 100644 --- a/src/components/docs-landmarks.tsx +++ b/src/components/docs-landmarks.tsx @@ -1,63 +1,24 @@ "use client"; +import { usePathname } from "next/navigation"; import { useLayoutEffect } from "react"; -function headingText(heading: Element) { - return (heading.textContent ?? "") - .replaceAll("Copy Anchor Link", "") - .replace(/\s+/g, " ") - .trim(); -} - +// The mobile contents control is a
inside the TOC popover. Fumadocs +// has no prop for that element. A header stays a banner landmark unless its +// own role changes, so this sets role="group" after each client navigation. +// usePathname() is the dependency because the docs layout stays mounted while +// the page swaps; an empty dependency list would leave the next page's header +// unlabeled. export function DocsLandmarks() { - useLayoutEffect(() => { - const page = document.getElementById("nd-page"); - if (page) page.setAttribute("role", "main"); - - const pageToc = document.getElementById("nd-toc"); - if (pageToc && !pageToc.getAttribute("role")) { - pageToc.setAttribute("role", "navigation"); - pageToc.setAttribute("aria-label", "On this page"); - } + const pathname = usePathname(); + useLayoutEffect(() => { + if (!pathname.startsWith("/docs")) return; for (const header of document.querySelectorAll("#nd-docs-layout header")) { if (header.id === "nd-subnav") continue; header.setAttribute("role", "group"); - const toc = header.parentElement; - if (!toc || toc.getAttribute("role")) continue; - toc.setAttribute("role", "navigation"); - toc.setAttribute("aria-label", "Page sections"); - } - - const seen = new Map(); - for (const region of document.querySelectorAll( - '#nd-page [role="region"]', - )) { - if ( - region.getAttribute("aria-label") || - region.getAttribute("aria-labelledby") - ) { - continue; - } - let heading: Element | null = null; - let node: Element | null = region.closest("figure"); - while (node?.previousElementSibling) { - const previous = node.previousElementSibling; - if (/^H[1-6]$/.test(previous.tagName)) { - heading = previous; - break; - } - node = previous; - } - const base = heading ? `${headingText(heading)} sample` : "Code sample"; - const count = (seen.get(base) ?? 0) + 1; - seen.set(base, count); - region.setAttribute( - "aria-label", - count === 1 ? base : `${base} ${count}`, - ); } - }, []); + }, [pathname]); return null; } diff --git a/tests/docs-a11y-chrome.test.mjs b/tests/docs-a11y-chrome.test.mjs new file mode 100644 index 0000000..09fa8a5 --- /dev/null +++ b/tests/docs-a11y-chrome.test.mjs @@ -0,0 +1,97 @@ +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; +import test from "node:test"; + +const css = await readFile( + new URL("../src/app/global.css", import.meta.url), + "utf8", +); +const page = await readFile( + new URL("../src/app/docs/[[...slug]]/page.tsx", import.meta.url), + "utf8", +); +const landmarks = await readFile( + new URL("../src/components/docs-landmarks.tsx", import.meta.url), + "utf8", +); +const code = await readFile( + new URL("../src/components/docs-code.tsx", import.meta.url), + "utf8", +); +const fumadocsPage = await readFile( + new URL( + "../node_modules/fumadocs-ui/dist/layouts/docs/page/slots/container.js", + import.meta.url, + ), + "utf8", +); +const fumadocsToc = await readFile( + new URL( + "../node_modules/fumadocs-ui/dist/layouts/docs/page/slots/toc.js", + import.meta.url, + ), + "utf8", +); +const fumadocsLayout = await readFile( + new URL( + "../node_modules/fumadocs-ui/dist/layouts/docs/slots/container.js", + import.meta.url, + ), + "utf8", +); +const fumadocsHeader = await readFile( + new URL( + "../node_modules/fumadocs-ui/dist/layouts/docs/slots/header.js", + import.meta.url, + ), + "utf8", +); +const fumadocsCode = await readFile( + new URL( + "../node_modules/fumadocs-ui/dist/components/codeblock.js", + import.meta.url, + ), + "utf8", +); + +test("pins the fumadocs ids the docs chrome fix depends on", () => { + assert.match(fumadocsPage, /id:\s*"nd-page"/); + assert.match(fumadocsToc, /id:\s*"nd-toc"/); + assert.match(fumadocsLayout, /id:\s*"nd-docs-layout"/); + assert.match(fumadocsHeader, /id:\s*"nd-subnav"/); + assert.match(fumadocsCode, /\.\.\.viewportProps,\s*role:\s*"region"/); +}); + +test("pins the dark Shiki comment color override", () => { + for (const token of [ + "--shiki-dark:#6A737D", + "--shiki-dark: #6A737D", + "--shiki-dark:#6a737d", + "--shiki-dark: #6a737d", + ]) { + assert.equal(css.includes(token), true, token); + } + assert.match(css, /--shiki-dark:\s*#8b949e\s*!important;/); +}); + +test("sets docs landmark roles through fumadocs props", () => { + assert.match(page, /role="main"/); + assert.match(page, /role:\s*"navigation"/); + assert.match(page, /"aria-label":\s*"On this page"/); + assert.match(page, /"aria-label":\s*"Page sections"/); + assert.match(page, /pre:\s*DocsPre/); + assert.match( + code, + /viewportProps=\{\{\s*"aria-label":\s*`Code sample \$\{id\}`\s*\}\}/, + ); +}); + +test("reapplies the uncovered header role when the docs path changes", () => { + assert.match(landmarks, /usePathname\(\)/); + assert.match(landmarks, /useLayoutEffect\([\s\S]*?,\s*\[pathname\]\s*\)/); + assert.match(landmarks, /#nd-docs-layout header/); + assert.match(landmarks, /header\.id === "nd-subnav"/); + assert.match(landmarks, /setAttribute\("role", "group"\)/); + assert.equal(landmarks.includes("useLayoutEffect(() => {"), true); + assert.equal(/\],\s*\[\s*\]\s*\)/.test(landmarks), false); +}); From d937659c3e455f4acb08e613f49969555111c7fb Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 22:24:19 +0000 Subject: [PATCH 3/4] Label code regions by title or language and page order. useId labels were read aloud as generated ids. Each block now uses its title, or the language plus a page-order number, reset whenever the path changes. Co-authored-by: Rodrigo --- source.config.ts | 21 ++++++++++- src/app/docs/[[...slug]]/page.tsx | 18 +++++---- src/components/docs-code.tsx | 63 +++++++++++++++++++++++++++++-- src/lib/code-sample-label.mjs | 23 +++++++++++ tests/docs-a11y-chrome.test.mjs | 50 ++++++++++++++++++++++-- 5 files changed, 159 insertions(+), 16 deletions(-) create mode 100644 src/lib/code-sample-label.mjs diff --git a/source.config.ts b/source.config.ts index 4cef005..b15f308 100644 --- a/source.config.ts +++ b/source.config.ts @@ -1,3 +1,4 @@ +import { rehypeCodeDefaultOptions } from "fumadocs-core/mdx-plugins"; import { metaSchema, pageSchema } from "fumadocs-core/source/schema"; import { defineConfig, defineDocs } from "fumadocs-mdx/config"; @@ -18,6 +19,24 @@ export const docs = defineDocs({ export default defineConfig({ mdxOptions: { - // MDX options + rehypeCodeOptions: { + ...rehypeCodeDefaultOptions, + transformers: [ + ...(rehypeCodeDefaultOptions.transformers ?? []), + { + name: "kimetsu-code-language", + pre(node) { + const lang = this.options.lang; + if (typeof lang === "string" && lang.length > 0) { + node.properties.dataLanguage = lang; + } + const title = this.options.meta?.title; + if (typeof title === "string" && title.length > 0) { + node.properties.dataTitle = title; + } + }, + }, + ], + }, }, }); diff --git a/src/app/docs/[[...slug]]/page.tsx b/src/app/docs/[[...slug]]/page.tsx index 8f04460..94080b0 100644 --- a/src/app/docs/[[...slug]]/page.tsx +++ b/src/app/docs/[[...slug]]/page.tsx @@ -9,7 +9,7 @@ import { import { createRelativeLink } from "fumadocs-ui/mdx"; import type { Metadata } from "next"; import { notFound } from "next/navigation"; -import { DocsPre } from "@/components/docs-code"; +import { DocsCodeScope, DocsPre } from "@/components/docs-code"; import { getMDXComponents } from "@/components/mdx"; import { gitConfig } from "@/lib/shared"; import { getPageImage, getPageMarkdownUrl, source } from "@/lib/source"; @@ -52,13 +52,15 @@ export default async function Page(props: PageProps<"/docs/[[...slug]]">) { /> - + + + ); diff --git a/src/components/docs-code.tsx b/src/components/docs-code.tsx index 1eed61b..bea8635 100644 --- a/src/components/docs-code.tsx +++ b/src/components/docs-code.tsx @@ -1,12 +1,67 @@ "use client"; import { CodeBlock, Pre } from "fumadocs-ui/components/codeblock"; -import { type ComponentProps, useId } from "react"; +import { usePathname } from "next/navigation"; +import { + type ComponentProps, + createContext, + type ReactNode, + useContext, + useRef, +} from "react"; +import { codeSampleLabel } from "@/lib/code-sample-label.mjs"; -export function DocsPre({ children, ...props }: ComponentProps<"pre">) { - const id = useId(); +type SampleCounter = { + pathname: string; + order: number; + used: Set; +}; + +const CodeSampleCounter = createContext(null); + +export function DocsCodeScope({ children }: { children: ReactNode }) { + const pathname = usePathname(); + const counter = useRef({ + pathname: "", + order: 0, + used: new Set(), + }); + // Reset on every render, including after a client navigation changes pathname, + // so page-order numbers start at 1 for the page that is rendering. + counter.current = { pathname, order: 0, used: new Set() }; + return ( + + {children} + + ); +} + +type DocsPreProps = ComponentProps<"pre"> & { + title?: string; + "data-language"?: string; + "data-title"?: string; +}; + +export function DocsPre({ + children, + title, + "data-language": dataLanguage, + "data-title": dataTitle, + ...props +}: DocsPreProps) { + const counter = useContext(CodeSampleCounter); + if (!counter) { + throw new Error("DocsPre must render inside DocsCodeScope"); + } + counter.order += 1; + const label = codeSampleLabel({ + title: title ?? dataTitle, + language: dataLanguage, + order: counter.order, + used: counter.used, + }); return ( - +
{children}
); diff --git a/src/lib/code-sample-label.mjs b/src/lib/code-sample-label.mjs new file mode 100644 index 0000000..b37f070 --- /dev/null +++ b/src/lib/code-sample-label.mjs @@ -0,0 +1,23 @@ +const forbidden = /[«»:]/g; + +function clean(value) { + return value.replace(forbidden, "").replace(/\s+/g, " ").trim(); +} + +export function codeSampleLabel({ title, language, order, used }) { + const named = clean(title ?? ""); + const lang = clean(language ?? ""); + const base = named + ? named + : lang + ? `${lang} code ${order}` + : `Code sample ${order}`; + let label = base; + let extra = 2; + while (used.has(label)) { + label = `${base} ${extra}`; + extra += 1; + } + used.add(label); + return label; +} diff --git a/tests/docs-a11y-chrome.test.mjs b/tests/docs-a11y-chrome.test.mjs index 09fa8a5..bb25a08 100644 --- a/tests/docs-a11y-chrome.test.mjs +++ b/tests/docs-a11y-chrome.test.mjs @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import test from "node:test"; +import { codeSampleLabel } from "../src/lib/code-sample-label.mjs"; const css = await readFile( new URL("../src/app/global.css", import.meta.url), @@ -18,6 +19,10 @@ const code = await readFile( new URL("../src/components/docs-code.tsx", import.meta.url), "utf8", ); +const labelSource = await readFile( + new URL("../src/lib/code-sample-label.mjs", import.meta.url), + "utf8", +); const fumadocsPage = await readFile( new URL( "../node_modules/fumadocs-ui/dist/layouts/docs/page/slots/container.js", @@ -80,10 +85,49 @@ test("sets docs landmark roles through fumadocs props", () => { assert.match(page, /"aria-label":\s*"On this page"/); assert.match(page, /"aria-label":\s*"Page sections"/); assert.match(page, /pre:\s*DocsPre/); - assert.match( - code, - /viewportProps=\{\{\s*"aria-label":\s*`Code sample \$\{id\}`\s*\}\}/, + assert.match(page, /DocsCodeScope/); + assert.match(code, /codeSampleLabel\(/); + assert.match(code, /usePathname\(\)/); + assert.match(code, /viewportProps=\{\{\s*"aria-label":\s*label\s*\}\}/); +}); + +test("builds readable code labels without screen-reader id marks", () => { + assert.match(labelSource, /\$\{lang\} code \$\{order\}/); + assert.match(labelSource, /`Code sample \$\{order\}`/); + assert.equal(code.includes("useId"), false); + const titled = new Set(); + assert.equal( + codeSampleLabel({ + title: "Install", + language: "bash", + order: 1, + used: titled, + }), + "Install", ); + const plain = new Set(); + assert.equal( + codeSampleLabel({ language: "bash", order: 1, used: plain }), + "bash code 1", + ); + assert.equal( + codeSampleLabel({ language: "json", order: 2, used: plain }), + "json code 2", + ); + const untitled = new Set(); + assert.equal(codeSampleLabel({ order: 1, used: untitled }), "Code sample 1"); + const noisy = new Set(); + const cleaned = codeSampleLabel({ + title: "Note: «read»", + order: 1, + used: noisy, + }); + assert.equal(cleaned, "Note read"); + for (const label of [cleaned, "bash code 1", "Code sample 1", "Install"]) { + assert.equal(label.includes("«"), false, label); + assert.equal(label.includes("»"), false, label); + assert.equal(label.includes(":"), false, label); + } }); test("reapplies the uncovered header role when the docs path changes", () => { From 71117f6063d5b3bd93f650e888d3baab87937cbf Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 22:27:30 +0000 Subject: [PATCH 4/4] Stamp each code block's page order while the page is built. DocsPre only reads that order. A render-time counter could skip numbers when React renders a block twice. Co-authored-by: Rodrigo --- source.config.ts | 45 ++++++++++++++++++++++++ src/app/docs/[[...slug]]/page.tsx | 18 +++++----- src/components/docs-code.tsx | 58 ++++++++----------------------- tests/docs-a11y-chrome.test.mjs | 22 ++++++++++-- 4 files changed, 86 insertions(+), 57 deletions(-) diff --git a/source.config.ts b/source.config.ts index b15f308..1681cd2 100644 --- a/source.config.ts +++ b/source.config.ts @@ -1,6 +1,50 @@ import { rehypeCodeDefaultOptions } from "fumadocs-core/mdx-plugins"; import { metaSchema, pageSchema } from "fumadocs-core/source/schema"; import { defineConfig, defineDocs } from "fumadocs-mdx/config"; +import { codeSampleLabel } from "./src/lib/code-sample-label.mjs"; + +type HastNode = { + type?: string; + tagName?: string; + name?: string; + properties?: Record; + children?: HastNode[]; +}; + +function textProp(value: unknown) { + return typeof value === "string" ? value : undefined; +} + +function stampCodeOrder() { + return (tree: HastNode) => { + const blocks: HastNode[] = []; + const walk = (node: HastNode) => { + const isPre = + (node.type === "element" && node.tagName === "pre") || + ((node.type === "mdxJsxFlowElement" || + node.type === "mdxJsxTextElement") && + node.name === "pre"); + if (isPre) blocks.push(node); + for (const child of node.children ?? []) walk(child); + }; + walk(tree); + const used = new Set(); + for (const [index, block] of blocks.entries()) { + const order = index + 1; + if (!block.properties) block.properties = {}; + const props = block.properties; + const title = textProp(props.dataTitle) ?? textProp(props.title); + const language = textProp(props.dataLanguage); + props.dataOrder = order; + props.dataCodeLabel = codeSampleLabel({ + title, + language, + order, + used, + }); + } + }; +} // You can customize Zod schemas for frontmatter and `meta.json` here // see https://fumadocs.dev/docs/mdx/collections @@ -38,5 +82,6 @@ export default defineConfig({ }, ], }, + rehypePlugins: (plugins) => [...plugins, stampCodeOrder], }, }); diff --git a/src/app/docs/[[...slug]]/page.tsx b/src/app/docs/[[...slug]]/page.tsx index 94080b0..8f04460 100644 --- a/src/app/docs/[[...slug]]/page.tsx +++ b/src/app/docs/[[...slug]]/page.tsx @@ -9,7 +9,7 @@ import { import { createRelativeLink } from "fumadocs-ui/mdx"; import type { Metadata } from "next"; import { notFound } from "next/navigation"; -import { DocsCodeScope, DocsPre } from "@/components/docs-code"; +import { DocsPre } from "@/components/docs-code"; import { getMDXComponents } from "@/components/mdx"; import { gitConfig } from "@/lib/shared"; import { getPageImage, getPageMarkdownUrl, source } from "@/lib/source"; @@ -52,15 +52,13 @@ export default async function Page(props: PageProps<"/docs/[[...slug]]">) { /> - - - + ); diff --git a/src/components/docs-code.tsx b/src/components/docs-code.tsx index bea8635..e5f838a 100644 --- a/src/components/docs-code.tsx +++ b/src/components/docs-code.tsx @@ -1,45 +1,15 @@ "use client"; import { CodeBlock, Pre } from "fumadocs-ui/components/codeblock"; -import { usePathname } from "next/navigation"; -import { - type ComponentProps, - createContext, - type ReactNode, - useContext, - useRef, -} from "react"; +import type { ComponentProps } from "react"; import { codeSampleLabel } from "@/lib/code-sample-label.mjs"; -type SampleCounter = { - pathname: string; - order: number; - used: Set; -}; - -const CodeSampleCounter = createContext(null); - -export function DocsCodeScope({ children }: { children: ReactNode }) { - const pathname = usePathname(); - const counter = useRef({ - pathname: "", - order: 0, - used: new Set(), - }); - // Reset on every render, including after a client navigation changes pathname, - // so page-order numbers start at 1 for the page that is rendering. - counter.current = { pathname, order: 0, used: new Set() }; - return ( - - {children} - - ); -} - type DocsPreProps = ComponentProps<"pre"> & { title?: string; "data-language"?: string; "data-title"?: string; + "data-order"?: number | string; + "data-code-label"?: string; }; export function DocsPre({ @@ -47,19 +17,19 @@ export function DocsPre({ title, "data-language": dataLanguage, "data-title": dataTitle, + "data-order": dataOrder, + "data-code-label": dataCodeLabel, ...props }: DocsPreProps) { - const counter = useContext(CodeSampleCounter); - if (!counter) { - throw new Error("DocsPre must render inside DocsCodeScope"); - } - counter.order += 1; - const label = codeSampleLabel({ - title: title ?? dataTitle, - language: dataLanguage, - order: counter.order, - used: counter.used, - }); + const order = Number(dataOrder); + const label = + dataCodeLabel ?? + codeSampleLabel({ + title: title ?? dataTitle, + language: dataLanguage, + order: Number.isFinite(order) ? order : 1, + used: new Set(), + }); return (
{children}
diff --git a/tests/docs-a11y-chrome.test.mjs b/tests/docs-a11y-chrome.test.mjs index bb25a08..b61766c 100644 --- a/tests/docs-a11y-chrome.test.mjs +++ b/tests/docs-a11y-chrome.test.mjs @@ -23,6 +23,10 @@ const labelSource = await readFile( new URL("../src/lib/code-sample-label.mjs", import.meta.url), "utf8", ); +const sourceConfig = await readFile( + new URL("../source.config.ts", import.meta.url), + "utf8", +); const fumadocsPage = await readFile( new URL( "../node_modules/fumadocs-ui/dist/layouts/docs/page/slots/container.js", @@ -85,10 +89,14 @@ test("sets docs landmark roles through fumadocs props", () => { assert.match(page, /"aria-label":\s*"On this page"/); assert.match(page, /"aria-label":\s*"Page sections"/); assert.match(page, /pre:\s*DocsPre/); - assert.match(page, /DocsCodeScope/); + assert.equal(page.includes("DocsCodeScope"), false); assert.match(code, /codeSampleLabel\(/); - assert.match(code, /usePathname\(\)/); + assert.match(code, /"data-order":\s*dataOrder/); assert.match(code, /viewportProps=\{\{\s*"aria-label":\s*label\s*\}\}/); + assert.equal(code.includes("usePathname"), false); + assert.equal(code.includes("+="), false); + assert.match(sourceConfig, /dataOrder\s*=\s*order/); + assert.match(sourceConfig, /stampCodeOrder/); }); test("builds readable code labels without screen-reader id marks", () => { @@ -123,11 +131,19 @@ test("builds readable code labels without screen-reader id marks", () => { used: noisy, }); assert.equal(cleaned, "Note read"); - for (const label of [cleaned, "bash code 1", "Code sample 1", "Install"]) { + const built = [ + cleaned, + "bash code 1", + "json code 2", + "Code sample 1", + "Install", + ]; + for (const label of built) { assert.equal(label.includes("«"), false, label); assert.equal(label.includes("»"), false, label); assert.equal(label.includes(":"), false, label); } + assert.match(sourceConfig, /dataCodeLabel = codeSampleLabel/); }); test("reapplies the uncovered header role when the docs path changes", () => {