Skip to content

Commit b3ae84f

Browse files
committed
feat: show error message for "Copy Markdown" button (#61)
1 parent caa658d commit b3ae84f

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

src/app/docs/[[...slug]]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default async function Page(props: PageProps<"/docs/[[...slug]]">) {
7272
)}
7373
{page.path && (
7474
<div className="flex flex-row gap-2 shrink-0">
75-
<LLMCopyButton pagePath={page.path} />
75+
<LLMCopyButton slugs={page.slugs} />
7676
<ViewOptions pagePath={page.path} githubUrl={githubUrl} />
7777
</div>
7878
)}

src/components/ai/page-actions.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,33 @@ import { getMarkdownContent } from "@/lib/get-markdown-content";
1111

1212
const cache = new Map<string, string>();
1313

14-
// Helper function to normalize doc file paths to GitHub URL format
15-
function normalizeDocPathForGithub(path: string): string {
16-
let normalized = path.startsWith("content/") ? path : `content/${path}`;
17-
if (!normalized.startsWith("content/docs/")) {
18-
normalized = normalized.replace(/^content\//, "content/docs/");
19-
}
20-
return normalized;
21-
}
22-
2314
export function LLMCopyButton({
2415
/**
25-
* The page path for fetching the raw Markdown/MDX content
16+
* The page slugs for fetching the raw Markdown/MDX content
2617
*/
27-
pagePath,
18+
slugs,
2819
}: {
29-
pagePath: string;
20+
slugs: string[];
3021
}) {
3122
const [isLoading, setLoading] = useState(false);
3223
const [checked, onClick] = useCopyButton(async () => {
3324
try {
3425
setLoading(true);
35-
const cached = cache.get(pagePath);
36-
const content = cached || (await getMarkdownContent(pagePath));
26+
const cacheKey = slugs.join("/");
27+
const cached = cache.get(cacheKey);
28+
const content = cached || (await getMarkdownContent(slugs));
3729

3830
if (!cached) {
39-
cache.set(pagePath, content);
31+
cache.set(cacheKey, content);
4032
}
4133

4234
await navigator.clipboard.writeText(content);
4335
} catch (error) {
4436
console.error("Failed to copy markdown to clipboard:", error);
45-
window.alert("Failed to copy the markdown to your clipboard. Please copy it manually.");
37+
const errorMessage = error instanceof Error ? error.message : String(error);
38+
window.alert(
39+
`Failed to copy the markdown to your clipboard: ${errorMessage}\n\nPlease copy it manually.`,
40+
);
4641
throw error;
4742
} finally {
4843
setLoading(false);

src/lib/get-markdown-content.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22

33
import { source, getLLMText } from "./source";
44

5-
export async function getMarkdownContent(pagePath: string): Promise<string> {
5+
export async function getMarkdownContent(slugs: string[]): Promise<string> {
66
try {
7-
// Get all pages from the source
8-
const pages = source.getPages();
9-
10-
// Normalize the path - remove leading/trailing slashes and 'content/' prefix if present
11-
let normalizedPath = pagePath.replace(/^\/|\/$/g, "").replace(/^content\//, "");
12-
13-
// Try to find a matching page
14-
// page.path from Fumadocs is typically in the format 'docs/...' or similar
15-
const candidates = [
16-
normalizedPath,
17-
normalizedPath.startsWith("docs/") ? normalizedPath : `docs/${normalizedPath}`,
18-
];
19-
20-
let page;
21-
for (const candidate of candidates) {
22-
page = pages.find((p) => p.path === candidate);
23-
if (page) break;
24-
}
7+
// Use slugs to get the page directly from the source
8+
const page = source.getPage(slugs);
259

2610
if (!page) {
27-
throw new Error(`Page not found: ${pagePath}`);
11+
throw new Error(`Page not found for slugs: ${slugs.join("/")}`);
2812
}
2913

3014
// Get the formatted markdown content with metadata (title, URL, source, description)

0 commit comments

Comments
 (0)