|
| 1 | +import type { FC } from 'react'; |
1 | 2 | import MarkdownPreview from '@uiw/react-markdown-preview';
|
2 | 3 | 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'; |
5 | 6 | import data from './README.md';
|
6 | 7 |
|
7 | 8 | const Preview = CodeLayout.Preview;
|
8 | 9 | const Code = CodeLayout.Code;
|
9 | 10 | const Toolbar = CodeLayout.Toolbar;
|
10 | 11 |
|
| 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 | + |
11 | 55 | export default function MarkdownExample() {
|
12 | 56 | return (
|
13 | 57 | <MarkdownPreview
|
14 | 58 | disableCopy={true}
|
15 | 59 | source={data.source}
|
16 | 60 | 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} />, |
42 | 62 | }}
|
43 | 63 | />
|
44 | 64 | );
|
|
0 commit comments