Mermaid Diagrams
Create professional software diagrams using Mermaid's text-based syntax. Mermaid renders diagrams from simple text definitions, making diagrams version-controllable, easy to update, and maintainable alongside code.
Installation
OpenClaw / Moltbot / Clawbot
CODEBLOCK0
Core Syntax
All Mermaid diagrams follow this pattern:
CODEBLOCK1
Key principles:
- - First line declares diagram type (e.g.,
classDiagram, sequenceDiagram, flowchart) - Use
%% for comments - Line breaks and indentation improve readability but aren't required
- Unknown words break diagrams; invalid parameters fail silently
Diagram Type Selection
| Type | Use For | Reference |
|---|
| Class Diagrams | Domain modeling, OOP design, entity relationships | INLINECODE4 |
| Sequence Diagrams |
API flows, auth flows, component interactions |
references/sequence-diagrams.md |
|
Flowcharts | Processes, algorithms, decision trees, user journeys |
references/flowcharts.md |
|
ERD | Database schemas, table relationships, data modeling |
references/erd-diagrams.md |
|
C4 Diagrams | System context, containers, components, architecture |
references/c4-diagrams.md |
|
State Diagrams | State machines, lifecycle states | — |
|
Git Graphs | Branching strategies | — |
|
Gantt Charts | Project timelines, scheduling | — |
For styling, themes, and layout options: See INLINECODE9
Quick Start Examples
Class Diagram (Domain Model)
CODEBLOCK2
Sequence Diagram (API Flow)
CODEBLOCK3
Flowchart (User Journey)
CODEBLOCK4
ERD (Database Schema)
CODEBLOCK5
Best Practices
- 1. Start simple — begin with core entities/components, add details incrementally
- Use meaningful names — clear labels make diagrams self-documenting
- Comment extensively — use
%% comments to explain complex relationships - Keep focused — one diagram per concept; split large diagrams into multiple views
- Version control — store
.mmd files alongside code for easy updates - Add context — include titles and notes to explain diagram purpose
- Iterate — refine diagrams as understanding evolves
Configuration and Theming
Configure diagrams using frontmatter:
CODEBLOCK6
Available themes: default, forest, dark, neutral, base
Layout options:
- -
layout: dagre (default) — classic balanced layout - INLINECODE13 — advanced layout for complex diagrams
Look options:
- -
look: classic — traditional Mermaid style - INLINECODE15 — sketch-like appearance
Rendering and Export
Native support in:
- - GitHub/GitLab — automatically renders in Markdown
- VS Code — with Markdown Mermaid extension
- Notion, Obsidian, Confluence — built-in support
Export options:
- - Mermaid Live Editor — online editor with PNG/SVG export
- Mermaid CLI —
npm install -g @mermaid-js/mermaid-cli then INLINECODE17
When to Create Diagrams
Always diagram when:
- - Starting new projects or features
- Documenting complex systems
- Explaining architecture decisions
- Designing database schemas
- Planning refactoring efforts
- Onboarding new team members
Use diagrams to:
- - Align stakeholders on technical decisions
- Document domain models collaboratively
- Visualize data flows and system interactions
- Plan before coding
- Create living documentation that evolves with code
Common Pitfalls
- - Breaking characters — avoid
{} in comments; escape special characters - Syntax errors — misspellings break diagrams; validate in Mermaid Live
- Overcomplexity — split complex diagrams into multiple focused views
- Missing relationships — document all important connections between entities
- Stale diagrams — a wrong diagram is worse than no diagram; update when systems change
NEVER Do
- 1. NEVER create diagrams with more than 15 nodes — they become unreadable; split into multiple focused diagrams
- NEVER leave arrows unlabeled — every connection should explain the relationship or data flow
- NEVER create diagrams without a title or caption — context-free diagrams are useless outside the author's head
- NEVER use diagrams as the sole documentation — pair diagrams with prose that explains the "why"
- NEVER let diagrams go stale — update diagrams when architecture changes; stale diagrams mislead
- NEVER use Mermaid for data visualization — Mermaid is for architecture and flow diagrams, not charts of business data
Mermaid 图表
使用 Mermaid 基于文本的语法创建专业软件图表。Mermaid 从简单的文本定义渲染图表,使图表可版本控制、易于更新,并能与代码一起维护。
安装
OpenClaw / Moltbot / Clawbot
bash
npx clawhub@latest install mermaid-diagrams
核心语法
所有 Mermaid 图表遵循以下模式:
mermaid
diagramType
definition content
关键原则:
- - 第一行声明图表类型(例如 classDiagram、sequenceDiagram、flowchart)
- 使用 %% 添加注释
- 换行和缩进可提高可读性,但不是必需的
- 未知单词会导致图表出错;无效参数会静默失败
图表类型选择
| 类型 | 用途 | 参考文档 |
|---|
| 类图 | 领域建模、面向对象设计、实体关系 | references/class-diagrams.md |
| 时序图 |
API 流程、认证流程、组件交互 | references/sequence-diagrams.md |
|
流程图 | 流程、算法、决策树、用户旅程 | references/flowcharts.md |
|
ERD | 数据库模式、表关系、数据建模 | references/erd-diagrams.md |
|
C4 图 | 系统上下文、容器、组件、架构 | references/c4-diagrams.md |
|
状态图 | 状态机、生命周期状态 | — |
|
Git 图 | 分支策略 | — |
|
甘特图 | 项目时间线、排期 | — |
关于样式、主题和布局选项:参见 references/advanced-features.md
快速入门示例
类图(领域模型)
mermaid
classDiagram
Title -- Genre
Title *-- Season
Title *-- Review
User --> Review : creates
class Title {
+string name
+int releaseYear
+play()
}
class Genre {
+string name
+getTopTitles()
}
时序图(API 流程)
mermaid
sequenceDiagram
participant User
participant API
participant Database
User->>API: POST /login
API->>Database: Query credentials
Database-->>API: Return user data
alt Valid credentials
API-->>User: 200 OK + JWT token
else Invalid credentials
API-->>User: 401 Unauthorized
end
流程图(用户旅程)
mermaid
flowchart TD
Start([User visits site]) --> Auth{Authenticated?}
Auth -->|No| Login[Show login page]
Auth -->|Yes| Dashboard[Show dashboard]
Login --> Creds[Enter credentials]
Creds --> Validate{Valid?}
Validate -->|Yes| Dashboard
Validate -->|No| Error[Show error]
Error --> Login
ERD(数据库模式)
mermaid
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
PRODUCT ||--o{ LINE_ITEM : includes
USER {
int id PK
string email UK
string name
datetime created_at
}
ORDER {
int id PK
int user_id FK
decimal total
datetime created_at
}
最佳实践
- 1. 从简单开始 — 从核心实体/组件入手,逐步添加细节
- 使用有意义的名称 — 清晰的标签使图表具有自文档性
- 广泛添加注释 — 使用 %% 注释解释复杂关系
- 保持聚焦 — 每个概念一个图表;将大型图表拆分为多个视图
- 版本控制 — 将 .mmd 文件与代码一起存储,便于更新
- 添加上下文 — 包含标题和注释以解释图表目的
- 迭代改进 — 随着理解的深入不断完善图表
配置与主题
使用 frontmatter 配置图表:
mermaid
config:
theme: base
themeVariables:
primaryColor: #ff6b6b
flowchart LR
A --> B
可用主题: default、forest、dark、neutral、base
布局选项:
- - layout: dagre(默认)— 经典平衡布局
- layout: elk — 复杂图表的进阶布局
外观选项:
- - look: classic — 传统 Mermaid 风格
- look: handDrawn — 手绘风格外观
渲染与导出
原生支持平台:
- - GitHub/GitLab — 在 Markdown 中自动渲染
- VS Code — 配合 Markdown Mermaid 扩展
- Notion、Obsidian、Confluence — 内置支持
导出选项:
- - Mermaid Live Editor — 在线编辑器,支持 PNG/SVG 导出
- Mermaid CLI — npm install -g @mermaid-js/mermaid-cli 然后 mmdc -i input.mmd -o output.png
何时创建图表
始终在以下情况创建图表:
- - 开始新项目或新功能时
- 记录复杂系统时
- 解释架构决策时
- 设计数据库模式时
- 规划重构工作时
- 新团队成员入职时
使用图表的目的:
- - 在技术决策上对齐利益相关者
- 协作记录领域模型
- 可视化数据流和系统交互
- 编码前进行规划
- 创建随代码演进的活文档
常见陷阱
- - 破坏性字符 — 避免在注释中使用 {};转义特殊字符
- 语法错误 — 拼写错误会破坏图表;在 Mermaid Live 中验证
- 过度复杂 — 将复杂图表拆分为多个聚焦的视图
- 遗漏关系 — 记录实体之间的所有重要连接
- 过时图表 — 错误的图表比没有图表更糟糕;系统变更时及时更新
绝对禁止
- 1. 绝对不要创建超过 15 个节点的图表 — 它们会变得不可读;拆分为多个聚焦的图表
- 绝对不要留下未标注的箭头 — 每个连接都应解释关系或数据流
- 绝对不要创建没有标题或说明的图表 — 脱离上下文的图表在作者之外毫无用处
- 绝对不要将图表作为唯一文档 — 图表应与解释为什么的文字说明配合使用
- 绝对不要让图表过时 — 架构变更时更新图表;过时图表会误导他人
- 绝对不要使用 Mermaid 进行数据可视化 — Mermaid 用于架构和流程图,而非业务数据图表