diagram-mermaid
Generate Mermaid code blocks (```mermaid fences) for inline Markdown rendering. Covers 11 diagram types — zero runtime dependencies, renders natively in GitHub, Obsidian, VS Code, and most Markdown viewers.
Quick Start
- Classify the diagram type from the user's request (see Routing table below)
- Read the matching example in
examples/<type>.md for that type's syntax patterns
- For complex syntax: load
references/mermaid-syntax.md for full grammar reference
- For layout issues: load
references/layout-best-practices.md (avoiding node overlap, arrow crossings)
- For special characters / long text: load
references/common-pitfalls.md (escaping, line breaks)
- Write Mermaid between
```mermaid opening fence and ``` closing fence
- Run the self-review checklist before delivering
Critical Rules (apply to ALL diagram types)
- Code fence: ALWAYS use
```mermaid opening fence (lowercase). Close with ```.
- First line: the diagram type keyword (
flowchart, sequenceDiagram, classDiagram, stateDiagram-v2, erDiagram, gantt, pie, mindmap, timeline, gitGraph, journey).
- Direction (flowchart only):
flowchart TD (top-down, default), flowchart LR (left-right), flowchart BT (bottom-top), flowchart RL (right-left).
- Node ID vs label:
A[Label text] — A is the ID (used in edges), [Label text] is the displayed text. Don't use reserved words (end, subgraph, class, style) as IDs.
- Special characters in labels: wrap label text in
"..." if it contains (, ), [, ], {, }, <, >, #, ", &, or |. Example: A["function(x, y)"].
- Long text: use
<br/> for line breaks inside labels. Example: A["First line<br/>Second line"].
- Comments:
%% for single-line comments.
- Styling:
classDef name fill:#f9f,stroke:#333; then class nodeId name; (or class nodeId1,nodeId2 name;).
- Subgraphs (flowchart):
subgraph Title\n ... \nend — wraps related nodes.
Diagram Type Routing
When the user's request matches one of these, read the matching example first:
| Type |
Trigger keywords |
First line |
Example file |
| Flowchart |
流程图、process、workflow、decision tree、流程 |
flowchart TD |
examples/flowchart.md |
| Sequence |
时序图、调用链、交互、message、API call |
sequenceDiagram |
examples/sequence.md |
| Class |
类图、class diagram、inheritance、UML class |
classDiagram |
examples/class.md |
| State |
状态图、state machine、lifecycle、状态机 |
stateDiagram-v2 |
examples/state.md |
| ER |
ER图、entity、relationship、database schema、实体关系 |
erDiagram |
examples/er.md |
| Gantt |
甘特图、project timeline、schedule、project plan |
gantt |
examples/gantt.md |
| Pie |
饼图、pie chart、proportion、占比 |
pie |
examples/pie.md |
| Mindmap |
思维导图、brainstorm tree、concept map、脑图 |
mindmap |
examples/mindmap.md |
| Timeline |
时间线、timeline、chronology、历史 |
timeline |
examples/timeline.md |
| gitGraph |
git图、分支图、commit history、branch graph |
gitGraph |
examples/gitgraph.md |
| Journey |
用户旅程、user journey、用户体验、customer journey |
journey |
examples/journey.md |
Quick Examples
Flowchart
flowchart LR
A[User Request] --> B{Auth?}
B -- Yes --> C[Process]
B -- No --> D[401]
C --> E[(Database)]
C --> F[Cache]
E --> G[Response]
F --> G
Sequence
sequenceDiagram
participant U as User
participant API as API Server
participant DB as Database
U->>API: GET /resource
API->>DB: SELECT
DB-->>API: rows
API-->>U: 200 JSON
Class
classDiagram
class User {
+id: Long
+name: String
+login(): boolean
}
class Order {
+id: Long
+total: Decimal
}
User "1" -- "*" Order : places
State
stateDiagram-v2
[*] --> Pending
Pending --> Processing : start
Processing --> Done : success
Processing --> Failed : error
Done --> [*]
Failed --> [*]
ER
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
USER {
long id PK
string name
}
ORDER {
long id PK
decimal total
}
Gantt
gantt
title Project Schedule
dateFormat YYYY-MM-DD
section Design
Spec :a, 2024-01-01, 7d
Mockups :after a, 5d
section Dev
Backend :2024-01-15, 10d
Frontend :2024-01-20, 8d
Pie
pie title Browser Market Share
"Chrome" : 65
"Safari" : 18
"Firefox" : 5
"Edge" : 4
"Other" : 8
Mindmap
mindmap
root((Project))
Frontend
React
Tailwind
Backend
FastAPI
PostgreSQL
DevOps
Docker
CI/CD
Timeline
timeline
title Project History
2022 : Idea
2023 : MVP
2024 : v1.0
2025 : Scale
gitGraph
gitGraph
commit id: "init"
commit id: "feat-A"
branch feature
checkout feature
commit id: "WIP"
checkout main
merge feature
commit id: "release"
Journey
journey
title User shopping journey
section Browse
Visit homepage: 5: User
Search product: 4: User
section Buy
Add to cart: 5: User
Checkout: 3: User, System
Pay: 2: User
section After
Receive email: 5: User, System
For more examples and the full syntax of each type, see examples/<type>.mmd and references/mermaid-syntax.md.
依赖与降级
| 依赖 |
必需性 |
缺失时行为 |
| 任何 Markdown 渲染器(GitHub / Obsidian / VS Code / Cursor 等) |
必需(用于渲染 Mermaid 代码块为图) |
代码块以纯文本展示——但 Mermaid 源码仍正确,可粘贴到在线渲染器 |
| 在线 Mermaid 渲染器(如 https://mermaid.live) |
可选(手动渲染备选) |
不影响生成本身;用户可粘贴代码到 mermaid.live 在线渲染 |
关键:Mermaid 代码块永远可生成 — 本 skill 只产出文本代码块,零运行时依赖。GitHub 原生支持 Mermaid 渲染(README/issue/PR 评论里直接显示为图),Obsidian、VS Code(装 Mermaid 扩展)、Cursor、多数 Markdown 预览器也都原生支持。
兼容性提示
- GitHub:原生支持所有 11 种类型(2024 起含 mindmap、timeline、gitGraph、journey)
- GitLab:原生支持,但 mindmap/timeline 等新类型可能落后
- Obsidian:原生支持(需在设置里启用 Mermaid)
- VS Code:需装
Markdown Preview Mermaid Support 扩展
- Cursor / Windsurf:内置 Mermaid 支持
- Notion:不支持 Mermaid 代码块——这种场景请走
diagram-html 或 diagram-image
输出自检清单
交付前的最终检查清单:
代码块结构
语法正确性
内容质量
布局质量
如以上任一项失败,参考 references/layout-best-practices.md 和 references/common-pitfalls.md。
相关技能
本 skill 是 diagram 技能家族的一员,按输出格式分工,4 个 skill 互补但不重叠:
| Skill |
输出形态 |
主用途 |
diagram-mermaid(本 skill) |
Mermaid 代码块(内联 Markdown) |
GitHub README/issue/PR 嵌入,零依赖,GitHub 直接渲染,11 种基础图类型 |
diagram-plantuml |
PlantUML 代码块(内联 Markdown) |
UML/云架构/网络拓扑/安全/ArchiMate/BPMN/数据管道/IoT/思维导图,9500+ 图标库 |
diagram-html |
独立 HTML 文件 |
可分享的成品图,浏览器打开即用,双主题切换 + 浏览器导出菜单 |
diagram-image |
SVG + PNG 文件 |
命令行直接产出图片文件,适合 CI/批处理/嵌入不支持 SVG 的环境 |
选用决策:
- 在 Markdown 里嵌入图、要源码可读、可 diff → 本 skill(
diagram-mermaid)或 diagram-plantuml
- 要可交互的 HTML 成品、双主题切换、点按钮导出 →
diagram-html
- 要命令行直接出 SVG/PNG 文件、CI/批处理 →
diagram-image
默认推荐:
- 简单 flowchart / sequence / state / ER / mindmap → 优先本 skill(最轻量,GitHub 原生渲染)
- 需要 AWS/Azure/GCP/Cisco 等专业图标或 BPMN/ArchiMate/IoT/UML 全家桶 → 优先
diagram-plantuml
- 要可分享的 HTML 成品 →
diagram-html
- 要 SVG/PNG 文件 →
diagram-image
1---2name: diagram-mermaid3description: Generate Mermaid code blocks (` ```mermaid ` fences) for inline Markdown rendering of technical diagrams. Covers 11 types — flowchart, sequence, class, state, ER, Gantt, pie, mindmap, timeline, gitGraph, and journey. Use whenever the user asks for a flowchart, sequence diagram, state diagram, ER diagram, Gantt chart, pie chart, mindmap, timeline, git graph, or user journey map. Trigger on: "画流程图" "画时序图" "状态图" "ER图" "甘特图" "饼图" "思维导图" "时间线" "git图" "用户旅程" "Mermaid" "GitHub 渲染" "嵌入文档" or any diagram request where the output should be inline Markdown. Part of the diagram skill family — pick this skill for Mermaid code blocks, zero-dependency output that renders natively in GitHub/Markdown, or one of the 11 supported types. Lightest-weight entry point of the diagram family — default for "画个流程图/时序图" unless another format is explicitly requested.4---56# diagram-mermaid78Generate Mermaid code blocks (` ```mermaid ` fences) for inline Markdown rendering. Covers 11 diagram types — zero runtime dependencies, renders natively in GitHub, Obsidian, VS Code, and most Markdown viewers.910## Quick Start11121. **Classify the diagram type** from the user's request (see Routing table below)132. **Read the matching example** in `examples/<type>.md` for that type's syntax patterns143. **For complex syntax**: load `references/mermaid-syntax.md` for full grammar reference154. **For layout issues**: load `references/layout-best-practices.md` (avoiding node overlap, arrow crossings)165. **For special characters / long text**: load `references/common-pitfalls.md` (escaping, line breaks)176. **Write Mermaid** between ` ```mermaid ` opening fence and ` ``` ` closing fence187. **Run the self-review checklist** before delivering1920## Critical Rules (apply to ALL diagram types)2122- **Code fence**: ALWAYS use ` ```mermaid ` opening fence (lowercase). Close with ` ``` `.23- **First line**: the diagram type keyword (`flowchart`, `sequenceDiagram`, `classDiagram`, `stateDiagram-v2`, `erDiagram`, `gantt`, `pie`, `mindmap`, `timeline`, `gitGraph`, `journey`).24- **Direction** (flowchart only): `flowchart TD` (top-down, default), `flowchart LR` (left-right), `flowchart BT` (bottom-top), `flowchart RL` (right-left).25- **Node ID vs label**: `A[Label text]` — `A` is the ID (used in edges), `[Label text]` is the displayed text. Don't use reserved words (`end`, `subgraph`, `class`, `style`) as IDs.26- **Special characters in labels**: wrap label text in `"..."` if it contains `(`, `)`, `[`, `]`, `{`, `}`, `<`, `>`, `#`, `"`, `&`, or `|`. Example: `A["function(x, y)"]`.27- **Long text**: use `<br/>` for line breaks inside labels. Example: `A["First line<br/>Second line"]`.28- **Comments**: `%%` for single-line comments.29- **Styling**: `classDef name fill:#f9f,stroke:#333;` then `class nodeId name;` (or `class nodeId1,nodeId2 name;`).30- **Subgraphs** (flowchart): `subgraph Title\n ... \nend` — wraps related nodes.3132## Diagram Type Routing3334When the user's request matches one of these, read the matching example first:3536| Type | Trigger keywords | First line | Example file |37|---|---|---|---|38| **Flowchart** | 流程图、process、workflow、decision tree、流程 | `flowchart TD` | [`examples/flowchart.md`](examples/flowchart.md) |39| **Sequence** | 时序图、调用链、交互、message、API call | `sequenceDiagram` | [`examples/sequence.md`](examples/sequence.md) |40| **Class** | 类图、class diagram、inheritance、UML class | `classDiagram` | [`examples/class.md`](examples/class.md) |41| **State** | 状态图、state machine、lifecycle、状态机 | `stateDiagram-v2` | [`examples/state.md`](examples/state.md) |42| **ER** | ER图、entity、relationship、database schema、实体关系 | `erDiagram` | [`examples/er.md`](examples/er.md) |43| **Gantt** | 甘特图、project timeline、schedule、project plan | `gantt` | [`examples/gantt.md`](examples/gantt.md) |44| **Pie** | 饼图、pie chart、proportion、占比 | `pie` | [`examples/pie.md`](examples/pie.md) |45| **Mindmap** | 思维导图、brainstorm tree、concept map、脑图 | `mindmap` | [`examples/mindmap.md`](examples/mindmap.md) |46| **Timeline** | 时间线、timeline、chronology、历史 | `timeline` | [`examples/timeline.md`](examples/timeline.md) |47| **gitGraph** | git图、分支图、commit history、branch graph | `gitGraph` | [`examples/gitgraph.md`](examples/gitgraph.md) |48| **Journey** | 用户旅程、user journey、用户体验、customer journey | `journey` | [`examples/journey.md`](examples/journey.md) |4950## Quick Examples5152### Flowchart5354```mermaid55flowchart LR56 A[User Request] --> B{Auth?}57 B -- Yes --> C[Process]58 B -- No --> D[401]59 C --> E[(Database)]60 C --> F[Cache]61 E --> G[Response]62 F --> G63```6465### Sequence6667```mermaid68sequenceDiagram69 participant U as User70 participant API as API Server71 participant DB as Database72 U->>API: GET /resource73 API->>DB: SELECT74 DB-->>API: rows75 API-->>U: 200 JSON76```7778### Class7980```mermaid81classDiagram82 class User {83 +id: Long84 +name: String85 +login(): boolean86 }87 class Order {88 +id: Long89 +total: Decimal90 }91 User "1" -- "*" Order : places92```9394### State9596```mermaid97stateDiagram-v298 [*] --> Pending99 Pending --> Processing : start100 Processing --> Done : success101 Processing --> Failed : error102 Done --> [*]103 Failed --> [*]104```105106### ER107108```mermaid109erDiagram110 USER ||--o{ ORDER : places111 ORDER ||--|{ LINE_ITEM : contains112 USER {113 long id PK114 string name115 }116 ORDER {117 long id PK118 decimal total119 }120```121122### Gantt123124```mermaid125gantt126 title Project Schedule127 dateFormat YYYY-MM-DD128 section Design129 Spec :a, 2024-01-01, 7d130 Mockups :after a, 5d131 section Dev132 Backend :2024-01-15, 10d133 Frontend :2024-01-20, 8d134```135136### Pie137138```mermaid139pie title Browser Market Share140 "Chrome" : 65141 "Safari" : 18142 "Firefox" : 5143 "Edge" : 4144 "Other" : 8145```146147### Mindmap148149```mermaid150mindmap151 root((Project))152 Frontend153 React154 Tailwind155 Backend156 FastAPI157 PostgreSQL158 DevOps159 Docker160 CI/CD161```162163### Timeline164165```mermaid166timeline167 title Project History168 2022 : Idea169 2023 : MVP170 2024 : v1.0171 2025 : Scale172```173174### gitGraph175176```mermaid177gitGraph178 commit id: "init"179 commit id: "feat-A"180 branch feature181 checkout feature182 commit id: "WIP"183 checkout main184 merge feature185 commit id: "release"186```187188### Journey189190```mermaid191journey192 title User shopping journey193 section Browse194 Visit homepage: 5: User195 Search product: 4: User196 section Buy197 Add to cart: 5: User198 Checkout: 3: User, System199 Pay: 2: User200 section After201 Receive email: 5: User, System202```203204For more examples and the full syntax of each type, see `examples/<type>.mmd` and `references/mermaid-syntax.md`.205206## 依赖与降级207208| 依赖 | 必需性 | 缺失时行为 |209|---|---|---|210| 任何 Markdown 渲染器(GitHub / Obsidian / VS Code / Cursor 等) | 必需(用于渲染 Mermaid 代码块为图) | 代码块以纯文本展示——但 Mermaid 源码仍正确,可粘贴到在线渲染器 |211| 在线 Mermaid 渲染器(如 https://mermaid.live) | 可选(手动渲染备选) | 不影响生成本身;用户可粘贴代码到 mermaid.live 在线渲染 |212213**关键:Mermaid 代码块永远可生成** — 本 skill 只产出文本代码块,零运行时依赖。GitHub 原生支持 Mermaid 渲染(README/issue/PR 评论里直接显示为图),Obsidian、VS Code(装 Mermaid 扩展)、Cursor、多数 Markdown 预览器也都原生支持。214215### 兼容性提示216217- **GitHub**:原生支持所有 11 种类型(2024 起含 mindmap、timeline、gitGraph、journey)218- **GitLab**:原生支持,但 mindmap/timeline 等新类型可能落后219- **Obsidian**:原生支持(需在设置里启用 Mermaid)220- **VS Code**:需装 `Markdown Preview Mermaid Support` 扩展221- **Cursor / Windsurf**:内置 Mermaid 支持222- **Notion**:不支持 Mermaid 代码块——这种场景请走 `diagram-html` 或 `diagram-image`223224## 输出自检清单225226交付前的最终检查清单:227228### 代码块结构229- [ ] 以 ` ```mermaid ` 开头(小写,无空格在 mermaid 前)230- [ ] 以 ` ``` ` 结尾(独占一行)231- [ ] 内部第一行是图类型关键字(`flowchart`/`sequenceDiagram`/`classDiagram`/...)232- [ ] 无嵌套代码块导致 fence 冲突233234### 语法正确性235- [ ] 节点 ID 简短、不与关键字冲突(不用 `end`、`subgraph`、`class`、`style`、`click` 等)236- [ ] 节点 label 中的特殊字符(`()`、`[]`、`{}`、`#`、`"`、`<`、`>`、`&`、`|`)已用 `"..."` 包裹237- [ ] 长文本用 `<br/>` 换行238- [ ] 箭头语法正确:`-->`、`---`、`-.->`、`==>`、`--x`、`--o`(flowchart);`->>`、`-->>`、`-x`、`--x`(sequence)239- [ ] subgraph 有 `end` 闭合240- [ ] stateDiagram 用 `[*]` 表示开始/结束状态241242### 内容质量243- [ ] 节点数量合理(flowchart ≤ 30 节点,sequence ≤ 10 参与者,class ≤ 20 类)244- [ ] label 文本清晰简洁245- [ ] 箭头方向与数据流方向一致246- [ ] 在 GitHub Markdown 预览或 mermaid.live 中无报错247248### 布局质量249- [ ] 无节点重叠(必要时调整方向 TD/LR 或用 subgraph 分组)250- [ ] 无箭头交叉(必要时用 subgraph 隔离)251- [ ] 长链路拆分为多行/subgraph,避免单行长链252253如以上任一项失败,参考 `references/layout-best-practices.md` 和 `references/common-pitfalls.md`。254255## 相关技能256257本 skill 是 diagram 技能家族的一员,按输出格式分工,4 个 skill 互补但不重叠:258259| Skill | 输出形态 | 主用途 |260|---|---|---|261| `diagram-mermaid`(本 skill) | Mermaid 代码块(内联 Markdown) | GitHub README/issue/PR 嵌入,零依赖,GitHub 直接渲染,11 种基础图类型 |262| `diagram-plantuml` | PlantUML 代码块(内联 Markdown) | UML/云架构/网络拓扑/安全/ArchiMate/BPMN/数据管道/IoT/思维导图,9500+ 图标库 |263| `diagram-html` | 独立 HTML 文件 | 可分享的成品图,浏览器打开即用,双主题切换 + 浏览器导出菜单 |264| `diagram-image` | SVG + PNG 文件 | 命令行直接产出图片文件,适合 CI/批处理/嵌入不支持 SVG 的环境 |265266**选用决策**:267- 在 Markdown 里嵌入图、要源码可读、可 diff → 本 skill(`diagram-mermaid`)或 `diagram-plantuml`268- 要可交互的 HTML 成品、双主题切换、点按钮导出 → `diagram-html`269- 要命令行直接出 SVG/PNG 文件、CI/批处理 → `diagram-image`270271**默认推荐**:272- 简单 flowchart / sequence / state / ER / mindmap → 优先本 skill(最轻量,GitHub 原生渲染)273- 需要 AWS/Azure/GCP/Cisco 等专业图标或 BPMN/ArchiMate/IoT/UML 全家桶 → 优先 `diagram-plantuml`274- 要可分享的 HTML 成品 → `diagram-html`275- 要 SVG/PNG 文件 → `diagram-image`