Generate syntactically correct Mermaid diagrams for technical documentation
The mermaid-gen skill provides AI assistants with expert guidance for creating syntactically correct Mermaid diagrams while avoiding common parsing errors that cause rendering failures. This skill focuses on diagram syntax correctness and best practices.
Key Benefits:
Use mermaid-gen when:
Do NOT use mermaid-gen when:
charts-flow skill instead)Relationship to charts-flow skill:
charts-flow: Manages diagram files, generates SVG, embeds in documentsmermaid-gen: Ensures diagram syntax is correct before file creationcharts-flow skill calls mermaid-gen for diagram generation| Input | Description | Example |
|---|---|---|
| Diagram Type | Type of Mermaid diagram | flowchart, sequence, state, class |
| Diagram Purpose | What the diagram represents | "Agent communication flow", "State machine transitions" |
| Content Description | Key elements and relationships | "3 agents, 1 database, request/response flow" |
| Input | Description | Default |
|---|---|---|
| Existing Code | Current Mermaid code (if fixing) | None (creating new) |
| Error Message | Parse error from renderer | None |
| Styling Preferences | Color scheme, layout direction | Project defaults |
| Naming Convention | Node ID format | camelCase |
Follow this 5-step process to create or fix Mermaid diagrams:
<br/>) in double quotesNodeID[Display Label]%%) for complex sectionsError Recovery: If parse error occurs, see "Troubleshooting Guide" section below.
❌ WRONG - Nested quotes cause parse errors:
User[User: "What is the status?"] --> System
Response["Message: "Success""] --> User
✅ CORRECT - Remove inner quotes or escape properly:
User[User: What is the status?] --> System
Response["Message: Success"] --> User
Rule: Never use nested quotes within node labels enclosed in [] or (). Either:
<br/> tags❌ WRONG - Decimals and special chars without quotes:
Results[Top Results:<br/>1. system_arch.md (0.89)<br/>2. safety.yaml (0.84)]
✅ CORRECT - Wrap in double quotes when using
with numbers/decimals:
Results["Top Results:<br/>1. system_arch.md (0.89)<br/>2. safety.yaml (0.84)"]
Rule: When node labels contain:
<br/> tags AND decimal numbers<br/> tags AND special charactersWrap the ENTIRE label in double quotes: NodeID["label content"]
❌ WRONG - Quotes in edge labels:
A -->|"Execute"| B
A -->|"Cancel" or Timeout| C
✅ CORRECT - Remove quotes from edge labels:
A -->|Execute| B
A -->|Cancel or Timeout| C
Rule: Edge labels (text between |...|) should never contain quotes unless absolutely necessary. Use plain text.
❌ WRONG - Ampersands and special chars in style references:
subgraph "Audit & Compliance"
Node1
end
style Audit & Compliance fill:#fff
✅ CORRECT - Use ID/label syntax for subgraphs:
subgraph AuditCompliance["Audit & Compliance"]
Node1
end
style AuditCompliance fill:#fff
Rule: When applying styles to subgraphs with special characters:
subgraph ID["Display Name"]style ID fill:#color✅ CORRECT - Separate IDs from labels:
UserNode[User: Query the system] --> ProcessNode[Processing Service]
ProcessNode --> DBNode[(Database)]
Rule:
[) is the node ID (no spaces, use camelCase)[] is the display label (can have spaces, special chars when quoted)flowchart TD
%% Define nodes with clear IDs and labels
User[User Input] -->|Action| Service[Processing Service<br/>FastAPI]
Service -->|Validate| Validator{Validation Check}
Validator -->|Invalid| ErrorNode["Error Response<br/>Status: 400"]
Validator -->|Valid| Database[(Database<br/>PostgreSQL)]
Database -->|Query Result| Transform[Transform Data]
Transform -->|Response| User
%% Group related components
subgraph Backend["Backend Services"]
Service
Validator
Transform
end
subgraph Storage["Data Storage"]
Database
end
%% Apply styles AFTER all nodes defined
style User fill:#e1f5ff
style Backend fill:#f0f0f0
style Storage fill:#fff4e1
sequenceDiagram
participant User
participant API as API Gateway
participant Auth as Auth Service
participant DB as Database
User->>API: Request with credentials
API->>Auth: Validate token
Auth-->>API: Token valid
API->>DB: Query data
DB-->>API: Return results
API-->>User: Success response
Note over User,DB: All communication encrypted via TLS
stateDiagram-v2
[*] --> Idle
Idle --> Processing: Start request
Processing --> Success: Complete
Processing --> Failed: Error occurs
Success --> [*]
Failed --> Retry: Auto retry
Retry --> Processing: Attempt again
Failed --> [*]: Max retries exceeded
<br/> wrapped in double quotes|...|)%% comments<br/> tags instead)node1, node2 (use descriptive IDs)User Request:
"Create a flowchart showing the authentication flow: user submits credentials, system validates, checks database, returns token or error"
Skill Actions:
userInput, validator, dbCheck, tokenService, errorResponse, successResponseResult:
flowchart TD
userInput[User: Submit Credentials] -->|POST /auth| validator{Validate Format}
validator -->|Invalid| errorResponse["Error Response<br/>Status: 400"]
validator -->|Valid| dbCheck[(Database<br/>Verify User)]
dbCheck -->|Not Found| errorResponse
dbCheck -->|Found| tokenService[Generate JWT Token]
tokenService -->|Return| successResponse["Success Response<br/>Token + Expiry"]
User Request:
"My sequence diagram won't render. Error: 'Expecting SQE, got STR'. Here's the code:
User->>API: Request "data""
Skill Actions:
Request "data"Request dataResult:
sequenceDiagram
User->>API: Request data
API-->>User: Response
Explanation: Edge labels between |...| or after : should not contain quotes.
User Request:
"Create a state diagram for order processing: Draft → Submitted → Processing → Completed or Failed. Include transition conditions."
Skill Actions:
stateDiagram-v2 syntaxDraft, Submitted, Processing, Completed, FailedResult:
stateDiagram-v2
[*] --> Draft
Draft --> Submitted: User clicks Submit
Submitted --> Processing: Payment verified
Processing --> Completed: All items shipped
Processing --> Failed: Payment declined
Completed --> [*]
Failed --> Draft: Retry allowed
Failed --> [*]: Max retries exceeded
flowchart TD
Input[Receive Input] --> Validate{Validate Input}
Validate -->|Invalid| Reject["Reject with Error<br/>Return 400"]
Validate -->|Valid| Process[Process Request]
Process --> Output[Return Success]
flowchart TD
User[User Query] --> Router{Query Router}
Router -->|Database| SQL[SQL Connector]
Router -->|Cache| Redis[Redis Connector]
Router -->|API| External[External API]
SQL --> Merge[Merge Results]
Redis --> Merge
External --> Merge
Merge --> Response[Formatted Response]
Response --> User
flowchart TD
Start[Start Process] --> Try{Execute}
Try -->|Success| Complete[Complete Successfully]
Try -->|Error| Retry{Retry Count < 3?}
Retry -->|Yes| Wait[Exponential Backoff]
Wait --> Try
Retry -->|No| Failed["Failed<br/>Log and Alert"]
subgraph ID["Name"]flowchart TD
User[User: Submit command] -->|1. Classify| Detector{Command Detector<br/>GPT-4 + Rules}
Detector -->|Query| QueryPath[Execute Immediately]
Detector -->|Command| SafetyPath[Safety Validation]
SafetyPath -->|Check| Circuit{Circuit Breaker Status}
Circuit -->|Active| Block["Block Command<br/>Display Reason"]
Circuit -->|Inactive| Execute[Execute Command]
Execute -->|Log| Audit[WORM Audit Storage]
Execute -->|Response| User
subgraph SafetyControls["Safety Controls"]
Detector
Circuit
Audit
end
style User fill:#e1f5ff
style SafetyControls fill:#fff4e1
flowchart LR
Source[Data Source] -->|Ingest| Process[Processing Pipeline]
Process -->|Transform| Transform[Transform Layer<br/>Validate & Clean]
Transform -->|Load| Storage[(Data Warehouse)]
Storage -->|Query| Analytics[Analytics Service]
Analytics -->|Visualize| Dashboard[Dashboard UI]
subgraph Pipeline["Data Pipeline"]
Process
Transform
end
style Pipeline fill:#f0f0f0
| Element | Syntax | Example |
|---|---|---|
| Node (rectangle) | ID[Label] |
Node1[Process Data] |
| Node (rounded) | ID(Label) |
Start(Begin Process) |
| Node (database) | ID[(Label)] |
DB[(PostgreSQL)] |
| Node (circle) | ID((Label)) |
Point((A)) |
| Decision | ID{Label} |
Check{Is Valid?} |
| Arrow | --> |
A --> B |
| Labeled arrow | -- text --> or `--> |
text |
| Subgraph | subgraph ID["Label"] |
subgraph Sys["System"] |
| Style | style ID fill:#color |
style Node1 fill:#e1f5ff |
| Comment | %% comment |
%% This is a note |
| Line break | <br/> in label |
Node["Line 1<br/>Line 2"] |
When using the mermaid-gen skill, the primary output is:
Mermaid Diagram Code Block:
mermaid language identifierExample Output:
```mermaid
flowchart TD
%% Define nodes
Start[Start Process] --> Process[Execute Task]
Process --> End[Complete]
%% Apply styling
style Start fill:#e1f5ff
style End fill:#c8e6c9
```
Skill Relationship:
Typical Workflow:
charts-flow skill invoked for file managementcharts-flow internally calls mermaid-gen for diagram generationmermaid-gen returns validated Mermaid codecharts-flow creates diagram file, generates SVG, embeds in parent documentStandalone Usage: When user only needs diagram code (not file management):
mermaid-gen directlyCombined Usage: When user needs complete diagram file workflow:
charts-flow skill (which uses mermaid-gen internally)Every output from mermaid-gen must meet these criteria:
charts-flow Skill:
mermaid-gen to generate diagram contentdiagrams/ subdirectoriesdoc-flow Skill:
mermaid-gen support ADRs, SYS docs, and architecture specificationsproject-mngt Skill:
Technical Guidelines (per CLAUDE.md):
Best Practice Integration:
mermaid-gen for syntax correctnesscharts-flow for file management and performance optimization