Skip to content

Commit 1c67dba

Browse files
committed
chore(deps): update dependency react-code-preview-layout to v3 #141
1 parent e9d4b72 commit 1c67dba

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@uiw/react-codesandbox": "^1.1.5",
4545
"@uiw/react-codepen": "^1.0.2",
4646
"markdown-react-code-preview-loader": "^2.1.2",
47-
"react-code-preview-layout": "^2.0.6",
47+
"react-code-preview-layout": "^3.0.0",
4848
"@kkt/less-modules": "^7.4.9",
4949
"@kkt/ncc": "~1.0.8",
5050
"@kkt/raw-modules": "^7.4.9",
@@ -59,6 +59,10 @@
5959
"react": "^18.2.0",
6060
"react-dom": "^18.2.0",
6161
"tsbb": "^4.1.5",
62+
"jest": "^29.5.0",
63+
"jest-watch-typeahead": "^2.2.2",
64+
"jest-environment-jsdom": "~29.5.0",
65+
"jest-environment-node": "^29.5.0",
6266
"uiw": "^4.21.14"
6367
},
6468
"eslintConfig": {

website/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import GitHubCorners from '@uiw/react-github-corners';
33
import '@uiw/reset.css';
4+
import "@wcj/dark-mode"
45
import './App.css';
56
import Page from './components/Markdown';
6-
import "@wcj/dark-mode"
77

88
const App: React.FC = () => {
99
return (

website/components/Markdown/index.tsx

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,90 @@
11
import MarkdownPreview from '@uiw/react-markdown-preview';
2-
import styles from './index.module.less';
3-
import data from "../../../README.md"
4-
import { useEffect, useRef } from "react"
2+
import { Root, Element, RootContent } from 'hast';
53
import CodeLayout from 'react-code-preview-layout';
64
import { getMetaId, isMeta, getURLParameters } from 'markdown-react-code-preview-loader';
7-
import { CodeComponent, ReactMarkdownNames } from 'react-markdown/lib/ast-to-react';
5+
import styles from './index.module.less';
6+
import data from "../../../README.md"
87
import { getToolbarExtra } from "./Code"
9-
const CodePreview: CodeComponent | ReactMarkdownNames = ({ inline, node, ...props }) => {
10-
const $dom = useRef<HTMLDivElement>(null);
11-
const { 'data-meta': meta, ...rest } = props as any;
128

13-
useEffect(() => {
14-
if ($dom.current) {
15-
const parentElement = $dom.current.parentElement;
16-
if (parentElement && parentElement.parentElement) {
17-
parentElement.parentElement.replaceChild($dom.current, parentElement);
18-
}
19-
}
20-
}, [$dom]);
21-
22-
if (inline || !isMeta(meta)) {
23-
return <code {...props} />;
24-
}
25-
26-
const line = node.position?.start.line;
27-
const metaId = getMetaId(meta) || String(line);
28-
const Child = data.components[`${metaId}`];
29-
if (metaId && typeof Child === 'function') {
30-
const code = data.data[metaId].value || '';
31-
const param = getURLParameters(meta);
32-
return (
33-
<CodeLayout
34-
ref={$dom}
35-
toolbarExtra={getToolbarExtra(code, param.codePen === "true", param.codeSandbox === "true")}
36-
toolbar={param.title || 'Example'} background={"transparent"} code={<pre {...rest} />} text={code}>
37-
<Child />
38-
</CodeLayout>
39-
);
40-
}
41-
return <code {...rest} />;
42-
};
9+
const Preview = CodeLayout.Preview;
10+
const Code = CodeLayout.Code;
11+
const Toolbar = CodeLayout.Toolbar;
4312

4413
export default function Markdown() {
4514
return (
4615
<MarkdownPreview
4716
style={{ padding: '20px 26px' }}
4817
source={data.source}
4918
className={styles.markdown}
19+
rehypeRewrite={(
20+
node: Root | RootContent,
21+
index: number,
22+
parent: Root | Element,
23+
) => {
24+
if (node.type === 'element' && parent && parent.type === 'root') {
25+
const menu = parent.children[1] as Element | undefined;
26+
let childLength = [...parent.children].filter(
27+
(item) => item.type !== 'raw',
28+
).length;
29+
const lastChild = parent.children[parent.children.length - 1];
30+
if (lastChild?.type === 'raw') {
31+
childLength = parent.children.length - 2;
32+
}
33+
if (
34+
(index + 1 === childLength ||
35+
index - 1 === childLength ||
36+
index === childLength) &&
37+
menu?.properties?.class !== 'menu-toc'
38+
) {
39+
const child = [...parent.children].map((item) => {
40+
if (item.type === 'element' && item.tagName === 'pre') {
41+
const meta = item.children[0]?.data?.meta as string;
42+
if (isMeta(meta)) {
43+
item.tagName = 'div';
44+
item.properties = {
45+
...item.properties,
46+
'data-md': meta,
47+
'data-meta': 'preview',
48+
};
49+
return { ...item };
50+
}
51+
}
52+
return item;
53+
});
54+
parent.children = [
55+
{
56+
type: 'element',
57+
tagName: 'div',
58+
children: child as Element[],
59+
},
60+
];
61+
}
62+
}
63+
64+
}}
5065
components={{
51-
code: CodePreview
66+
div: ({ node, ...props }) => {
67+
const { 'data-meta': meta, 'data-md': metaData, ...rest } = props as any;
68+
const line = node.position?.start.line;
69+
const metaId = getMetaId(metaData) || String(line);
70+
const Child = data.components[metaId];
71+
if (meta !== 'preview' || !metaId || typeof Child !== 'function')
72+
return <div {...props} />;
73+
const code = data.data[metaId].value || '';
74+
const param = getURLParameters(meta);
75+
const extra = getToolbarExtra(code, param.codePen === "true", param.codeSandbox === "true");
76+
return (
77+
<CodeLayout disableCheckered style={{ marginBottom: 18 }}>
78+
<Preview>
79+
<Child />
80+
</Preview>
81+
<Toolbar extra={extra} text={code}>{param.title || 'Example'}</Toolbar>
82+
<Code style={{ padding: 0 }}>
83+
<pre {...rest} />
84+
</Code>
85+
</CodeLayout>
86+
);
87+
},
5288
}}
5389
/>
5490
);

website/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react';
21
import App from './App';
32
import { createRoot } from 'react-dom/client';
43

website/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../tsconfig",
3-
"include": ["../src"],
3+
"include": ["./"],
44
"compilerOptions": {
55
"baseUrl": ".",
66
"emitDeclarationOnly": true,

0 commit comments

Comments
 (0)