From 1f431cdf62c1ab1f9b7d4d4078d428e6d016e564 Mon Sep 17 00:00:00 2001 From: Brandon Kramer Date: Fri, 20 Nov 2026 12:43:14 -0500 Subject: [PATCH 1/3] feat: add Copy page button using GDS Reworks #1089 to use ExperimentalCopyButton. Co-authored-by: PaulieB14 --- website/src/components/CopyPageButton.tsx | 107 ++++++++++++++++++ website/src/components/index.ts | 1 + .../src/layout/templates/default/content.tsx | 26 +++-- website/src/pages/en/global.json | 2 + 4 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 website/src/components/CopyPageButton.tsx diff --git a/website/src/components/CopyPageButton.tsx b/website/src/components/CopyPageButton.tsx new file mode 100644 index 000000000000..c554c16bf745 --- /dev/null +++ b/website/src/components/CopyPageButton.tsx @@ -0,0 +1,107 @@ +import { useRef } from 'react' + +import { ExperimentalCopyButton, type ExperimentalCopyButtonProps } from '@edgeandnode/gds' + +import { useI18n } from '@/i18n' + +/** + * Turn an "Edit on GitHub" URL (`/blob/` for local pages, `/tree/` for remote pages) into the URL of the raw file. + */ +export function getRawSourceUrl(githubUrl: string) { + return githubUrl.replace( + /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/(?:blob|tree)\//, + 'https://raw.githubusercontent.com/$1/$2/', + ) +} + +const FENCE = /^\s*(```|~~~)/ +const MDX_IMPORT = /^import\s.+\sfrom\s+['"][^'"]+['"];?\s*$|^import\s+['"][^'"]+['"];?\s*$/ + +/** + * Turn a page's MDX source into Markdown that can be pasted into an LLM: replace the YAML frontmatter with the + * page title and its URL, and remove MDX `import` lines. Lines inside code blocks are never removed. + */ +export function toLlmMarkdown(source: string, { title, url }: { title?: string; url: string }) { + let inFence = false + const body = source + .replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, '') + .split(/\r?\n/) + .filter((line) => { + if (FENCE.test(line)) inFence = !inFence + return inFence || !MDX_IMPORT.test(line) + }) + .join('\n') + .replace(/\n{3,}/g, '\n\n') + .trim() + // Pages sourced from other repos keep their own `# Title`, so only add one when it's missing + const ownHeading = /^# .*/.exec(body)?.[0] + const heading = ownHeading ?? (title ? `# ${title}` : null) + const content = ownHeading ? body.slice(ownHeading.length).trim() : body + return `${[heading, `Source: ${url}`, content].filter(Boolean).join('\n\n')}\n` +} + +interface CopyPageButtonProps extends Omit { + /** URL of the page's source file on GitHub (the "Edit on GitHub" URL). */ + sourceUrl: string + /** Page title, added as a heading since the frontmatter is removed. */ + title?: string +} + +/** + * Copies the current page as Markdown, for pasting into an LLM. + */ +export function CopyPageButton({ sourceUrl, title, onClick, onFocus, onPointerEnter, ...props }: CopyPageButtonProps) { + const { t } = useI18n() + const markdown = useRef | null>(null) + + // Start loading on hover or focus, so the source is usually ready by the time the button is clicked + const load = () => { + if (!markdown.current) { + const promise = fetch(getRawSourceUrl(sourceUrl)) + .then((response) => { + if (!response.ok) throw new Error(`Failed to load ${response.url} (${response.status})`) + return response.text() + }) + .then((source) => toLlmMarkdown(source, { title, url: window.location.href.split('#')[0]! })) + // Allow a retry if loading failed + promise.catch(() => { + markdown.current = null + }) + markdown.current = promise + } + return markdown.current + } + + const copy = async () => { + const text = load() + // Safari only allows writing to the clipboard during the click itself, so pass it the pending text + if (typeof ClipboardItem !== 'undefined' && 'write' in navigator.clipboard) { + const blob = text.then((value) => new Blob([value], { type: 'text/plain' })) + await navigator.clipboard.write([new ClipboardItem({ 'text/plain': blob })]) + } else { + await navigator.clipboard.writeText(await text) + } + } + + return ( + { + onPointerEnter?.(event) + void load() + }} + onFocus={(event) => { + onFocus?.(event) + void load() + }} + onClick={(event) => { + onClick?.(event) + void copy().catch(() => undefined) + }} + {...props} + > + {t('global.page.copy')} + + ) +} diff --git a/website/src/components/index.ts b/website/src/components/index.ts index 64ec47d494e8..73a36db6acaf 100644 --- a/website/src/components/index.ts +++ b/website/src/components/index.ts @@ -1,6 +1,7 @@ export * from './Callout' export * from './Card' export * from './CodeBlock' +export * from './CopyPageButton' export * from './DocSearch' export * from './HashRedirect' export * from './Heading' diff --git a/website/src/layout/templates/default/content.tsx b/website/src/layout/templates/default/content.tsx index d8397b04f9ee..7b4e69035dbc 100644 --- a/website/src/layout/templates/default/content.tsx +++ b/website/src/layout/templates/default/content.tsx @@ -3,6 +3,7 @@ import { type ComponentProps, useContext } from 'react' import { ButtonOrLink, classNames, ExperimentalDivider, ExperimentalLink } from '@edgeandnode/gds' import { ArrowLeft, ArrowRight, CalendarDynamic, HourglassDynamic, SocialGitHub } from '@edgeandnode/gds/icons' +import { CopyPageButton } from '@/components' import { useI18n } from '@/i18n' import { LayoutContext } from '../../shared' @@ -39,17 +40,20 @@ export default function TemplateDefaultContent({ className, children, ...props } > {!frontMatter.hideContentHeader ? (
- {readingTime ? ( - - -

- {Math.ceil(readingTime.minutes)} {t('global.page.readingTime.minutes')} -

-
- ) : null} +
+ {readingTime ? ( + + +

+ {Math.ceil(readingTime.minutes)} {t('global.page.readingTime.minutes')} +

+
+ ) : null} + +
{frontMatter.title ? (

{frontMatter.title}

) : null} diff --git a/website/src/pages/en/global.json b/website/src/pages/en/global.json index a39d29fd121c..d988805eb578 100644 --- a/website/src/pages/en/global.json +++ b/website/src/pages/en/global.json @@ -19,6 +19,8 @@ "previous": "Previous page", "next": "Next page", "edit": "Edit on GitHub", + "copy": "Copy page", + "copied": "Copied as Markdown", "onThisPage": "On this page", "tableOfContents": "Table of contents", "linkToThisSection": "Link to this section" From 7085969346887cbf0fb1a7ba34c618877101883b Mon Sep 17 00:00:00 2001 From: Brandon Kramer Date: Fri, 20 Nov 2026 13:41:40 -0500 Subject: [PATCH 2/3] fix: only confirm Copy page after the copy succeeds Co-authored-by: PaulieB14 --- website/src/components/CopyPageButton.tsx | 63 +++++++++++++++-------- website/src/pages/en/global.json | 1 + 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/website/src/components/CopyPageButton.tsx b/website/src/components/CopyPageButton.tsx index c554c16bf745..fa18f1188b50 100644 --- a/website/src/components/CopyPageButton.tsx +++ b/website/src/components/CopyPageButton.tsx @@ -1,6 +1,7 @@ -import { useRef } from 'react' +import { type FocusEvent, type MouseEvent, type PointerEvent, useEffect, useRef, useState } from 'react' -import { ExperimentalCopyButton, type ExperimentalCopyButtonProps } from '@edgeandnode/gds' +import { ExperimentalButton, type ExperimentalButtonProps, Tooltip } from '@edgeandnode/gds' +import { CopyInteractive } from '@edgeandnode/gds/icons' import { useI18n } from '@/i18n' @@ -40,7 +41,7 @@ export function toLlmMarkdown(source: string, { title, url }: { title?: string; return `${[heading, `Source: ${url}`, content].filter(Boolean).join('\n\n')}\n` } -interface CopyPageButtonProps extends Omit { +interface CopyPageButtonProps extends Omit { /** URL of the page's source file on GitHub (the "Edit on GitHub" URL). */ sourceUrl: string /** Page title, added as a heading since the frontmatter is removed. */ @@ -48,11 +49,19 @@ interface CopyPageButtonProps extends Omit | null>(null) + const [status, setStatus] = useState<'idle' | 'copied' | 'failed'>('idle') + + useEffect(() => { + if (status === 'idle') return + const timer = setTimeout(() => setStatus('idle'), 2000) + return () => clearTimeout(timer) + }, [status]) // Start loading on hover or focus, so the source is usually ready by the time the button is clicked const load = () => { @@ -84,24 +93,36 @@ export function CopyPageButton({ sourceUrl, title, onClick, onFocus, onPointerEn } return ( - { - onPointerEnter?.(event) - void load() - }} - onFocus={(event) => { - onFocus?.(event) - void load() - }} - onClick={(event) => { - onClick?.(event) - void copy().catch(() => undefined) + - {t('global.page.copy')} - + } + onPointerEnter={(event: PointerEvent) => { + onPointerEnter?.(event) + void load() + }} + onFocus={(event: FocusEvent) => { + onFocus?.(event) + void load() + }} + onClick={(event: MouseEvent) => { + onClick?.(event) + setStatus('idle') + copy().then( + () => setStatus('copied'), + () => setStatus('failed'), + ) + }} + {...props} + > + {t('global.page.copy')} + + ) } diff --git a/website/src/pages/en/global.json b/website/src/pages/en/global.json index d988805eb578..19332bd29a13 100644 --- a/website/src/pages/en/global.json +++ b/website/src/pages/en/global.json @@ -21,6 +21,7 @@ "edit": "Edit on GitHub", "copy": "Copy page", "copied": "Copied as Markdown", + "copyFailed": "Couldn't copy this page", "onThisPage": "On this page", "tableOfContents": "Table of contents", "linkToThisSection": "Link to this section" From 952593ebe2a61c4278e87549f3d58b347de6b596 Mon Sep 17 00:00:00 2001 From: Jerry Okolo <73024324+itsjerryokolo@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:17:04 +0100 Subject: [PATCH 3/3] Reset the copy button when the docs page changes --- website/src/layout/templates/default/content.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/layout/templates/default/content.tsx b/website/src/layout/templates/default/content.tsx index 7b4e69035dbc..02c9cf7ca6d4 100644 --- a/website/src/layout/templates/default/content.tsx +++ b/website/src/layout/templates/default/content.tsx @@ -52,7 +52,7 @@ export default function TemplateDefaultContent({ className, children, ...props }

) : null} - + {frontMatter.title ? (

{frontMatter.title}