rspress-plugin-mermaid 0.1.0
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/LICENSE +21 -0
- package/README.md +61 -0
- package/dist/components/MermaidRender.d.ts +8 -0
- package/dist/components/MermaidRender.js +32 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/doc_build/static/search_index.e342ad1c.json +1 -0
- package/docs/index.md +10 -0
- package/image.png +0 -0
- package/package.json +42 -0
- package/rspress.config.ts +9 -0
- package/src/components/MermaidRender.tsx +55 -0
- package/src/index.ts +36 -0
- package/tsconfig.json +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Linbudu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# rspress-plugin-mermaid
|
|
2
|
+
|
|
3
|
+
Rspress plugin to render mermaid diagrams in markdown files.
|
|
4
|
+
|
|
5
|
+
Write mermaid as code blocks in markdown files and they will be rendered as SVGs:
|
|
6
|
+
|
|
7
|
+
````markdown
|
|
8
|
+
```mermaid
|
|
9
|
+
flowchart TD
|
|
10
|
+
A[Christmas] -->|Get money| B(Go shopping)
|
|
11
|
+
B --> C{Let me think}
|
|
12
|
+
C -->|One| D[Laptop]
|
|
13
|
+
C -->|Two| E[iPhone]
|
|
14
|
+
C -->|Three| F[fa:fa-car Car]
|
|
15
|
+
```
|
|
16
|
+
````
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<img src="./image.png" alt="sample" width="400" height="550" />
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm i rspress-plugin-mermaid
|
|
26
|
+
pnpm add rspress-plugin-mermaid
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import * as path from 'path';
|
|
31
|
+
import { defineConfig } from 'rspress/config';
|
|
32
|
+
import mermaid from 'rspress-plugin-mermaid';
|
|
33
|
+
|
|
34
|
+
export default defineConfig({
|
|
35
|
+
root: path.join(__dirname, 'docs'),
|
|
36
|
+
plugins: [mermaid()],
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Configure
|
|
41
|
+
|
|
42
|
+
### mermaidConfig
|
|
43
|
+
|
|
44
|
+
Mermaid configuration options, will be passed to `mermaid.initialize` function. See [mermaid documentation](https://mermaid.js.org/config/schema-docs/config.html) for more details.
|
|
45
|
+
|
|
46
|
+
- Type: `object`
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import * as path from 'path';
|
|
50
|
+
import { defineConfig } from 'rspress/config';
|
|
51
|
+
import mermaid from 'rspress-plugin-mermaid';
|
|
52
|
+
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
root: path.join(__dirname, 'docs'),
|
|
55
|
+
plugins: [mermaid({
|
|
56
|
+
mermaidConfig: {
|
|
57
|
+
theme: 'forest',
|
|
58
|
+
},
|
|
59
|
+
})],
|
|
60
|
+
});
|
|
61
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useState } from 'react';
|
|
3
|
+
import mermaid from 'mermaid';
|
|
4
|
+
const MermaidRenderer = (props) => {
|
|
5
|
+
const { code, config = {} } = props;
|
|
6
|
+
const id = useId();
|
|
7
|
+
const [svg, setSvg] = useState('');
|
|
8
|
+
const [renderError, setRenderError] = useState(false);
|
|
9
|
+
async function renderMermaid2SVG() {
|
|
10
|
+
// https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53
|
|
11
|
+
const hasDarkClass = document.documentElement.classList.contains('dark');
|
|
12
|
+
const mermaidConfig = {
|
|
13
|
+
securityLevel: 'loose',
|
|
14
|
+
startOnLoad: false,
|
|
15
|
+
theme: hasDarkClass ? 'dark' : 'default',
|
|
16
|
+
...config,
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
mermaid.initialize(mermaidConfig);
|
|
20
|
+
const { svg } = await mermaid.render(id.replace(/:/g, ''), code);
|
|
21
|
+
setSvg(svg);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
setRenderError(true);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
renderMermaid2SVG();
|
|
29
|
+
}, [code]);
|
|
30
|
+
return (_jsx(_Fragment, { children: renderError ? null : _jsx("div", { dangerouslySetInnerHTML: { __html: svg } }) }));
|
|
31
|
+
};
|
|
32
|
+
export default MermaidRenderer;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RspressPlugin } from '@rspress/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,23 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { CodeBlock2GlobalComponentPluginFactory } from 'rspress-plugin-devkit';
|
|
3
|
+
export default function rspressPluginMermaid(options = {}) {
|
|
4
|
+
const { mermaidConfig = {} } = options;
|
|
5
|
+
return new CodeBlock2GlobalComponentPluginFactory({
|
|
6
|
+
name: 'rspress-plugin-mermaid',
|
|
7
|
+
transformers: [
|
|
8
|
+
{
|
|
9
|
+
lang: 'mermaid',
|
|
10
|
+
componentPath: path.join(__dirname, './components/MermaidRender.tsx'),
|
|
11
|
+
childrenProvider() {
|
|
12
|
+
return [];
|
|
13
|
+
},
|
|
14
|
+
propsProvider(code) {
|
|
15
|
+
return {
|
|
16
|
+
code,
|
|
17
|
+
config: mermaidConfig,
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
}).instantiate();
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[{"id":0,"title":"RSPress x Mermaid","content":"#\n\n","routePath":"/","lang":"","toc":[],"domain":"","frontmatter":{},"version":""}]
|
package/docs/index.md
ADDED
package/image.png
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rspress-plugin-mermaid",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rspress plugin to render mermaid diagrams",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rspress",
|
|
7
|
+
"plugin",
|
|
8
|
+
"mermaid"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/linbudu599/rspress-plugins/tree/main/packages/rspress-plugin-mermaid#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/linbudu599/rspress-plugins/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/linbudu599/rspress-plugins.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Linbudu <linbudu599@gmail.com> (https://github.com/linbudu599)",
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@rspress/shared": "^1.16.2",
|
|
24
|
+
"mermaid": "^10.9.0",
|
|
25
|
+
"rspress-plugin-devkit": "^0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.12.2",
|
|
29
|
+
"@types/react": "^18.2.73",
|
|
30
|
+
"typescript": "^5"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"rspress": "*"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc --declarationMap false",
|
|
37
|
+
"dev": "tsc -w",
|
|
38
|
+
"docs:build": "rspress build",
|
|
39
|
+
"docs:dev": "rspress dev",
|
|
40
|
+
"prepublish": "npm run build"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React, { useEffect, useId, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import mermaid, { type MermaidConfig } from 'mermaid';
|
|
4
|
+
|
|
5
|
+
export interface MermaidRendererProps {
|
|
6
|
+
code: string;
|
|
7
|
+
config?: MermaidConfig;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const MermaidRenderer: React.FC<MermaidRendererProps> = (props) => {
|
|
11
|
+
const { code, config = {} } = props;
|
|
12
|
+
|
|
13
|
+
const id = useId();
|
|
14
|
+
|
|
15
|
+
const [svg, setSvg] = useState('');
|
|
16
|
+
|
|
17
|
+
const [renderError, setRenderError] = useState(false);
|
|
18
|
+
|
|
19
|
+
async function renderMermaid2SVG() {
|
|
20
|
+
// https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53
|
|
21
|
+
const hasDarkClass = document.documentElement.classList.contains('dark');
|
|
22
|
+
|
|
23
|
+
const mermaidConfig: MermaidConfig = {
|
|
24
|
+
securityLevel: 'loose',
|
|
25
|
+
startOnLoad: false,
|
|
26
|
+
theme: hasDarkClass ? 'dark' : 'default',
|
|
27
|
+
...config,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
mermaid.initialize(mermaidConfig);
|
|
32
|
+
|
|
33
|
+
const { svg } = await mermaid.render(
|
|
34
|
+
id.replace(/:/g, ''),
|
|
35
|
+
code as string,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
setSvg(svg);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
setRenderError(true);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
renderMermaid2SVG();
|
|
46
|
+
}, [code]);
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<>
|
|
50
|
+
{renderError ? null : <div dangerouslySetInnerHTML={{ __html: svg }} />}
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default MermaidRenderer;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { CodeBlock2GlobalComponentPluginFactory } from 'rspress-plugin-devkit';
|
|
4
|
+
|
|
5
|
+
import type { RspressPlugin } from '@rspress/shared';
|
|
6
|
+
import type { MermaidConfig } from 'mermaid';
|
|
7
|
+
import type { MermaidRendererProps } from './components/MermaidRender';
|
|
8
|
+
|
|
9
|
+
interface RspressPluginMermaidOptions {
|
|
10
|
+
mermaidConfig?: MermaidConfig;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default function rspressPluginMermaid(
|
|
14
|
+
options: RspressPluginMermaidOptions = {},
|
|
15
|
+
): RspressPlugin {
|
|
16
|
+
const { mermaidConfig = {} } = options;
|
|
17
|
+
|
|
18
|
+
return new CodeBlock2GlobalComponentPluginFactory({
|
|
19
|
+
name: 'rspress-plugin-mermaid',
|
|
20
|
+
transformers: [
|
|
21
|
+
{
|
|
22
|
+
lang: 'mermaid',
|
|
23
|
+
componentPath: path.join(__dirname, './components/MermaidRender.tsx'),
|
|
24
|
+
childrenProvider() {
|
|
25
|
+
return [];
|
|
26
|
+
},
|
|
27
|
+
propsProvider(code) {
|
|
28
|
+
return {
|
|
29
|
+
code,
|
|
30
|
+
config: mermaidConfig,
|
|
31
|
+
} satisfies MermaidRendererProps;
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
}).instantiate();
|
|
36
|
+
}
|