diff --git a/packages/docs-next/src/copy-markdown-button.tsx b/packages/docs-next/src/copy-markdown-button.tsx new file mode 100644 index 00000000..99a49d2b --- /dev/null +++ b/packages/docs-next/src/copy-markdown-button.tsx @@ -0,0 +1,57 @@ +"use client"; + +import { buttonVariants } from "fumadocs-ui/components/ui/button"; +import { useCopyButton } from "fumadocs-ui/utils/use-copy-button"; +import { Check, Copy } from "lucide-react"; +import { type ReactElement, useState } from "react"; + +// Matches the Open trigger beside it: secondary text, no outline, a filled hover. +const BUTTON_CLASS = `${buttonVariants({ variant: "ghost", size: "sm" })} gap-2 bg-transparent text-fd-muted-foreground [&_svg]:size-3.5 [&_svg]:text-fd-muted-foreground`; + +const cache = new Map(); + +/** + * Copies the page's markdown twin to the clipboard, fetching `markdownUrl` + * exactly as given. + */ +// Fork of fumadocs-ui 16.17.9 MarkdownCopyButton (layouts/shared/page-actions.tsx) without its BASE_URL prefix. +export function CopyMarkdownButton({ + markdownUrl, +}: { + /** The page's markdown twin, root-relative and carrying the base path. */ + markdownUrl: string; +}): ReactElement { + const [isLoading, setLoading] = useState(false); + const [checked, onClick] = useCopyButton(async () => { + const cached = cache.get(markdownUrl); + if (cached !== undefined) return navigator.clipboard.writeText(cached); + setLoading(true); + try { + await navigator.clipboard.write([ + new ClipboardItem({ + "text/plain": fetch(markdownUrl).then(async (res) => { + if (!res.ok) throw new Error(`Failed to fetch ${markdownUrl}: ${res.status}`); + const content = await res.text(); + if (process.env.NODE_ENV === "production") cache.set(markdownUrl, content); + return content; + }), + }), + ]); + } finally { + setLoading(false); + } + }); + + return ( + + ); +} diff --git a/packages/docs-next/src/page-actions.test.tsx b/packages/docs-next/src/page-actions.test.tsx index 8f6e0773..c4ff9101 100644 --- a/packages/docs-next/src/page-actions.test.tsx +++ b/packages/docs-next/src/page-actions.test.tsx @@ -8,8 +8,8 @@ import { renderToFragment } from "../test/dom.js"; import { PageActions } from "./page-actions.js"; import { ViewOptions } from "./view-options.js"; -const MARKDOWN_URL = "/beta-docs/v2.36/admin/users.md"; -const PAGE_PATH = "/beta-docs/v2.36/admin/users/"; +const MARKDOWN_URL = "/docs/v2.36/admin/users.md"; +const PAGE_PATH = "/docs/v2.36/admin/users/"; const PAGE_URL = `https://coder.com${PAGE_PATH}`; // fumadocs' copy button reads the current pathname through `fumadocs-core`'s @@ -37,7 +37,9 @@ test("the row renders Copy Markdown and the Open dropdown trigger", () => { assert.deepEqual(buttonLabels(root), ["Copy Markdown", "Open"]); }); -test("Copy Markdown copies the page's markdown twin", async (t) => { +test("Copy Markdown fetches the markdown twin under the base path exactly once", async (t) => { + // test/bundler-env.mjs gives fumadocs the bundle's BASE_URL, which its own + // copy button prefixed onto this already-prefixed URL. const fetched: string[] = []; t.mock.method(globalThis, "fetch", async (input: string) => { fetched.push(input); @@ -106,7 +108,7 @@ function pathsOf(root: ParentNode): (string | null)[] { } test("the Open trigger carries the shared dropdown caret, so it reads as a dropdown", () => { - const root = renderToFragment(); + const root = renderToFragment(); const trigger = root.querySelector("button"); assert.ok(trigger, "the trigger renders"); assert.equal(trigger.textContent, "Open"); @@ -177,7 +179,7 @@ test("opening the dropdown lists every item, addressed at the page the reader is items.map((item) => item.getAttribute("href")), [ // The twin resolves against the page, so it keeps the base path. - "https://coder.com/beta-docs/v2.36/admin/users.md", + "https://coder.com/docs/v2.36/admin/users.md", `https://scira.ai/?q=${encoded}`, `https://chatgpt.com/?prompt=${encoded}&hints=search`, `https://claude.ai/new?q=${encoded}`, diff --git a/packages/docs-next/src/page-actions.tsx b/packages/docs-next/src/page-actions.tsx index f9ad465e..c79765e5 100644 --- a/packages/docs-next/src/page-actions.tsx +++ b/packages/docs-next/src/page-actions.tsx @@ -1,5 +1,5 @@ -import { MarkdownCopyButton } from "fumadocs-ui/layouts/docs/page"; import type { ReactElement } from "react"; +import { CopyMarkdownButton } from "./copy-markdown-button.js"; import { ViewOptions } from "./view-options.js"; /** @@ -11,15 +11,18 @@ import { ViewOptions } from "./view-options.js"; export type PageActionsOption = false | { viewOptions?: boolean }; /** - * The row of page actions under the page description: fumadocs' Copy Markdown - * button and our own "Open in ..." dropdown, both pointed at the page's - * markdown twin. + * The row of page actions under the page description: our own Copy Markdown + * button and "Open in ..." dropdown, both pointed at the page's markdown twin. * * `markdownUrl` is root-relative and carries the site's base path. - * `MarkdownCopyButton` fetches it and the dropdown links to it as an anchor + * `CopyMarkdownButton` fetches it and the dropdown links to it as an anchor * href, so the browser resolves it against the page's own origin and no * hostname is ever baked into the build. * + * The copy button is ours rather than fumadocs' `MarkdownCopyButton` because + * that one prefixes the bundler's `import.meta.env.BASE_URL`, which doubles a + * URL that already carries the base path. + * * The dropdown is ours rather than fumadocs' `ViewOptionsPopover` because its * assistant prompts name the page's Markdown twin rather than the HTML page, * and it marks only the items that leave the docs (refer to @@ -38,10 +41,7 @@ export function PageActions({ if (options === false) return null; return (
- + {options.viewOptions === false ? undefined : }
); diff --git a/packages/docs-next/test/bundler-env.mjs b/packages/docs-next/test/bundler-env.mjs new file mode 100644 index 00000000..766b7f8f --- /dev/null +++ b/packages/docs-next/test/bundler-env.mjs @@ -0,0 +1,15 @@ +// ESM loader hook: give fumadocs' modules the `import.meta.env.BASE_URL` that +// Next's bundler inlines on the site (`"/docs/"` in production), so anything of +// theirs that prefixes it behaves here as it does in the browser. Set +// DOCS_NEXT_TEST_BASE_URL="/" to run a test as a root-served build, such as a +// preview or the offline export. +const FUMADOCS = /\/node_modules\/fumadocs-(?:ui|core)\/dist\/.+\.js$/; +const BASE_URL = process.env.DOCS_NEXT_TEST_BASE_URL ?? "/docs/"; + +export async function load(url, context, nextLoad) { + const result = await nextLoad(url, context); + if (result.format !== "module" || result.source === undefined || !FUMADOCS.test(url)) + return result; + const env = `import.meta.env = { BASE_URL: ${JSON.stringify(BASE_URL)} };\n`; + return { ...result, source: env + String(result.source) }; +} diff --git a/packages/docs-next/test/setup.mjs b/packages/docs-next/test/setup.mjs index e191a2c1..9b5ea4ce 100644 --- a/packages/docs-next/test/setup.mjs +++ b/packages/docs-next/test/setup.mjs @@ -6,3 +6,4 @@ import { register } from "node:module"; register("./ignore-css.mjs", import.meta.url); register("./mock-next-navigation.mjs", import.meta.url); +register("./bundler-env.mjs", import.meta.url);