@rspress-theme-anatole/rspress-plugin-mermaid 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/MermaidRender.tsx +52 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +33 -0
- package/dist/typings.d.ts +5 -0
- package/dist/typings.js +1 -0
- package/package.json +32 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React, { useEffect, useId, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import mermaid, { type MermaidConfig } from 'mermaid';
|
|
4
|
+
|
|
5
|
+
import type { MermaidRendererProps } from '../dist/typings';
|
|
6
|
+
|
|
7
|
+
const MermaidRenderer: React.FC<MermaidRendererProps> = (props) => {
|
|
8
|
+
const { code, config = {} } = props;
|
|
9
|
+
|
|
10
|
+
const id = useId();
|
|
11
|
+
|
|
12
|
+
const [svg, setSvg] = useState('');
|
|
13
|
+
|
|
14
|
+
const [renderError, setRenderError] = useState(false);
|
|
15
|
+
|
|
16
|
+
async function renderMermaid2SVG() {
|
|
17
|
+
// https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53
|
|
18
|
+
const hasDarkClass = document.documentElement.classList.contains('dark');
|
|
19
|
+
|
|
20
|
+
const mermaidConfig: MermaidConfig = {
|
|
21
|
+
securityLevel: 'loose',
|
|
22
|
+
startOnLoad: false,
|
|
23
|
+
theme: hasDarkClass ? 'dark' : 'default',
|
|
24
|
+
...config,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
mermaid.initialize(mermaidConfig);
|
|
29
|
+
|
|
30
|
+
const { svg } = await mermaid.render(
|
|
31
|
+
id.replace(/:/g, ''),
|
|
32
|
+
code as string,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
setSvg(svg);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
setRenderError(true);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
renderMermaid2SVG();
|
|
43
|
+
}, [code]);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<>
|
|
47
|
+
{renderError ? null : <div dangerouslySetInnerHTML={{ __html: svg }} />}
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export default MermaidRenderer;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RspressPlugin } from '@rspress-theme-anatole/shared';
|
|
2
|
+
import type { MermaidConfig } from 'mermaid';
|
|
3
|
+
interface RspressPluginMermaidOptions {
|
|
4
|
+
mermaidConfig?: MermaidConfig;
|
|
5
|
+
}
|
|
6
|
+
export default function rspressPluginMermaid(options?: RspressPluginMermaidOptions): RspressPlugin;
|
|
7
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { PresetConfigMutator, RemarkCodeBlockToGlobalComponentPluginFactory, } from '@rspress-theme-anatole/rspress-plugin-devkit';
|
|
3
|
+
export default function rspressPluginMermaid(options = {}) {
|
|
4
|
+
const { mermaidConfig = {} } = options;
|
|
5
|
+
const remarkMermaid = new RemarkCodeBlockToGlobalComponentPluginFactory({
|
|
6
|
+
components: [
|
|
7
|
+
{
|
|
8
|
+
lang: 'mermaid',
|
|
9
|
+
componentPath: path.join(__dirname, '../components', 'MermaidRender.tsx'),
|
|
10
|
+
childrenProvider() {
|
|
11
|
+
return [];
|
|
12
|
+
},
|
|
13
|
+
propsProvider(code) {
|
|
14
|
+
return {
|
|
15
|
+
code,
|
|
16
|
+
config: mermaidConfig,
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
name: '@rspress-theme-anatole/rspress-plugin-mermaid',
|
|
24
|
+
config(config) {
|
|
25
|
+
return new PresetConfigMutator(config).disableMdxRs().toConfig();
|
|
26
|
+
},
|
|
27
|
+
markdown: {
|
|
28
|
+
remarkPlugins: [remarkMermaid.remarkPlugin],
|
|
29
|
+
globalComponents: remarkMermaid.mdxComponents,
|
|
30
|
+
},
|
|
31
|
+
builderConfig: remarkMermaid.builderConfig,
|
|
32
|
+
};
|
|
33
|
+
}
|
package/dist/typings.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rspress-theme-anatole/rspress-plugin-mermaid",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"components"
|
|
7
|
+
],
|
|
8
|
+
"keywords": [
|
|
9
|
+
"rspress",
|
|
10
|
+
"plugin",
|
|
11
|
+
"mermaid"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@rspress-theme-anatole/shared": "0.1.19",
|
|
21
|
+
"mermaid": "^10.9.0",
|
|
22
|
+
"@rspress-theme-anatole/rspress-plugin-devkit": "0.1.19"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^20.12.5",
|
|
26
|
+
"@types/react": "^18.2.74",
|
|
27
|
+
"typescript": "^5.4.4"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"rspress": "*"
|
|
31
|
+
}
|
|
32
|
+
}
|