This skill should be used when the user asks to "create a diagram", "draw a flowchart", "generate a sequence diagram", "make a state diagram", "create a class diagram", "draw an ER diagram", "update...
Create, edit, and validate Mermaid diagrams using the mmdc command-line tool. This skill ensures diagrams are syntactically correct and visually well-organized before finalizing.
mmdc to check syntax before considering a diagram completeGenerate a unique session ID and create the working directory:
mkdir -p /tmp/claude/[uid]
Replace [uid] with a short unique identifier (e.g., timestamp or random string like mmd-20241219-abc123).
Write the mermaid code to a temporary file:
/tmp/claude/[uid]/diagram.mmd
`
Run the Mermaid CLI to validate syntax and generate output:
mmdc -i /tmp/claude/[uid]/diagram.mmd -o /tmp/claude/[uid]/diagram.svg -w 4096
If syntax errors occur, analyze the error message, fix the mermaid code, and retry.
Use the Read tool to examine the generated SVG file:
Read /tmp/claude/[uid]/diagram.svg
Evaluate the diagram for:
If the layout is chaotic or unclear, refactor the diagram:
Repeat Steps 2-4 until the diagram is well-organized.
Request permission to open the SVG for user inspection:
open /tmp/claude/[uid]/diagram.svg
Wait for user confirmation or feedback. If changes are requested, iterate on the diagram.
Once approved, copy the final mermaid code to the target location (markdown file or standalone .mmd file).
Read the file containing the mermaid diagram. For markdown files, extract the content within the mermaid code fence:
```mermaid
... diagram code ...
```
Generate a unique session ID and create the working directory:
mkdir -p /tmp/claude/[uid]
Write the extracted mermaid code to /tmp/claude/[uid]/diagram.mmd.
Follow Steps 3-6 from "Creating New Diagrams" to validate, generate SVG, inspect, and refine the layout.
Once approved, update the original source file with the improved diagram code.
| Direction | Use When |
|---|---|
| TB (top-bottom) | Hierarchies, org charts, decision trees |
| LR (left-right) | Processes, timelines, data flows |
| BT (bottom-top) | Dependency graphs, build systems |
Group related nodes to improve organization:
flowchart LR
subgraph Frontend
A[React App]
B[Components]
end
subgraph Backend
C[API Server]
D[Database]
end
A --> C
C --> D
Declare nodes in the order they should appear. The declaration order influences layout positioning.
Add invisible links to control spacing (use sparingly):
flowchart LR
A --> B
B ~~~ C
C --> D
| Type | Declaration | Use Case |
|---|---|---|
| Flowchart | flowchart TB |
Processes, workflows, decisions |
| Sequence | sequenceDiagram |
API calls, interactions, messaging |
| State | stateDiagram-v2 |
State machines, lifecycles |
| Class | classDiagram |
OOP structures, relationships |
| ER | erDiagram |
Database schemas |
| Git | gitGraph |
Branch histories |
| Pie | pie |
Data distributions |
| Mindmap | mindmap |
Brainstorming, hierarchies |
Basic usage:
mmdc -i input.mmd -o output.svg -w 4096
Common options:
| Option | Description |
|---|---|
-i <file> |
Input mermaid file |
-o <file> |
Output file (svg, png, pdf) |
-b <color> |
Background color (e.g., white, transparent) |
-t <theme> |
Theme: default, forest, dark, neutral |
-w <width> |
Page width in pixels |
-H <height> |
Page height in pixels |
For diagrams showing before/after states (common in PRs and issues), use color styling:
flowchart LR
A[Component A] --> B[Old Component]
A --> C[New Component]
C --> D[New Dependency]
style B fill:#ffcccc,stroke:#ff0000
style C fill:#ccffcc,stroke:#00ff00
style D fill:#ccffcc,stroke:#00ff00
Legend:
fill:#ffcccc,stroke:#ff0000) = Removedfill:#ccffcc,stroke:#00ff00) = Added