The Mermaid extension adds a diagram block. The block shows only the diagram. Hover it to copy the Mermaid source, edit it, or delete the block, or double-click it to edit.
- Pick Diagram from the slash menu to insert a diagram. It opens in the editor with an example as the placeholder.
- The editor shows the source next to a live preview. Switch between code, split and preview layouts from the header.
- Click Apply or press Cmd/Ctrl+Enter to save, or Cancel / Esc to discard. Cancelling a new, empty diagram removes it.
- The preview re-renders 300ms after you stop typing, and follows the
darkclass on<html>for light and dark mode. - Syntax errors show above the last diagram that rendered, so a typo doesn't blank the block.
- Mermaid is loaded with a dynamic import the first time a diagram renders, so it stays out of your initial bundle.
- Mermaid runs with
securityLevel: "strict", so diagram labels can't inject scripts or HTML into the page.
The Code Block extension also previews Mermaid: set a code block's language to Mermaid and the diagram renders below the code. Use the code block when you want the source visible and edited as normal document text. Use this extension when you want the diagram as its own block, with the source tucked behind an editor.
Installation
$ pnpm dlx shadcn@latest add "https://www.rtecn.space/r/styles/base-nova/mermaid.json"
This installs extensions/mermaid, extensions/core/, extensions/ui/, and the mermaid package.
To install the files manually instead, install these packages and copy the mermaid, core and ui folders from the package source into extensions/:
$ pnpm add @tiptap/core @tiptap/react @tiptap/pm mermaid lucide-react clsx tailwind-merge
Usage
Add Mermaid to the extensions and MermaidToolbar to the toolbar. The toolbar button inserts a new diagram and opens its editor. Its tooltip reads Diagram; pass label to change it. The copy, edit and delete actions come from a built-in node view.
"use client";
import { useEditor } from "@tiptap/react";
import { StarterKit } from "@tiptap/starter-kit";
import { RichTextEditor } from "@/components/editor";
import { Mermaid } from "@/components/extensions/mermaid";
import { MermaidToolbar } from "@/components/extensions/mermaid/toolbar";
import "@/components/editor/style.css";
export function MyEditor() {
const editor = useEditor({
immediatelyRender: false,
extensions: [StarterKit, Mermaid],
});
return (
<RichTextEditor editor={editor}>
<RichTextEditor.Toolbar>
<RichTextEditor.ControlsGroup>
<RichTextEditor.Bold />
<RichTextEditor.Italic />
<MermaidToolbar editor={editor} />
</RichTextEditor.ControlsGroup>
</RichTextEditor.Toolbar>
<RichTextEditor.Content />
</RichTextEditor>
);
}With @editorcn/block-editor
Add a Diagram item to the slash commands:
"use client";
import { useEditor } from "@tiptap/react";
import { StarterKit } from "@tiptap/starter-kit";
import {
BlockEditor,
SlashCommand,
getSlashCommandSuggestion,
} from "@/components/block-editor";
import {
Mermaid,
mermaidSlashCommandItem,
} from "@/components/extensions/mermaid";
import "@/components/block-editor/style.css";
export function MyBlockEditor() {
const editor = useEditor({
immediatelyRender: false,
extensions: [
StarterKit,
Mermaid,
SlashCommand.configure({
suggestion: getSlashCommandSuggestion([mermaidSlashCommandItem]),
}),
],
});
return <BlockEditor editor={editor} />;
}Commands
editor.commands.setMermaid();
editor.commands.setMermaid({ value: "graph TD\n A[Start] --> B[End]" });| Command | Description |
|---|---|
setMermaid | Inserts a diagram. Pass value to set the source, or leave it out for an empty one. |
Settings
HTMLAttributes
HTML attributes added to the diagram wrapper.
Mermaid.configure({ HTMLAttributes: { class: "my-diagram" } });Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
value | string | null | null | The Mermaid source. |
The source is stored as a node attribute, not as editable text. Each Apply is one change, so a single undo reverts it. In a collaborative document, two people editing the same diagram at once don't merge: the last apply wins.
Output
The source is kept inside a code element, so HTML read without this extension still shows it as a code block.
<div data-type="mermaid"><pre><code class="language-mermaid">graph TD
A[Start] --> B[End]</code></pre></div>{
"type": "mermaid",
"attrs": { "value": "graph TD\n A[Start] --> B[End]" }
}With the Markdown extension, diagrams round-trip as fenced mermaid blocks. If the source contains backticks, the fence gets longer so it can't close early:
```mermaid
graph TD
A[Start] --> B[End]
```If the Code Block extension is also installed, mermaid fences become diagram blocks and every other fence stays a code block.
Static Renderer
The Static Renderer styles div[data-type="mermaid"] as a card with the source inside it, so the output still works in server components and without JavaScript. Mermaid draws in the browser, so the diagram comes from a client component, which shows the source until Mermaid has loaded and keeps it if the source has a syntax error.
HTML
Wrap the renderer in MermaidDiagrams. It finds the diagrams in the rendered HTML and draws them, and redraws when the content or the theme changes:
import { StaticRenderer } from "@/components/static-renderer";
import { MermaidDiagrams } from "@/components/extensions/mermaid";
<MermaidDiagrams>
<StaticRenderer content={html} />
</MermaidDiagrams>;JSON
Map the node to MermaidPreview:
import { StaticRenderer } from "@/components/static-renderer";
import { Mermaid, MermaidPreview } from "@/components/extensions/mermaid";
<StaticRenderer
content={json}
extensions={[StarterKit, Mermaid]}
options={{
nodeMapping: {
mermaid: ({ node }) => (
<div data-type="mermaid">
<MermaidPreview value={node.attrs.value} />
</div>
),
},
}}
/>;MermaidDiagrams also works around JSON output, so you can skip the nodeMapping and wrap the renderer instead.
Ready-made editor apps built on editorcn: a chat composer, a document editor, a blog post editor, and a Notion-style notes workspace. Install them with the shadcn CLI.