Mermaid Diagram Generator
Overview
Generate Mermaid diagram code from natural language descriptions and validate syntax before outputting. Mermaid is a JavaScript-based diagramming tool that renders diagrams from text definitions.
When to Use
- User says "create a flowchart", "draw a diagram", "make a mind map"
- User wants "sequence diagram", "class diagram", "ER diagram", "state diagram"
- User mentions "Mermaid" directly
- User wants to visualize processes, architectures, or relationships
- User needs syntax-validated diagram code
Supported Diagram Types
| Type |
Keyword triggers |
| Flowchart |
flowchart, flow chart, 流程图 |
| Sequence Diagram |
sequence, 时序图, 顺序图 |
| Class Diagram |
class diagram, 类图 |
| State Diagram |
state diagram, 状态图 |
| ER Diagram |
ER diagram, er图, 实体关系图 |
| Gantt Chart |
gantt, 甘特图 |
| Pie Chart |
pie chart, 饼图 |
| Mind Map |
mind map, 思维导图 |
| Journey |
user journey, 用户旅程 |
| Git Graph |
git graph, git graph |
Core Pattern
Step 1: Identify Diagram Type
Match user intent to the most appropriate Mermaid diagram type:
- Process flows → Flowchart
- Time-based interactions → Sequence Diagram
- Object relationships → Class Diagram
- State transitions → State Diagram
- Database schema → ER Diagram
- Timeline scheduling → Gantt Chart
- Proportional data → Pie Chart
- Hierarchical information → Mind Map
Step 2: Generate Mermaid Syntax
%% Example: Flowchart
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Process 1]
B -->|No| D[Process 2]
C --> E[End]
D --> E
Step 3: Validate Syntax (REQUIRED)
Always validate before outputting:
- Check bracket matching: All
[, ], (, ), {, } must be balanced
- Verify direction:
TD, BT, LR, RL for graphs; << for sequence actors
- Validate arrow syntax:
--> or --- for links, -->|text| for labeled links
- Check node IDs: Unique identifiers without special characters
- Test with Mermaid Live Editor: https://mermaid.live/
Common syntax errors to avoid:
- Missing direction (
graph without TD/BT/LR/RL)
- Unclosed brackets in node labels
- Invalid arrow types (use
--> not ->)
- Duplicate node IDs
- Missing colon after graph type (
graph TD not graph)
Step 4: Provide Output
Present the validated Mermaid code in a markdown code block with mermaid language tag.
Quick Reference
Flowchart Syntax
graph TD/BT/LR/RL
Node[DisplayID Text]
NodeID --> OtherNode
NodeID -->|Label| OtherNode
NodeID --> Decision{condition}
Sequence Diagram Syntax
sequenceDiagram
participant A as Actor
participant B as System
A->>B: Request
B-->>A: Response
Note over A,B: Note text
Class Diagram Syntax
classDiagram
Class01 <|-- DerivedClass
Class01 : +method1()
Class01 : +method2()
Class01 *-- Class02
State Diagram Syntax
stateDiagram-v2
[*] --> State1
State1 --> State2
State2 --> [*]
ER Diagram Syntax
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
Validation Checklist
Before presenting any Mermaid code, verify:
Common Mistakes
| Mistake |
Fix |
| Missing graph direction |
Add TD/BT/LR/RL after graph |
| Unclosed brackets |
Close all [, (, { |
| Wrong arrow type |
Use --> for solid, -- for line |
| Missing pipe in labels |
Use `--> |
| Duplicate IDs |
Use unique IDs: A1, A2 not A, A |
Integration with Excalidraw
For complex diagrams requiring manual editing:
- Use Excalidraw for interactive diagrams
- Export Excalidraw to Mermaid when possible
- Consider user's preference for tool
Resources
1---2name: mermaid-diagram-generator3description: Use when user wants to create flowcharts, sequence diagrams, class diagrams, state diagrams, ER diagrams, gantt charts, pie charts, or any Mermaid diagram from natural language descriptions, requiring syntax validation4---56# Mermaid Diagram Generator78## Overview910Generate Mermaid diagram code from natural language descriptions and validate syntax before outputting. Mermaid is a JavaScript-based diagramming tool that renders diagrams from text definitions.1112## When to Use1314- User says "create a flowchart", "draw a diagram", "make a mind map"15- User wants "sequence diagram", "class diagram", "ER diagram", "state diagram"16- User mentions "Mermaid" directly17- User wants to visualize processes, architectures, or relationships18- User needs syntax-validated diagram code1920## Supported Diagram Types2122| Type | Keyword triggers |23|------|------------------|24| Flowchart | flowchart, flow chart, 流程图 |25| Sequence Diagram | sequence, 时序图, 顺序图 |26| Class Diagram | class diagram, 类图 |27| State Diagram | state diagram, 状态图 |28| ER Diagram | ER diagram, er图, 实体关系图 |29| Gantt Chart | gantt, 甘特图 |30| Pie Chart | pie chart, 饼图 |31| Mind Map | mind map, 思维导图 |32| Journey | user journey, 用户旅程 |33| Git Graph | git graph, git graph |3435## Core Pattern3637### Step 1: Identify Diagram Type3839Match user intent to the most appropriate Mermaid diagram type:40- Process flows → Flowchart41- Time-based interactions → Sequence Diagram42- Object relationships → Class Diagram43- State transitions → State Diagram44- Database schema → ER Diagram45- Timeline scheduling → Gantt Chart46- Proportional data → Pie Chart47- Hierarchical information → Mind Map4849### Step 2: Generate Mermaid Syntax5051```mermaid52%% Example: Flowchart53graph TD54 A[Start] --> B{Decision}55 B -->|Yes| C[Process 1]56 B -->|No| D[Process 2]57 C --> E[End]58 D --> E59```6061### Step 3: Validate Syntax (REQUIRED)6263**Always validate before outputting:**64651. **Check bracket matching**: All `[`, `]`, `(`, `)`, `{`, `}` must be balanced662. **Verify direction**: `TD`, `BT`, `LR`, `RL` for graphs; `<<` for sequence actors673. **Validate arrow syntax**: `-->` or `---` for links, `-->|text|` for labeled links684. **Check node IDs**: Unique identifiers without special characters695. **Test with Mermaid Live Editor**: https://mermaid.live/7071**Common syntax errors to avoid:**72- Missing direction (`graph` without TD/BT/LR/RL)73- Unclosed brackets in node labels74- Invalid arrow types (use `-->` not `->`)75- Duplicate node IDs76- Missing colon after graph type (`graph TD` not `graph`)7778### Step 4: Provide Output7980Present the validated Mermaid code in a markdown code block with `mermaid` language tag.8182## Quick Reference8384### Flowchart Syntax8586```87graph TD/BT/LR/RL88 Node[DisplayID Text]89 NodeID --> OtherNode90 NodeID -->|Label| OtherNode91 NodeID --> Decision{condition}92```9394### Sequence Diagram Syntax9596```97sequenceDiagram98 participant A as Actor99 participant B as System100 A->>B: Request101 B-->>A: Response102 Note over A,B: Note text103```104105### Class Diagram Syntax106107```108classDiagram109 Class01 <|-- DerivedClass110 Class01 : +method1()111 Class01 : +method2()112 Class01 *-- Class02113```114115### State Diagram Syntax116117```118stateDiagram-v2119 [*] --> State1120 State1 --> State2121 State2 --> [*]122```123124### ER Diagram Syntax125126```127erDiagram128 CUSTOMER ||--o{ ORDER : places129 ORDER ||--|{ LINE-ITEM : contains130```131132## Validation Checklist133134Before presenting any Mermaid code, verify:135136- [ ] All brackets and braces are balanced137- [ ] Graph direction is specified (TD/BT/LR/RL)138- [ ] Node IDs use only alphanumeric and underscore139- [ ] Arrow directions are correct (-->, --, |-)140- [ ] Labels use pipe characters (|label|)141- [ ] No duplicate node IDs in same diagram142- [ ] Special characters in labels are escaped143144## Common Mistakes145146| Mistake | Fix |147|---------|-----|148| Missing graph direction | Add TD/BT/LR/RL after `graph` |149| Unclosed brackets | Close all `[`, `(`, `{` |150| Wrong arrow type | Use `-->` for solid, `--` for line |151| Missing pipe in labels | Use `-->|label|` not `-->label` |152| Duplicate IDs | Use unique IDs: A1, A2 not A, A |153154## Integration with Excalidraw155156For complex diagrams requiring manual editing:157- Use Excalidraw for interactive diagrams158- Export Excalidraw to Mermaid when possible159- Consider user's preference for tool160161## Resources162163- Mermaid Live Editor: https://mermaid.live/164- Mermaid Documentation: https://mermaid.js.org/intro/165- Mermaid GitHub: https://github.com/mermaid-js/mermaid