Skip to content

Commit 8d4688f

Browse files
committed
website: update example.
1 parent 3c11cc6 commit 8d4688f

File tree

4 files changed

+55
-59
lines changed

4 files changed

+55
-59
lines changed

website/.kktrc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default (conf: Configuration, env: 'development' | 'production', options:
88
conf = mdCodeModulesLoader(conf);
99

1010
conf.module!.exprContextCritical = false;
11+
conf.ignoreWarnings = [
12+
{
13+
module: /node_modules[\\/]parse5[\\/]/,
14+
},
15+
];
1116
if (env === 'production') {
1217
conf.output = { ...conf.output, publicPath: './' };
1318
}

website/src/pages/doc/index.tsx

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
import data from 'react-code-preview-layout/README.md';
2-
import CodeLayout from 'react-code-preview-layout';
32
import MarkdownPreview from '@uiw/react-markdown-preview';
4-
import { getMetaId, isMeta, getURLParameters } from 'markdown-react-code-preview-loader';
5-
6-
const Preview = CodeLayout.Preview;
7-
const Code = CodeLayout.Code;
8-
const Toolbar = CodeLayout.Toolbar;
3+
import { CodePreview } from '../markdown-example';
94

105
const Doc = () => (
116
<MarkdownPreview
127
disableCopy={true}
138
source={data.source}
149
components={{
15-
code: ({ inline, node, ...props }) => {
16-
const { 'data-meta': meta, ...rest } = props as any;
17-
if (inline || !isMeta(meta)) {
18-
return <code {...props} />;
19-
}
20-
const line = node.position?.start.line;
21-
const metaId = getMetaId(meta) || String(line);
22-
const Child = data.components[`${metaId}`];
23-
if (metaId && typeof Child === 'function') {
24-
const code = data.data[metaId].value || '';
25-
const param = getURLParameters(meta);
26-
return (
27-
<CodeLayout>
28-
<Preview>
29-
<Child />
30-
</Preview>
31-
<Toolbar text={code}>{param.title || 'Code Example'}</Toolbar>
32-
<Code>
33-
<code {...rest} />
34-
</Code>
35-
</CodeLayout>
36-
);
37-
}
38-
return <code {...rest} />;
39-
},
10+
pre: (rest) => <CodePreview {...rest} components={data.components} data={data.data} />,
4011
}}
4112
/>
4213
);

website/src/pages/markdown-example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Markdown Preview Example
22
===
33

4-
这里是一个示例
4+
这里是一个`示例`
55

66
### 基本用法
77

website/src/pages/markdown-example/index.tsx

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,64 @@
1+
import type { FC } from 'react';
12
import MarkdownPreview from '@uiw/react-markdown-preview';
23
import CodeLayout from 'react-code-preview-layout';
3-
import { getCodeString } from 'rehype-rewrite';
4-
import { getMetaId, isMeta, getURLParameters } from 'markdown-react-code-preview-loader';
4+
import { getMetaId, isMeta, getURLParameters, type CodeBlockData } from 'markdown-react-code-preview-loader';
5+
import type { Element, RootContent } from 'hast';
56
import data from './README.md';
67

78
const Preview = CodeLayout.Preview;
89
const Code = CodeLayout.Code;
910
const Toolbar = CodeLayout.Toolbar;
1011

12+
type CodePreviewProps = React.HTMLAttributes<HTMLPreElement> & {
13+
node?: RootContent;
14+
components: CodeBlockData['components'];
15+
data: CodeBlockData['data'];
16+
};
17+
18+
export const CodePreview: FC<CodePreviewProps> = ({ components, data, node, ...props }) => {
19+
if (node && node.type === 'element' && node.tagName === 'pre') {
20+
const child = node.children[0] as Element;
21+
if (!child) return <pre {...props} />;
22+
const meta = ((child.data as any)?.meta || child.properties?.dataMeta) as string;
23+
if (!isMeta(meta)) {
24+
return <pre {...props} />;
25+
}
26+
const line = node?.position?.start.line;
27+
const metaId = getMetaId(meta) || String(line);
28+
const Child = components[`${metaId}`];
29+
if (metaId && typeof Child === 'function') {
30+
const code = data[metaId].value || '';
31+
const { title, boreder = 1, checkered = 1, code: codeNum = 1, toolbar = 1 } = getURLParameters(meta || '');
32+
return (
33+
<CodeLayout bordered={!!Number(boreder)} disableCheckered={!Number(checkered)} style={{ marginBottom: 16 }}>
34+
<Preview>
35+
<Child />
36+
</Preview>
37+
{!!Number(toolbar) && (
38+
<Toolbar text={code} visibleButton={!!Number(codeNum)}>
39+
{title || 'Code Example'}
40+
</Toolbar>
41+
)}
42+
43+
{!!Number(codeNum) && (
44+
<Code tagName="pre" style={{ marginBottom: 0 }} className={props.className}>
45+
{props.children}
46+
</Code>
47+
)}
48+
</CodeLayout>
49+
);
50+
}
51+
}
52+
return <code {...props} />;
53+
};
54+
1155
export default function MarkdownExample() {
1256
return (
1357
<MarkdownPreview
1458
disableCopy={true}
1559
source={data.source}
1660
components={{
17-
code: ({ inline, node, ...props }) => {
18-
const { 'data-meta': meta, ...rest } = props as any;
19-
if (inline || !isMeta(meta)) {
20-
return <code {...props} />;
21-
}
22-
const line = node.position?.start.line;
23-
const metaId = getMetaId(meta) || String(line);
24-
const Child = data.components[`${metaId}`];
25-
if (metaId && typeof Child === 'function') {
26-
const code = getCodeString(node.children);
27-
const param = getURLParameters(meta);
28-
return (
29-
<CodeLayout>
30-
<Preview>
31-
<Child />
32-
</Preview>
33-
<Toolbar text={code}>{param.title || 'Code Example'}</Toolbar>
34-
<Code>
35-
<code {...rest} />
36-
</Code>
37-
</CodeLayout>
38-
);
39-
}
40-
return <code {...rest} />;
41-
},
61+
pre: (rest) => <CodePreview {...rest} components={data.components} data={data.data} />,
4262
}}
4363
/>
4464
);

0 commit comments

Comments
 (0)