Skip to content

Commit 5319516

Browse files
committed
fix: 修复文档代码提示没有排版
1 parent 10c0568 commit 5319516

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

website/src/component/Preview/index.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,41 @@ const Preview = ({ path, ...mdData }) => {
4646
// transformImageUri={transformImageUri}
4747
source={mdData.source}
4848
rehypeRewrite={(node, index, parent) => {
49-
if (node.type === 'element' && node.tagName === 'pre' && node.children[0].data?.meta) {
50-
const meta = node.children[0].data?.meta;
51-
if (isMeta(meta)) {
52-
node.tagName = 'div';
53-
if (!node.properties) {
54-
node.properties = {};
55-
}
56-
node.properties['data-md'] = meta;
57-
node.properties['data-meta'] = 'preview';
49+
if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) {
50+
const child = node.children && node.children[0];
51+
if (child && child.properties && child.properties.ariaHidden === 'true') {
52+
child.children = [];
5853
}
5954
}
6055
}}
6156
components={{
62-
div: ({ node, ...props }) => {
63-
const { 'data-meta': meta, 'data-md': metaData } = props;
64-
if (meta === 'preview') {
65-
const line = node.position?.start.line;
66-
const metaId = getMetaId(meta) || String(line);
67-
const Child = mdData.components[`${metaId}`];
68-
if (metaId && typeof Child === 'function') {
69-
const code = mdData.data[metaId].value || '';
70-
const param = getURLParameters(metaData);
71-
return (
72-
<CodeLayout
73-
disableCheckered={getBooleanValue(param, 'disableCheckered', true)}
74-
bordered={getBooleanValue(param, 'bordered', true)}
75-
>
76-
<Preview>
77-
<Child />
78-
</Preview>
79-
<Toolbar text={code} copied={getBooleanValue(param, 'copied', true)}>
80-
{param.title || '示例'}
81-
</Toolbar>
82-
<Code>{code}</Code>
83-
</CodeLayout>
84-
);
85-
}
57+
code: ({ inline, node, ...props }) => {
58+
const { 'data-meta': meta, ...rest } = props;
59+
if (inline || !isMeta(meta)) {
60+
return <code {...props} />;
8661
}
87-
return <div {...props} />;
62+
const line = node.position?.start.line;
63+
const metaId = getMetaId(meta) || String(line);
64+
const Child = mdData.components[`${metaId}`];
65+
if (metaId && typeof Child === 'function') {
66+
const code = mdData.data[metaId].value || '';
67+
const param = getURLParameters(meta);
68+
return (
69+
<CodeLayout
70+
disableCheckered={getBooleanValue(param, 'disableCheckered', true)}
71+
bordered={getBooleanValue(param, 'bordered', true)}
72+
>
73+
<Preview>
74+
<Child />
75+
</Preview>
76+
<Toolbar text={code} copied={getBooleanValue(param, 'copied', true)}>
77+
{param.title || '示例'}
78+
</Toolbar>
79+
<Code>{code}</Code>
80+
</CodeLayout>
81+
);
82+
}
83+
return <code {...rest} />;
8884
},
8985
}}
9086
/>

0 commit comments

Comments
 (0)