mermaid-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMermaid Diagrams
Mermaid图表
Generate text files and export to PNG/SVG/PDF using (local) or Kroki API (no install).
.mmdmmdcKey advantage: Text-based syntax with fully automatic layout — no x/y coordinates needed.
使用本地或无需安装的Kroki API生成文本文件并导出为PNG/SVG/PDF格式。
mmdc.mmd核心优势: 基于文本的语法搭配全自动布局——无需设置x/y坐标。
When to use / when NOT to use
适用场景/不适用场景
Use this skill for: diagrams-as-code with automatic layout (flowchart, sequence, class, state, ER, gantt, mindmap, architecture) — text source that lives in git and embeds in Markdown.
Do NOT use it — route elsewhere — for:
- Pixel-precise placement, custom layout, branded icons, or heavy styling → drawio.
- A hand-drawn / sketchy aesthetic → excalidraw or tldraw.
- A freeform whiteboard or freehand strokes → tldraw.
- Strict, conventional UML notation → plantuml.
本技能适用于: 具备自动布局的代码化图表(流程图、时序图、类图、状态图、ER图、甘特图、思维导图、架构图)——文本源码可存入git并嵌入Markdown。
请勿使用本技能,应转用其他工具的场景:
- 像素级精确布局、自定义排版、品牌图标或复杂样式 → drawio。
- 手绘/草图风格 → excalidraw 或 tldraw。
- 自由白板或手绘笔触 → tldraw。
- 严格规范的UML符号 → plantuml。
Prerequisites
前提条件
Option A: Local (mmdc) — also needs a headless Chrome (mmdc renders via Puppeteer)
bash
npm install -g @mermaid-js/mermaid-cli
npx puppeteer browsers install chrome-headless-shell # required — mmdc has no bundled browser
mmdc --versionsucceeds even with no Chrome installed, but every export then fails withmmdc --version. Install the browser above (or setCould not find Chrometo a system Chrome). If you can't, use Kroki (Option B) — it needs no browser.PUPPETEER_EXECUTABLE_PATH
CI / Docker: mmdc crashes with in containers. Pass the bundled puppeteer config (see ):
Running as root without --no-sandboxscripts/puppeteer-config.jsonbash
mmdc -p scripts/puppeteer-config.json -i diagram.mmd -o diagram.pngOption B: Kroki API (no install)
bash
curl --version # Just need curl选项A:本地使用(mmdc) —— 还需要无头Chrome浏览器(mmdc通过Puppeteer渲染)
bash
npm install -g @mermaid-js/mermaid-cli
npx puppeteer browsers install chrome-headless-shell # 必须安装——mmdc未捆绑浏览器
mmdc --version即使未安装Chrome,也会执行成功,但后续所有导出操作都会因mmdc --version失败。请安装上述浏览器(或设置Could not find Chrome指向系统已有的Chrome)。若无法安装,可使用选项B的Kroki——无需浏览器。PUPPETEER_EXECUTABLE_PATH
CI/ Docker环境: 在容器中以root身份运行mmdc会因崩溃。请传入捆绑的puppeteer配置(见):
Running as root without --no-sandboxscripts/puppeteer-config.jsonbash
mmdc -p scripts/puppeteer-config.json -i diagram.mmd -o diagram.png选项B:Kroki API(无需安装)
bash
curl --version # 仅需curlWorkflow
工作流程
- Check deps — validate via Kroki (needs only ); check
curland a headless Chrome only when local export (PNG quality / PDF) is wantedmmdc --version - Pick diagram type — choose from table below
- Generate — write file to disk
.mmd - Validate — Kroki-first (see Validation; REQUIRED before export)
- Export — use or Kroki API to produce PNG/SVG/PDF
mmdc - Self-check (vision) — read the exported PNG and fix readability/layout defects that automatic layout can't prevent (clipped labels, cramped density, wrong orientation), then re-validate + re-export. Max 2 rounds; skip if no vision. See Self-Check (vision) below.
- Review loop — show the image to the user, apply the minimal edit per request, re-export until approved (5-round safety valve). See Review Loop below.
.mmd - Report — tell user the output file paths
- 检查依赖 —— 通过Kroki验证(仅需);仅当需要本地导出(PNG质量/PDF格式)时,检查
curl是否可用以及有无无头Chrome浏览器mmdc --version - 选择图表类型 —— 从下方表格中选择
- 生成图表 —— 将文件写入磁盘
.mmd - 验证 —— 优先使用Kroki(见验证部分;导出前必须执行)
- 导出 —— 使用或Kroki API生成PNG/SVG/PDF
mmdc - 自我检查(视觉校验) —— 查看导出的PNG,修复自动布局无法避免的可读性/布局缺陷(标签裁剪、密度拥挤、方向错误),然后重新验证并导出。最多2轮检查;若无视觉能力则跳过。详见下方自我检查(视觉校验)
- 审核循环 —— 向用户展示图片,根据请求对做最小修改,重新导出直至用户认可(最多5轮,超过则触发安全机制)。详见下方审核循环
.mmd - 反馈结果 —— 告知用户输出文件路径
Validation (Required)
验证(必填步骤)
NEVER export a diagram without validating first.
Prefer Kroki for validation — it needs no browser and sidesteps the trap entirely. Use for validation only when offline.
Could not find Chromemmdcbash
undefined导出图表前必须先验证。
优先使用Kroki进行验证——无需浏览器,完全规避问题。仅当离线时才使用验证。
Could not find Chromemmdcbash
undefinedValidate with Kroki (preferred — no browser needed)
使用Kroki验证(优先选择——无需浏览器)
curl -s -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/svg -o /tmp/test.svg && echo "Valid" || echo "Invalid"
curl -s -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/svg -o /tmp/test.svg && echo "Valid" || echo "Invalid"
Validate with mmdc (offline fallback — requires headless Chrome)
使用mmdc验证(离线备选——需要无头Chrome)
mmdc -i diagram.mmd -o /tmp/test.svg 2>&1
mmdc -i diagram.mmd -o /tmp/test.svg 2>&1
If error, fix the .mmd file and validate again
若报错,修复.mmd文件后重新验证
Only proceed to export after validation passes
仅在验证通过后才能进行导出
Common validation errors:
- Missing quotes around labels with special characters
- Wrong arrow syntax (use `->>` for sequence, `-->` for flowchart)
- Undeclared participants in sequence diagrams
> A `Could not find Chrome` (or puppeteer) error from `mmdc` is a **setup** problem, not a diagram error — the `.mmd` may be perfectly valid. Validate via Kroki instead of "fixing" correct syntax.
常见验证错误:
- 含特殊字符的标签未加引号
- 箭头语法错误(时序图用`->>`,流程图用`-->`)
- 时序图中未声明参与者
> `mmdc`返回的`Could not find Chrome`(或puppeteer)错误是**环境配置**问题,而非图表语法错误——`.mmd`可能完全合法。请改用Kroki验证,而非修改正确的语法。Self-Check (vision)
自我检查(视觉校验)
Zero-cost pre-check: before spending a vision call, read the PNG dimensions ( on macOS, elsewhere). An extreme aspect ratio (longer side > 8x the shorter) is a "wrong orientation" defect — fix it without vision.
sips -g pixelWidth -g pixelHeight diagram.pngfile diagram.pngValidation (above) only proves the syntax is legal — it says nothing about whether the rendered diagram is readable. After exporting, use the agent's vision capability to read the PNG and catch what automatic layout can't prevent. Mermaid positions everything itself, so the failures here are about content and readability, not overlaps:
| Check | What to look for | Fix |
|---|---|---|
| Label truncation | Node / edge text clipped or cut off | Shorten the label, or wrap it with |
| Cramped, unreadable density | Too many nodes crammed together; tangled lines | Flip direction ( |
| Wrong orientation / aspect | Diagram far too wide or too tall to read | Change |
| Edge spaghetti | Many edges crossing, hard to follow | Reorder node declarations so connected nodes sit adjacent; group with |
| Wrong diagram type | Type doesn't suit the content (e.g. flowchart for a timeline) | Switch type ( |
| Low contrast | Text blends into the node fill | Adjust |
- Max 2 self-check rounds — if issues remain after 2 fixes, show the user anyway.
- Re-validate (syntax) and re-export after every fix.
- If vision is unavailable, skip self-check and show the PNG directly.
零成本预检查: 在调用视觉能力前,先查看PNG尺寸(macOS用,其他系统用)。极端宽高比(长边大于短边8倍以上)属于“方向错误”缺陷——无需视觉能力即可修复。
sips -g pixelWidth -g pixelHeight diagram.pngfile diagram.png上述验证仅能证明语法合法,但无法保证渲染后的图表可读。导出后,使用智能体的视觉能力查看PNG,捕捉自动布局无法避免的问题。Mermaid会自动定位所有元素,因此此处的问题均与内容和可读性有关,不涉及元素重叠:
| 检查项 | 检查内容 | 修复方法 |
|---|---|---|
| 标签截断 | 节点/边的文本被裁剪或截断 | 缩短标签,或使用 |
| 密度拥挤、可读性差 | 节点过多过于密集;线条混乱 | 翻转方向( |
| 方向/宽高比错误 | 图表过宽或过高,难以阅读 | 修改 |
| 线条混乱 | 大量线条交叉,难以追踪 | 调整节点声明顺序,使关联节点相邻;用 |
| 图表类型错误 | 类型不匹配内容(如用流程图展示时间线) | 切换类型( |
| 对比度低 | 文字与节点背景融合 | 调整 |
- 最多2轮自我检查——若2次修复后仍有问题,直接向用户展示即可。
- 每次修复后都要重新验证(语法)并重新导出。
- 若无视觉能力,跳过自我检查,直接展示PNG。
Review Loop
审核循环
After self-check, show the exported image and collect feedback. Apply the minimal edit for each request, then re-validate and re-export:
.mmd| User request | Edit action |
|---|---|
| Change a label | Edit the node / edge text in the |
| Add / remove a node or edge | Add or delete the matching line |
| Change a color | Add / adjust a |
| Change layout direction | Swap |
| Restructure / group | Wrap related nodes in a |
- Overwrite the same /
diagram.mmdeach round — don't creatediagram.png,v1, …v2 - Safety valve: after 5 rounds, generate a mermaid.live handoff link (see next section) so the user fine-tunes the current diagram in the browser.
自我检查完成后,向用户展示导出的图片并收集反馈。针对每个请求对做最小修改,然后重新验证并导出:
.mmd| 用户请求 | 修改操作 |
|---|---|
| 修改标签 | 编辑 |
| 添加/删除节点或边 | 添加或删除对应的行 |
| 修改颜色 | 添加/调整 |
| 修改布局方向 | 切换 |
| 重构/分组 | 将相关节点包裹在 |
- 每次修改都覆盖原有的/
diagram.mmd——不要创建diagram.png、v1等版本。v2 - 安全机制: 超过5轮后,生成mermaid.live跳转链接(见下一节),让用户在浏览器中自行微调当前图表。
Mermaid.live Handoff
Mermaid.live 移交
When the review loop ends (approved or safety-valve), generate a one-click link that opens the current in the mermaid.live editor for interactive fine-tuning:
.mmdbash
python3 scripts/mermaid_live_link.py diagram.mmd当审核循环结束(用户认可或触发安全机制),生成一键链接,将当前打开在mermaid.live编辑器中进行交互式微调:
.mmdbash
python3 scripts/mermaid_live_link.py diagram.mmd
Pure `zlib` + `base64` (raw-deflate + URL-safe base64, same encoding mermaid.live uses), works offline. The script self-verifies with a round-trip decode before printing.
采用纯`zlib`+`base64`编码(原始压缩+URL安全base64,与mermaid.live使用的编码一致),离线可用。脚本会先进行往返解码验证,再输出链接。Batch Mode (Repo Survey)
批量模式(仓库调研)
When the user asks to diagram a whole repo ("survey this codebase", "map the architecture"):
- Scan — locate schemas (SQL / ORM models), API routes, and state machines / lifecycle enums in the codebase
- Propose — one diagram per finding: ER for the schema, sequence for a main API flow, state for a lifecycle, flowchart for overall service layout; confirm the set with the user before generating
- Generate + export — run the standard workflow per diagram into one output folder
- Overview page — inline every exported SVG into a single self-contained (title + one section per diagram, no CDN), so the user opens one file
index.html
Same validation and self-check rules apply to every diagram in the batch.
当用户要求为整个仓库生成图表(如“调研此代码库”、“绘制架构图”):
- 扫描 —— 在代码库中定位模式文件(SQL/ORM模型)、API路由、状态机/生命周期枚举
- 提议 —— 为每个发现生成一个图表:模式用ER图,主API流用时序图,生命周期用状态图,整体服务布局用流程图;生成前先与用户确认图表集合
- 生成+导出 —— 对每个图表执行标准工作流程,输出到同一个文件夹
- 概览页面 —— 将所有导出的SVG嵌入到单个自包含的中(含标题和每个图表的章节,无需CDN),用户只需打开一个文件即可查看
index.html
批量模式下的每个图表都需遵循相同的验证和自我检查规则。
Diagram Types
图表类型
| Type | Keyword | Use for |
|---|---|---|
| Flowchart | | processes, pipelines, decisions |
| Sequence | | API calls, message passing |
| Class | | OOP models, data structures |
| ER | | database schemas |
| State | | state machines, lifecycle |
| Gantt | | project timelines |
| Pie | | proportions |
| Git Graph | | branch strategies |
| C4 Context | | high-level system context |
| Architecture | | cloud / CI/CD service layouts |
| Mind Map | | topic breakdowns |
| User Journey | | user-experience flows |
| Use Case | | actor–system interactions (UML) |
| Cynefin | | sense-making / complexity domains |
| Event Modeling | | event-driven system timelines |
| Tree View | | file / directory hierarchies |
| Wardley Maps | | business strategy / value chains |
| 类型 | 关键字 | 适用场景 |
|---|---|---|
| 流程图 | | 流程、管道、决策逻辑 |
| 时序图 | | API调用、消息传递 |
| 类图 | | OOP模型、数据结构 |
| ER图 | | 数据库模式 |
| 状态图 | | 状态机、生命周期 |
| 甘特图 | | 项目时间线 |
| 饼图 | | 比例展示 |
| Git图 | | 分支策略 |
| C4上下文图 | | 高层系统上下文 |
| 架构图 | | 云/CI/CD服务布局 |
| 思维导图 | | 主题拆解 |
| 用户旅程图 | | 用户体验流程 |
| 用例图 | | 参与者-系统交互(UML) |
| Cynefin图 | | 情境感知/复杂度领域 |
| 事件建模图 | | 事件驱动系统时间线 |
| 树视图 | | 文件/目录层级 |
| Wardley图 | | 业务策略/价值链 |
Syntax Reference
语法参考
Flowchart: See reference/FLOWCHART.md
Sequence: See reference/SEQUENCE.md
Class & ER: See reference/CLASS-ER.md
Architecture: See reference/ARCHITECTURE.md
Use Case: See reference/USECASE.md
Other types: See reference/OTHER-TYPES.md
Themes & styling: See reference/THEMES.md
流程图:详见reference/FLOWCHART.md
时序图:详见reference/SEQUENCE.md
类图与ER图:详见reference/CLASS-ER.md
架构图:详见reference/ARCHITECTURE.md
用例图:详见reference/USECASE.md
其他类型:详见reference/OTHER-TYPES.md
主题与样式:详见reference/THEMES.md
Examples
示例
See reference/EXAMPLES.md for four worked examples (JWT auth sequence, microservices architecture, order state machine, cloud architecture) — each with the user prompt, the generated , and the output files.
.mmd详见reference/EXAMPLES.md中的4个完整示例(JWT认证时序图、微服务架构图、订单状态机、云架构图)——每个示例都包含用户提示、生成的文件和输出文件。
.mmdExport Commands
导出命令
Option 1: Local Export (mmdc)
选项1:本地导出(mmdc)
Requires installed locally. Best for offline use.
mmdcbash
undefined需要本地安装。适合离线使用。
mmdcbash
undefinedPNG (recommended: 2048px wide, white background)
PNG(推荐:宽2048px,白色背景)
mmdc -i diagram.mmd -o diagram.png -w 2048 --backgroundColor white
mmdc -i diagram.mmd -o diagram.png -w 2048 --backgroundColor white
PNG with theme — valid -t values: default | dark | neutral | forest
带主题的PNG —— 有效的-t值:default | dark | neutral | forest
(base
is NOT a valid -t value; it only works inside a %%{init: {'theme':'base'}}%% directive)
base—
mmdc -i diagram.mmd -o diagram.png -w 2048 --backgroundColor white --theme neutral
#(不是有效的-t值;仅在%%{init: {'theme':'base'}}%%指令中生效)
mmdc -i diagram.mmd -o diagram.png -w 2048 --backgroundColor white --theme neutral
baseSVG
SVG
mmdc -i diagram.mmd -o diagram.svg
mmdc -i diagram.mmd -o diagram.svg
mmdc -i diagram.mmd -o diagram.pdf
undefinedmmdc -i diagram.mmd -o diagram.pdf
undefinedOption 2: Kroki API (No Install Required)
选项2:Kroki API(无需安装)
Use Kroki when is not available. No local dependencies needed.
mmdcbash
undefined当不可用时使用Kroki。无需本地依赖。
mmdcbash
undefinedSVG via Kroki
通过Kroki导出SVG
curl -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/svg -o diagram.svg
curl -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/svg -o diagram.svg
PNG via Kroki
通过Kroki导出PNG
curl -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/png -o diagram.png
curl -X POST -H "Content-Type: text/plain" --data-binary @diagram.mmd https://kroki.io/mermaid/png -o diagram.png
PDF is NOT supported by Kroki for Mermaid — POSTing to /mermaid/pdf returns
Kroki不支持Mermaid导出PDF——向/mermaid/pdf发送POST请求会返回
HTTP 400 ("Unsupported output format: pdf for mermaid. Must be one of png or svg").
HTTP 400错误("Unsupported output format: pdf for mermaid. Must be one of png or svg")。
For PDF, use the local mmdc path instead: mmdc -i diagram.mmd -o diagram.pdf
如需PDF,请使用本地mmdc:mmdc -i diagram.mmd -o diagram.pdf
**Kroki advantages:**
- No local installation required
- Works on any system with `curl`
- Supports 20+ diagram types (PlantUML, GraphViz, D2, etc.)
**When to use Kroki:**
- `mmdc` installation fails
- Quick one-off diagrams
- CI/CD pipelines without Node.js
**Kroki优势:**
- 无需本地安装
- 任何有`curl`的系统都可使用
- 支持20+种图表类型(PlantUML、GraphViz、D2等)
**Kroki适用场景:**
- `mmdc`安装失败
- 快速生成一次性图表
- 无Node.js的CI/CD流水线Common Mistakes
常见问题
| Mistake | Fix |
|---|---|
| |
| Install the headless browser: |
| Kroki PDF fails with HTTP 400 | Kroki does PNG/SVG only for Mermaid; use local |
Valid diagram reported "invalid" by | The error is a Chrome/puppeteer setup failure, not a syntax error — don't rewrite correct |
| Wrong arrow in sequence | Use |
| Special chars in label | Wrap in quotes: |
| Blank/small output | Add |
| Participant order wrong | Declare |
| Subgraph name with spaces | Wrap in quotes: |
| 问题 | 解决方法 |
|---|---|
找不到 | 执行 |
| 安装无头浏览器: |
| Kroki导出PDF返回HTTP 400错误 | Kroki仅支持Mermaid导出PNG/SVG;如需PDF请使用本地 |
合法图表被 | 该错误是Chrome/puppeteer配置问题,而非语法错误——不要修改正确的 |
| 时序图箭头错误 | 请求用 |
| 标签含特殊字符 | 用引号包裹: |
| 输出空白/尺寸过小 | 添加 |
| 参与者顺序错误 | 在顶部显式声明 |
| 子图名称含空格 | 用引号包裹: |