fumadocs-mermaid 1.0.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 +259 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/remark-mdx-mermaid.d.ts +30 -0
- package/dist/remark-mdx-mermaid.d.ts.map +1 -0
- package/dist/remark-mdx-mermaid.js +43 -0
- package/dist/remark-mdx-mermaid.js.map +1 -0
- package/dist/ui/index.d.ts +2 -0
- package/dist/ui/index.js +5 -0
- package/dist/ui/mermaid.d.ts +32 -0
- package/dist/ui/mermaid.d.ts.map +1 -0
- package/dist/ui/mermaid.js +59 -0
- package/dist/ui/mermaid.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 fumadocs-mermaid contributors
|
|
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,259 @@
|
|
|
1
|
+
# fumadocs-mermaid
|
|
2
|
+
|
|
3
|
+
Mermaid diagram integration for [Fumadocs](https://fumadocs.dev) - a modern documentation framework for React.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎨 Automatic dark/light theme support with `next-themes`
|
|
8
|
+
- ⚡ Client-side rendering with smart caching
|
|
9
|
+
- 🔌 Plug-and-play integration with Fumadocs
|
|
10
|
+
- 📝 Convert markdown code blocks to diagrams automatically
|
|
11
|
+
- 🌳 Tree-shakeable ESM package
|
|
12
|
+
- 💪 Full TypeScript support
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install fumadocs-mermaid mermaid next-themes
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add fumadocs-mermaid mermaid next-themes
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add fumadocs-mermaid mermaid next-themes
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Option 1: Direct Component Usage
|
|
31
|
+
|
|
32
|
+
Import and register the `Mermaid` component in your MDX components:
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
// mdx-components.tsx
|
|
36
|
+
import defaultMdxComponents from 'fumadocs-ui/mdx';
|
|
37
|
+
import { Mermaid } from 'fumadocs-mermaid/ui';
|
|
38
|
+
import type { MDXComponents } from 'mdx/types';
|
|
39
|
+
|
|
40
|
+
export function getMDXComponents(components?: MDXComponents): MDXComponents {
|
|
41
|
+
return {
|
|
42
|
+
...defaultMdxComponents,
|
|
43
|
+
Mermaid,
|
|
44
|
+
...components,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Then use it in your MDX files:
|
|
50
|
+
|
|
51
|
+
```mdx
|
|
52
|
+
<Mermaid
|
|
53
|
+
chart="
|
|
54
|
+
graph TD;
|
|
55
|
+
A[Start] --> B{Decision};
|
|
56
|
+
B -->|Yes| C[Action 1];
|
|
57
|
+
B -->|No| D[Action 2];
|
|
58
|
+
C --> E[End];
|
|
59
|
+
D --> E;
|
|
60
|
+
"
|
|
61
|
+
/>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Option 2: Automatic Code Block Conversion (Recommended)
|
|
65
|
+
|
|
66
|
+
Use the remark plugin to automatically convert mermaid code blocks:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// source.config.ts (Fumadocs MDX)
|
|
70
|
+
import { remarkMdxMermaid } from 'fumadocs-mermaid';
|
|
71
|
+
import { defineConfig } from 'fumadocs-mdx/config';
|
|
72
|
+
|
|
73
|
+
export default defineConfig({
|
|
74
|
+
mdxOptions: {
|
|
75
|
+
remarkPlugins: [remarkMdxMermaid],
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Don't forget to register the component:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
// mdx-components.tsx
|
|
84
|
+
import defaultMdxComponents from 'fumadocs-ui/mdx';
|
|
85
|
+
import { Mermaid } from 'fumadocs-mermaid/ui';
|
|
86
|
+
|
|
87
|
+
export function getMDXComponents(components) {
|
|
88
|
+
return {
|
|
89
|
+
...defaultMdxComponents,
|
|
90
|
+
Mermaid,
|
|
91
|
+
...components,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Now you can write mermaid diagrams using code blocks in your MDX:
|
|
97
|
+
|
|
98
|
+
````mdx
|
|
99
|
+
```mermaid
|
|
100
|
+
graph TD;
|
|
101
|
+
A[Start] --> B{Decision};
|
|
102
|
+
B -->|Yes| C[Action 1];
|
|
103
|
+
B -->|No| D[Action 2];
|
|
104
|
+
```
|
|
105
|
+
````
|
|
106
|
+
|
|
107
|
+
## API Reference
|
|
108
|
+
|
|
109
|
+
### `<Mermaid />`
|
|
110
|
+
|
|
111
|
+
The React component for rendering Mermaid diagrams.
|
|
112
|
+
|
|
113
|
+
**Props:**
|
|
114
|
+
|
|
115
|
+
| Prop | Type | Default | Description |
|
|
116
|
+
|------|------|---------|-------------|
|
|
117
|
+
| `chart` | `string` | **Required** | The mermaid diagram definition |
|
|
118
|
+
| `theme` | `'default' \| 'dark' \| 'neutral' \| 'forest'` | Auto-detected | Theme override. Automatically uses next-themes if available |
|
|
119
|
+
| `themeCSS` | `string` | `'margin: 1.5rem auto 0;'` | Additional CSS for the diagram container |
|
|
120
|
+
|
|
121
|
+
**Example:**
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
<Mermaid
|
|
125
|
+
chart="graph LR; A-->B;"
|
|
126
|
+
theme="dark"
|
|
127
|
+
themeCSS="margin: 2rem auto;"
|
|
128
|
+
/>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### `remarkMdxMermaid()`
|
|
132
|
+
|
|
133
|
+
Remark plugin to convert mermaid code blocks into `<Mermaid />` components.
|
|
134
|
+
|
|
135
|
+
**Options:**
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
interface RemarkMdxMermaidOptions {
|
|
139
|
+
/**
|
|
140
|
+
* The language identifier for mermaid code blocks
|
|
141
|
+
* @defaultValue 'mermaid'
|
|
142
|
+
*/
|
|
143
|
+
lang?: string;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Example with custom language:**
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { remarkMdxMermaid } from 'fumadocs-mermaid';
|
|
151
|
+
|
|
152
|
+
export default defineConfig({
|
|
153
|
+
mdxOptions: {
|
|
154
|
+
remarkPlugins: [[remarkMdxMermaid, { lang: 'diagram' }]],
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Examples
|
|
160
|
+
|
|
161
|
+
### Flowchart
|
|
162
|
+
|
|
163
|
+
````mdx
|
|
164
|
+
```mermaid
|
|
165
|
+
graph TD;
|
|
166
|
+
A[Christmas] -->|Get money| B(Go shopping)
|
|
167
|
+
B --> C{Let me think}
|
|
168
|
+
C -->|One| D[Laptop]
|
|
169
|
+
C -->|Two| E[iPhone]
|
|
170
|
+
C -->|Three| F[Car]
|
|
171
|
+
```
|
|
172
|
+
````
|
|
173
|
+
|
|
174
|
+
### Sequence Diagram
|
|
175
|
+
|
|
176
|
+
````mdx
|
|
177
|
+
```mermaid
|
|
178
|
+
sequenceDiagram
|
|
179
|
+
Alice->>John: Hello John, how are you?
|
|
180
|
+
John-->>Alice: Great!
|
|
181
|
+
Alice-)John: See you later!
|
|
182
|
+
```
|
|
183
|
+
````
|
|
184
|
+
|
|
185
|
+
### Gantt Chart
|
|
186
|
+
|
|
187
|
+
````mdx
|
|
188
|
+
```mermaid
|
|
189
|
+
gantt
|
|
190
|
+
title A Gantt Diagram
|
|
191
|
+
dateFormat YYYY-MM-DD
|
|
192
|
+
section Section
|
|
193
|
+
A task :a1, 2024-01-01, 30d
|
|
194
|
+
Another task :after a1, 20d
|
|
195
|
+
```
|
|
196
|
+
````
|
|
197
|
+
|
|
198
|
+
### Class Diagram
|
|
199
|
+
|
|
200
|
+
````mdx
|
|
201
|
+
```mermaid
|
|
202
|
+
classDiagram
|
|
203
|
+
Animal <|-- Duck
|
|
204
|
+
Animal <|-- Fish
|
|
205
|
+
Animal : +int age
|
|
206
|
+
Animal : +String gender
|
|
207
|
+
Animal: +isMammal()
|
|
208
|
+
class Duck{
|
|
209
|
+
+String beakColor
|
|
210
|
+
+swim()
|
|
211
|
+
}
|
|
212
|
+
class Fish{
|
|
213
|
+
-int sizeInFeet
|
|
214
|
+
-canEat()
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
````
|
|
218
|
+
|
|
219
|
+
## Theme Support
|
|
220
|
+
|
|
221
|
+
The component automatically detects your theme using `next-themes`:
|
|
222
|
+
|
|
223
|
+
- Light mode → Uses Mermaid's `default` theme
|
|
224
|
+
- Dark mode → Uses Mermaid's `dark` theme
|
|
225
|
+
|
|
226
|
+
You can override this behavior with the `theme` prop:
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
<Mermaid chart="graph LR; A-->B;" theme="forest" />
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## TypeScript
|
|
233
|
+
|
|
234
|
+
The package is fully typed. Import types as needed:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import type { MermaidProps, RemarkMdxMermaidOptions } from 'fumadocs-mermaid';
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## How It Works
|
|
241
|
+
|
|
242
|
+
1. **Client-side rendering**: Diagrams are rendered on the client to avoid hydration issues
|
|
243
|
+
2. **Smart caching**: Both the Mermaid library and rendered diagrams are cached for performance
|
|
244
|
+
3. **Theme integration**: Automatically re-renders when theme changes (when using next-themes)
|
|
245
|
+
4. **Remark plugin**: Transforms markdown code blocks at build time into React components
|
|
246
|
+
|
|
247
|
+
## Requirements
|
|
248
|
+
|
|
249
|
+
- React 18+ or 19+
|
|
250
|
+
- Mermaid 10+ or 11+
|
|
251
|
+
- next-themes (optional, for automatic theme detection)
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|
|
256
|
+
|
|
257
|
+
## Credits
|
|
258
|
+
|
|
259
|
+
Inspired by the Mermaid integration examples from the official [Fumadocs documentation](https://fumadocs.dev/docs/markdown/mermaid).
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Transformer } from "unified";
|
|
2
|
+
import { Root } from "mdast";
|
|
3
|
+
|
|
4
|
+
//#region src/remark-mdx-mermaid.d.ts
|
|
5
|
+
interface RemarkMdxMermaidOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The language identifier for mermaid code blocks
|
|
8
|
+
* @defaultValue 'mermaid'
|
|
9
|
+
*/
|
|
10
|
+
lang?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Remark plugin to convert mermaid code blocks into `<Mermaid />` MDX components
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { remarkMdxMermaid } from 'fumadocs-mermaid';
|
|
18
|
+
* import { defineConfig } from 'fumadocs-mdx/config';
|
|
19
|
+
*
|
|
20
|
+
* export default defineConfig({
|
|
21
|
+
* mdxOptions: {
|
|
22
|
+
* remarkPlugins: [remarkMdxMermaid],
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare function remarkMdxMermaid(options?: RemarkMdxMermaidOptions): Transformer<Root, Root>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { RemarkMdxMermaidOptions, remarkMdxMermaid };
|
|
30
|
+
//# sourceMappingURL=remark-mdx-mermaid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remark-mdx-mermaid.d.ts","names":[],"sources":["../src/remark-mdx-mermaid.ts"],"sourcesContent":[],"mappings":";;;;UAgCiB,uBAAA;;AAAjB;AAuBA;;EAAqF,IAAA,CAAA,EAAA,MAAA;;;;;;;;;;;;;;;;;iBAArE,gBAAA,WAA0B,0BAA+B,YAAY,MAAM"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { visit } from "unist-util-visit";
|
|
2
|
+
|
|
3
|
+
//#region src/remark-mdx-mermaid.ts
|
|
4
|
+
function toMDX(code) {
|
|
5
|
+
return {
|
|
6
|
+
type: "mdxJsxFlowElement",
|
|
7
|
+
name: "Mermaid",
|
|
8
|
+
attributes: [{
|
|
9
|
+
type: "mdxJsxAttribute",
|
|
10
|
+
name: "chart",
|
|
11
|
+
value: code.trim()
|
|
12
|
+
}],
|
|
13
|
+
children: []
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Remark plugin to convert mermaid code blocks into `<Mermaid />` MDX components
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { remarkMdxMermaid } from 'fumadocs-mermaid';
|
|
22
|
+
* import { defineConfig } from 'fumadocs-mdx/config';
|
|
23
|
+
*
|
|
24
|
+
* export default defineConfig({
|
|
25
|
+
* mdxOptions: {
|
|
26
|
+
* remarkPlugins: [remarkMdxMermaid],
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function remarkMdxMermaid(options = {}) {
|
|
32
|
+
const { lang = "mermaid" } = options;
|
|
33
|
+
return (tree) => {
|
|
34
|
+
visit(tree, "code", (node) => {
|
|
35
|
+
if (node.lang !== lang || !node.value) return;
|
|
36
|
+
Object.assign(node, toMDX(node.value));
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { remarkMdxMermaid };
|
|
43
|
+
//# sourceMappingURL=remark-mdx-mermaid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remark-mdx-mermaid.js","names":[],"sources":["../src/remark-mdx-mermaid.ts"],"sourcesContent":["import { visit } from 'unist-util-visit';\nimport type { Transformer } from 'unified';\nimport type { Root } from 'mdast';\n\ninterface MdxJsxAttribute {\n type: 'mdxJsxAttribute';\n name: string;\n value: string;\n}\n\ninterface MdxJsxFlowElement {\n type: 'mdxJsxFlowElement';\n name: string;\n attributes: MdxJsxAttribute[];\n children: unknown[];\n}\n\nfunction toMDX(code: string): MdxJsxFlowElement {\n return {\n type: 'mdxJsxFlowElement',\n name: 'Mermaid',\n attributes: [\n {\n type: 'mdxJsxAttribute',\n name: 'chart',\n value: code.trim(),\n },\n ],\n children: [],\n };\n}\n\nexport interface RemarkMdxMermaidOptions {\n /**\n * The language identifier for mermaid code blocks\n * @defaultValue 'mermaid'\n */\n lang?: string;\n}\n\n/**\n * Remark plugin to convert mermaid code blocks into `<Mermaid />` MDX components\n *\n * @example\n * ```ts\n * import { remarkMdxMermaid } from 'fumadocs-mermaid';\n * import { defineConfig } from 'fumadocs-mdx/config';\n *\n * export default defineConfig({\n * mdxOptions: {\n * remarkPlugins: [remarkMdxMermaid],\n * },\n * });\n * ```\n */\nexport function remarkMdxMermaid(options: RemarkMdxMermaidOptions = {}): Transformer<Root, Root> {\n const { lang = 'mermaid' } = options;\n\n return (tree: Root) => {\n visit(tree, 'code', (node) => {\n if (node.lang !== lang || !node.value) return;\n\n Object.assign(node, toMDX(node.value));\n });\n };\n}\n"],"mappings":";;;AAiBA,SAAS,MAAM,MAAiC;AAC9C,QAAO;EACL,MAAM;EACN,MAAM;EACN,YAAY,CACV;GACE,MAAM;GACN,MAAM;GACN,OAAO,KAAK,MAAM;GACnB,CACF;EACD,UAAU,EAAE;EACb;;;;;;;;;;;;;;;;;AA0BH,SAAgB,iBAAiB,UAAmC,EAAE,EAA2B;CAC/F,MAAM,EAAE,OAAO,cAAc;AAE7B,SAAQ,SAAe;AACrB,QAAM,MAAM,SAAS,SAAS;AAC5B,OAAI,KAAK,SAAS,QAAQ,CAAC,KAAK,MAAO;AAEvC,UAAO,OAAO,MAAM,MAAM,KAAK,MAAM,CAAC;IACtC"}
|
package/dist/ui/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
//#region src/ui/mermaid.d.ts
|
|
4
|
+
interface MermaidProps {
|
|
5
|
+
/**
|
|
6
|
+
* The mermaid diagram chart definition
|
|
7
|
+
*/
|
|
8
|
+
chart: string;
|
|
9
|
+
/**
|
|
10
|
+
* Optional theme override. If not provided, will use the theme from next-themes context.
|
|
11
|
+
* @defaultValue 'default' for light, 'dark' for dark mode
|
|
12
|
+
*/
|
|
13
|
+
theme?: 'default' | 'dark' | 'neutral' | 'forest';
|
|
14
|
+
/**
|
|
15
|
+
* Additional CSS for the diagram container
|
|
16
|
+
*/
|
|
17
|
+
themeCSS?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Mermaid diagram component with theme support
|
|
21
|
+
*
|
|
22
|
+
* Automatically detects dark/light mode when used with next-themes.
|
|
23
|
+
* Renders on client-side only to avoid hydration issues.
|
|
24
|
+
*/
|
|
25
|
+
declare function Mermaid({
|
|
26
|
+
chart,
|
|
27
|
+
theme: themeOverride,
|
|
28
|
+
themeCSS
|
|
29
|
+
}: MermaidProps): react_jsx_runtime0.JSX.Element | null;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Mermaid, MermaidProps };
|
|
32
|
+
//# sourceMappingURL=mermaid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mermaid.d.ts","names":[],"sources":["../../src/ui/mermaid.tsx"],"sourcesContent":[],"mappings":";;;UAKiB,YAAA;;;AAAjB;EAwBgB,KAAA,EAAA,MAAO;EAAG;;;;EAAgF,KAAA,CAAA,EAAA,SAAA,GAAA,MAAA,GAAA,SAAA,GAAA,QAAA;EAAA;;;;;;;;;;;iBAA1F,OAAA;;SAAwB;;GAAsD,eAAY,kBAAA,CAAA,GAAA,CAAA,OAAA"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { use, useEffect, useId, useState } from "react";
|
|
4
|
+
import { useTheme } from "next-themes";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
|
|
7
|
+
//#region src/ui/mermaid.tsx
|
|
8
|
+
/**
|
|
9
|
+
* Mermaid diagram component with theme support
|
|
10
|
+
*
|
|
11
|
+
* Automatically detects dark/light mode when used with next-themes.
|
|
12
|
+
* Renders on client-side only to avoid hydration issues.
|
|
13
|
+
*/
|
|
14
|
+
function Mermaid({ chart, theme: themeOverride, themeCSS = "margin: 1.5rem auto 0;" }) {
|
|
15
|
+
const [mounted, setMounted] = useState(false);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
setMounted(true);
|
|
18
|
+
}, []);
|
|
19
|
+
if (!mounted) return null;
|
|
20
|
+
return /* @__PURE__ */ jsx(MermaidContent, {
|
|
21
|
+
chart,
|
|
22
|
+
themeOverride,
|
|
23
|
+
themeCSS
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
const cache = /* @__PURE__ */ new Map();
|
|
27
|
+
function cachePromise(key, setPromise) {
|
|
28
|
+
const cached = cache.get(key);
|
|
29
|
+
if (cached) return cached;
|
|
30
|
+
const promise = setPromise();
|
|
31
|
+
cache.set(key, promise);
|
|
32
|
+
return promise;
|
|
33
|
+
}
|
|
34
|
+
function MermaidContent({ chart, themeOverride, themeCSS }) {
|
|
35
|
+
const id = useId();
|
|
36
|
+
const { resolvedTheme: systemTheme } = useTheme();
|
|
37
|
+
const resolvedTheme = themeOverride ?? (systemTheme === "dark" ? "dark" : "default");
|
|
38
|
+
const { default: mermaid } = use(cachePromise("mermaid", () => import("mermaid")));
|
|
39
|
+
mermaid.initialize({
|
|
40
|
+
startOnLoad: false,
|
|
41
|
+
securityLevel: "loose",
|
|
42
|
+
fontFamily: "inherit",
|
|
43
|
+
themeCSS,
|
|
44
|
+
theme: resolvedTheme
|
|
45
|
+
});
|
|
46
|
+
const { svg, bindFunctions } = use(cachePromise(`${chart}-${resolvedTheme}`, () => {
|
|
47
|
+
return mermaid.render(id, chart.replaceAll("\\n", "\n"));
|
|
48
|
+
}));
|
|
49
|
+
return /* @__PURE__ */ jsx("div", {
|
|
50
|
+
ref: (container) => {
|
|
51
|
+
if (container) bindFunctions?.(container);
|
|
52
|
+
},
|
|
53
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { Mermaid };
|
|
59
|
+
//# sourceMappingURL=mermaid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mermaid.js","names":[],"sources":["../../src/ui/mermaid.tsx"],"sourcesContent":["'use client';\n\nimport { use, useEffect, useId, useState } from 'react';\nimport { useTheme } from 'next-themes';\n\nexport interface MermaidProps {\n /**\n * The mermaid diagram chart definition\n */\n chart: string;\n\n /**\n * Optional theme override. If not provided, will use the theme from next-themes context.\n * @defaultValue 'default' for light, 'dark' for dark mode\n */\n theme?: 'default' | 'dark' | 'neutral' | 'forest';\n\n /**\n * Additional CSS for the diagram container\n */\n themeCSS?: string;\n}\n\n/**\n * Mermaid diagram component with theme support\n *\n * Automatically detects dark/light mode when used with next-themes.\n * Renders on client-side only to avoid hydration issues.\n */\nexport function Mermaid({ chart, theme: themeOverride, themeCSS = 'margin: 1.5rem auto 0;' }: MermaidProps) {\n const [mounted, setMounted] = useState(false);\n\n useEffect(() => {\n setMounted(true);\n }, []);\n\n if (!mounted) return null;\n\n return <MermaidContent chart={chart} themeOverride={themeOverride} themeCSS={themeCSS} />;\n}\n\n// Cache for mermaid library and rendered diagrams\nconst cache = new Map<string, Promise<unknown>>();\n\nfunction cachePromise<T>(key: string, setPromise: () => Promise<T>): Promise<T> {\n const cached = cache.get(key);\n if (cached) return cached as Promise<T>;\n\n const promise = setPromise();\n cache.set(key, promise);\n return promise;\n}\n\ninterface MermaidContentProps {\n chart: string;\n themeOverride?: string;\n themeCSS: string;\n}\n\nfunction MermaidContent({ chart, themeOverride, themeCSS }: MermaidContentProps) {\n const id = useId();\n const { resolvedTheme: systemTheme } = useTheme();\n\n // Use override if provided, otherwise use system theme, fallback to 'default'\n const resolvedTheme = themeOverride ?? (systemTheme === 'dark' ? 'dark' : 'default');\n\n const { default: mermaid } = use(\n cachePromise('mermaid', () => import('mermaid'))\n );\n\n mermaid.initialize({\n startOnLoad: false,\n securityLevel: 'loose',\n fontFamily: 'inherit',\n themeCSS,\n theme: resolvedTheme as any,\n });\n\n const { svg, bindFunctions } = use(\n cachePromise(`${chart}-${resolvedTheme}`, () => {\n return mermaid.render(id, chart.replaceAll('\\\\n', '\\n'));\n })\n );\n\n return (\n <div\n ref={(container) => {\n if (container) bindFunctions?.(container);\n }}\n dangerouslySetInnerHTML={{ __html: svg }}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;;;AA6BA,SAAgB,QAAQ,EAAE,OAAO,OAAO,eAAe,WAAW,4BAA0C;CAC1G,MAAM,CAAC,SAAS,cAAc,SAAS,MAAM;AAE7C,iBAAgB;AACd,aAAW,KAAK;IACf,EAAE,CAAC;AAEN,KAAI,CAAC,QAAS,QAAO;AAErB,QAAO,oBAAC;EAAsB;EAAsB;EAAyB;GAAY;;AAI3F,MAAM,wBAAQ,IAAI,KAA+B;AAEjD,SAAS,aAAgB,KAAa,YAA0C;CAC9E,MAAM,SAAS,MAAM,IAAI,IAAI;AAC7B,KAAI,OAAQ,QAAO;CAEnB,MAAM,UAAU,YAAY;AAC5B,OAAM,IAAI,KAAK,QAAQ;AACvB,QAAO;;AAST,SAAS,eAAe,EAAE,OAAO,eAAe,YAAiC;CAC/E,MAAM,KAAK,OAAO;CAClB,MAAM,EAAE,eAAe,gBAAgB,UAAU;CAGjD,MAAM,gBAAgB,kBAAkB,gBAAgB,SAAS,SAAS;CAE1E,MAAM,EAAE,SAAS,YAAY,IAC3B,aAAa,iBAAiB,OAAO,WAAW,CACjD;AAED,SAAQ,WAAW;EACjB,aAAa;EACb,eAAe;EACf,YAAY;EACZ;EACA,OAAO;EACR,CAAC;CAEF,MAAM,EAAE,KAAK,kBAAkB,IAC7B,aAAa,GAAG,MAAM,GAAG,uBAAuB;AAC9C,SAAO,QAAQ,OAAO,IAAI,MAAM,WAAW,OAAO,KAAK,CAAC;GACxD,CACH;AAED,QACE,oBAAC;EACC,MAAM,cAAc;AAClB,OAAI,UAAW,iBAAgB,UAAU;;EAE3C,yBAAyB,EAAE,QAAQ,KAAK;GACxC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fumadocs-mermaid",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Mermaid diagram integration for Fumadocs",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"fumadocs",
|
|
7
|
+
"mermaid",
|
|
8
|
+
"diagrams",
|
|
9
|
+
"documentation",
|
|
10
|
+
"mdx"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/sebastianhuus/fumadocs-mermaid",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Sebastian Huus",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/sebastianhuus/fumadocs-mermaid.git"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./ui": {
|
|
31
|
+
"types": "./dist/ui/index.d.ts",
|
|
32
|
+
"import": "./dist/ui/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"dev": "tsdown --watch",
|
|
38
|
+
"clean": "rimraf dist",
|
|
39
|
+
"types:check": "tsc --noEmit",
|
|
40
|
+
"prepare": "tsdown"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"unist-util-visit": "^5.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/mdast": "^4.0.4",
|
|
47
|
+
"@types/node": "^22.0.0",
|
|
48
|
+
"@types/react": "^19.0.0",
|
|
49
|
+
"mermaid": "^11.0.0",
|
|
50
|
+
"next-themes": "^0.4.0",
|
|
51
|
+
"react": "^19.0.0",
|
|
52
|
+
"rimraf": "^6.0.0",
|
|
53
|
+
"tsdown": "^0.18.0",
|
|
54
|
+
"typescript": "^5.9.0",
|
|
55
|
+
"unified": "^11.0.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"mermaid": "^10.0.0 || ^11.0.0",
|
|
59
|
+
"next-themes": "*",
|
|
60
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
61
|
+
}
|
|
62
|
+
}
|