Skip to content

Commit e4b44a0

Browse files
Add CopyForLLMButton to copy markdown content for AI tools
Co-authored-by: rahul.chhabria <[email protected]>
1 parent bae3b4d commit e4b44a0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use client';
2+
3+
import {useState} from 'react';
4+
import {Clipboard, Check} from 'react-feather';
5+
6+
interface Props {
7+
/** Absolute path to the markdown version of this page (e.g. `/docs/page.md`) */
8+
markdownPath: string;
9+
}
10+
11+
export function CopyForLLMButton({markdownPath}: Props) {
12+
const [copied, setCopied] = useState(false);
13+
14+
async function handleClick() {
15+
try {
16+
// Attempt to fetch the markdown content and copy it
17+
const response = await fetch(markdownPath);
18+
const text = await response.text();
19+
await navigator.clipboard.writeText(text);
20+
} catch (err) {
21+
// Fallback: copy the markdown url itself
22+
await navigator.clipboard.writeText(window.location.origin + markdownPath);
23+
} finally {
24+
setCopied(true);
25+
setTimeout(() => setCopied(false), 1200);
26+
}
27+
}
28+
29+
return (
30+
<button
31+
type="button"
32+
onClick={handleClick}
33+
title="Copy page content for LLM"
34+
className="mr-2 float-right flex items-center gap-1 border rounded px-2 py-1 text-xs hover:bg-[var(--gray-2)]"
35+
data-mdast="ignore"
36+
>
37+
{copied ? (
38+
<Check size={14} />
39+
) : (
40+
<Clipboard size={14} />
41+
)}
42+
<span className="whitespace-nowrap">Copy for LLM</span>
43+
</button>
44+
);
45+
}

src/components/docPage/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {PlatformSdkDetail} from '../platformSdkDetail';
2323
import {Sidebar} from '../sidebar';
2424
import {SidebarTableOfContents} from '../sidebarTableOfContents';
2525
import {ReaderDepthTracker} from '../track-reader-depth';
26+
import {CopyForLLMButton} from '../copyForLLMButton';
2627

2728
type Props = {
2829
children: ReactNode;
@@ -94,6 +95,7 @@ export function DocPage({
9495
>
9596
<Markdown className="flex p-0 flex-wrap" width={24} height={24} />
9697
</Link>
98+
<CopyForLLMButton markdownPath={`/${pathname}.md`} />
9799
</div>
98100
<div>
99101
<hgroup>

0 commit comments

Comments
 (0)