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 ? (
) : null}
-
+
{frontMatter.title ? (
{frontMatter.title}