Skip to content

Commit bf5943d

Browse files
Add copy page as markdown feature
Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent 937bb42 commit bf5943d

3 files changed

Lines changed: 109 additions & 6 deletions

File tree

apps/docs/app/[lang]/docs/[[...slug]]/page.tsx

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { notFound } from 'next/navigation';
55
import defaultMdxComponents from 'fumadocs-ui/mdx';
66
import { Step, Steps } from 'fumadocs-ui/components/steps';
77
import { File, Folder, Files } from 'fumadocs-ui/components/files';
8+
import { CopyPageMarkdown } from '@/components/copy-page-markdown';
9+
import fs from 'fs';
10+
import path from 'path';
811

912
const components = {
1013
...defaultMdxComponents,
@@ -26,15 +29,45 @@ export default async function Page(props: {
2629
const data = page.data as any;
2730
const Content = data.body;
2831

32+
// Read the raw MDX content
33+
let rawMarkdown = '';
34+
try {
35+
const slugPath = params.slug?.join('/') || 'index';
36+
const possiblePaths = [
37+
path.join(process.cwd(), '../../content/docs', `${slugPath}.mdx`),
38+
path.join(process.cwd(), '../../content/docs', `${slugPath}.${params.lang}.mdx`),
39+
path.join(process.cwd(), '../../content/docs', slugPath, 'index.mdx'),
40+
path.join(process.cwd(), '../../content/docs', slugPath, `index.${params.lang}.mdx`),
41+
];
42+
43+
for (const filePath of possiblePaths) {
44+
if (fs.existsSync(filePath)) {
45+
rawMarkdown = fs.readFileSync(filePath, 'utf-8');
46+
break;
47+
}
48+
}
49+
} catch (error) {
50+
console.error('Failed to read raw markdown:', error);
51+
}
52+
2953
return (
3054
<DocsPage toc={data.toc} full={data.full}>
3155
<DocsBody>
32-
<h1 className="mb-2 text-3xl font-bold text-foreground">{page.data.title}</h1>
33-
{page.data.description && (
34-
<p className="mb-8 text-lg text-muted-foreground">
35-
{page.data.description}
36-
</p>
37-
)}
56+
<div className="flex items-start justify-between gap-4 mb-4">
57+
<div className="flex-1">
58+
<h1 className="mb-2 text-3xl font-bold text-foreground">{page.data.title}</h1>
59+
{page.data.description && (
60+
<p className="mb-8 text-lg text-muted-foreground">
61+
{page.data.description}
62+
</p>
63+
)}
64+
</div>
65+
{rawMarkdown && (
66+
<div className="flex-shrink-0">
67+
<CopyPageMarkdown content={rawMarkdown} />
68+
</div>
69+
)}
70+
</div>
3871
<Content components={components} />
3972
</DocsBody>
4073
</DocsPage>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use client';
2+
3+
import { useState } from 'react';
4+
import { Copy, Check } from 'lucide-react';
5+
6+
interface CopyPageMarkdownProps {
7+
content: string;
8+
}
9+
10+
export function CopyPageMarkdown({ content }: CopyPageMarkdownProps) {
11+
const [copied, setCopied] = useState(false);
12+
13+
const copyToClipboard = async () => {
14+
try {
15+
await navigator.clipboard.writeText(content);
16+
setCopied(true);
17+
setTimeout(() => setCopied(false), 2000);
18+
} catch (error) {
19+
console.error('Failed to copy:', error);
20+
}
21+
};
22+
23+
return (
24+
<button
25+
onClick={copyToClipboard}
26+
className="inline-flex items-center gap-2 rounded-md border border-input bg-background px-3 py-2 text-sm font-medium ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
27+
title="Copy entire page as Markdown"
28+
>
29+
{copied ? (
30+
<>
31+
<Check className="h-4 w-4" />
32+
<span>Copied!</span>
33+
</>
34+
) : (
35+
<>
36+
<Copy className="h-4 w-4" />
37+
<span>Copy as Markdown</span>
38+
</>
39+
)}
40+
</button>
41+
);
42+
}

content/docs/copy-test.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Copy Feature Test
3+
description: Testing the copy page as markdown feature
4+
---
5+
6+
## Introduction
7+
8+
This is a test page for the copy functionality.
9+
10+
## Example Code
11+
12+
```typescript
13+
const hello = 'world';
14+
console.log(hello);
15+
```
16+
17+
## List Example
18+
19+
- Item 1
20+
- Item 2
21+
- Item 3
22+
23+
## Table Example
24+
25+
| Column 1 | Column 2 |
26+
| --- | --- |
27+
| Data 1 | Data 2 |
28+
| Data 3 | Data 4 |

0 commit comments

Comments
 (0)