Skip to content

Commit 92b3a20

Browse files
committed
fix(Preview): 示例预览组件将节点挂在到div下
1 parent 60e6917 commit 92b3a20

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

website/src/component/Preview/index.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,49 @@ const Preview = ({ path, ...mdData }) => {
4646
// transformImageUri={transformImageUri}
4747
source={mdData.source}
4848
rehypeRewrite={(node, index, parent) => {
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 = [];
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';
5358
}
5459
}
5560
}}
5661
components={{
57-
code: ({ inline, node, ...props }) => {
58-
const { 'data-meta': meta, ...rest } = props;
59-
if (inline || !isMeta(meta)) {
60-
return <code {...props} />;
61-
}
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>
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+
disableToolbar={getBooleanValue(param, 'disableToolbar', false)}
75+
disableCode={getBooleanValue(param, 'disableCode', false)}
76+
disablePreview={getBooleanValue(param, 'disablePreview', false)}
77+
bordered={getBooleanValue(param, 'bordered', true)}
78+
copied={getBooleanValue(param, 'copied', true)}
79+
background={param.background}
80+
toolbar={param.title || '示例'}
81+
codeProps={{ style: { padding: 0 } }}
82+
style={{ padding: 0 }}
83+
code={<pre {...props} />}
84+
text={code}
85+
>
7486
<Child />
75-
</Preview>
76-
<Toolbar text={code} copied={getBooleanValue(param, 'copied', true)}>
77-
{param.title || '示例'}
78-
</Toolbar>
79-
<Code>{code}</Code>
80-
</CodeLayout>
81-
);
87+
</CodeLayout>
88+
);
89+
}
8290
}
83-
return <code {...rest} />;
91+
return <div {...props} />;
8492
},
8593
}}
8694
/>

0 commit comments

Comments
 (0)