Repository navigation
fix(docs-next): fetch the markdown twin once when copying markdown #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string, string>(); | ||
|
|
||
| /** | ||
| * 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 ( | ||
| <button | ||
| type="button" | ||
| disabled={isLoading} | ||
| aria-live="polite" | ||
| onClick={onClick} | ||
| className={BUTTON_CLASS} | ||
| > | ||
| {checked ? <Check /> : <Copy />} | ||
| {checked ? "Copied Markdown" : "Copy Markdown"} | ||
| </button> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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$/; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: the hook injects
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied in 4e2949a. The hook now reads |
||
| 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) }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: #370 moves fumadocs-ui to 16.19.0, and the other fork notes there were rechecked against it. Whichever of the two lands second should update this provenance note to 16.19.0 after diffing against 16.19.0's
MarkdownCopyButton. That way the next audit has an accurate baseline. I found no behaviour change in the copy path between the two versions.