Mermaid Diagrams
This guide explains how to use Mermaid diagrams in your documentation to visualize complex relationships, flows, and architectures.
Overview
Mermaid diagrams help visualize concepts that are difficult to explain with text alone. Use diagrams to complement your documentation when you need to show:
- System architecture and component relationships.
- Data flow between components.
- State machines and lifecycle transitions.
- Sequence diagrams for interactions.
- Class hierarchies and dependencies.
When to Use Diagrams
Use Mermaid diagrams when:
- Architecture Overview: Show how major components connect and interact.
- Flow Visualization: Illustrate decision trees, request flows, or data pipelines.
- State Transitions: Document lifecycle changes or state machines.
- Sequence Interactions: Show the order of operations between multiple components.
Don't use diagrams when:
- A simple bulleted list would be clearer.
- The diagram would be overly complex (>15 nodes).
- Text descriptions are sufficient.
Syntax
Mermaid diagrams are embedded in markdown using code blocks with the mermaid
language identifier:
```mermaid
graph TD
A[Start] --> B{Is it working?}
B -->|Yes| C[Great!]
B -->|No| D[Debug]
D --> B
```
Common Diagram Types
Flowcharts
Use flowcharts to show decision logic, process flows, or branching paths:
graph LR
Client[Client Request] --> Router[Router]
Router --> Auth{Authenticated?}
Auth -->|Yes| Handler[Request Handler]
Auth -->|No| Error[401 Unauthorized]
Handler --> Response[Send Response]
Sequence Diagrams
Show interactions between components over time:
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: HTTP Request
Server->>Database: Query Data
Database-->>Server: Return Results
Server-->>Client: HTTP Response
Class Diagrams
Document object-oriented relationships:
classDiagram
class Document {
+String content
+render()
+save()
}
class Guide {
+String title
+load()
}
Document <|-- Guide
State Diagrams
Illustrate state transitions and lifecycle:
stateDiagram-v2
[*] --> Idle
Idle --> Processing: start()
Processing --> Complete: finish()
Processing --> Error: fail()
Error --> Idle: retry()
Complete --> [*]
Best Practices
- Keep it Simple: Limit diagrams to 10-15 nodes for readability.
- Use Clear Labels: Node labels should be concise but descriptive.
- Consistent Styling: Use similar diagram types for similar concepts.
- Context First: Explain what the diagram shows before presenting it.
- Complement, Don't Replace: Use diagrams alongside text explanations.
- Test Rendering: Verify diagrams render correctly in the documentation.
Reference
For complete Mermaid syntax and diagram types, see the Mermaid documentation.