📊
简介
通过文本语法快速生成专业图表,支持流程图、时序图、架构图等12种类型,无需设计软件。
原始描述
Generate and render diagrams from Mermaid syntax into PNG, SVG, or PDF using customizable themes and various diagram types.
中文使用说明 来自收录源
核心用法
Mermaid Diagrams Skill 将文本描述转换为可视化图表,基于开源 Mermaid.js 库。用户以简洁语法编写 .mmd 文件,通过 CLI 工具 mmdc 渲染为 PNG/SVG/PDF 格式。
典型工作流程:
- 分析用户需求,选择最优图表类型(流程图、时序图、类图、状态图、甘特图等12种)
- 编写 Mermaid 语法文件至临时目录
- 执行渲染命令,指定主题、背景、缩放比例
- 返回图像并清理临时文件
关键参数:-t dark 暗色主题、-b transparent 透明背景、-s 2 高清缩放,适配技术文档和演示场景。
显著优点
- 纯文本驱动:版本可控、Diff 友好、协作效率高
- 类型丰富:覆盖软件架构、数据库设计、项目管理、Git 流程等全场景
- 零设计门槛:自动布局引擎处理对齐、间距、层级关系
- 输出灵活:支持透明背景 PNG、矢量 SVG、打印级 PDF
- 集成友好:CI/CD 可自动化生成文档图表
潜在局限
- 依赖 Node.js 环境:需全局安装
@mermaid-js/mermaid-cli,约 200MB+ 依赖体积 - 复杂布局受限:自动布局在超大规模图表(50+ 节点)可能出现重叠
- 样式定制深度有限:高级主题需手写 JSON 配置,学习成本高于可视化工具
- 字体渲染差异:跨平台可能因字体缺失导致布局偏移
- 实时预览缺失:需渲染后才能查看效果,迭代效率低于即时编辑工具
适合人群
- 技术写作者:快速为 README、技术文档、API 文档配图
- 软件架构师:绘制系统架构图、数据流图、部署拓扑
- 项目经理:生成甘特图、流程图用于进度汇报
- 开发者:在代码审查中可视化调用链路、状态转换
常规风险
- 命令注入风险:若用户输入未过滤直接拼接至 shell 命令,存在潜在安全隐患(当前实现未展示输入校验逻辑)
- 临时文件泄露:
/tmp目录文件若未清理可能残留敏感图表内容 - 依赖供应链:npm 包的完整性依赖官方源安全性
- SVG XSS 载体:输出 SVG 格式时若包含用户可控脚本标签,可能成为攻击向量(需确保渲染配置禁用脚本)
技能正文
Mermaid Diagrams Skill
Generate diagrams from text using Mermaid.
Requirements
@mermaid-js/mermaid-cli(npm install -g @mermaid-js/mermaid-cli)
Usage
- Write Mermaid syntax to a
.mmdfile - Run
mmdcto render PNG/SVG - Return the image to the user
Basic Command
mmdc -i input.mmd -o output.png -t dark -b transparent
Options
| Flag | Description |
|---|---|
-i |
Input .mmd file |
-o |
Output file (.png, .svg, .pdf) |
-t |
Theme: default, dark, forest, neutral |
-b |
Background color (transparent, hex) |
-w |
Width in pixels (default: 800) |
-H |
Height in pixels |
-s |
Scale factor (default: 1, use 2-3 for high-res) |
-c |
Config JSON file for advanced theming |
Supported Diagram Types
- Flowchart:
graph TD/graph LR - Sequence:
sequenceDiagram - Class:
classDiagram - State:
stateDiagram-v2 - ER:
erDiagram - Gantt:
gantt - Pie:
pie - Mindmap:
mindmap - Timeline:
timeline - Git graph:
gitGraph - Quadrant:
quadrantChart - Block:
block-beta
Workflow
- Determine the best diagram type for what the user wants
- Write the
.mmdfile to/tmp/mermaid-<name>.mmd - Render:
mmdc -i /tmp/mermaid-<name>.mmd -o /tmp/mermaid-<name>.png -t dark -b transparent -s 2 - Show the image to the user via
Readtool - Clean up temp files if not needed
Example: Architecture Diagram
graph TD
A[Client] -->|HTTPS| B[API Gateway]
B --> C[Auth Service]
B --> D[App Server]
D --> E[(PostgreSQL)]
D --> F[(Redis Cache)]
Example: Sequence Diagram
sequenceDiagram
participant U as User
participant A as API
participant DB as Database
U->>A: POST /login
A->>DB: Validate credentials
DB-->>A: User record
A-->>U: JWT token
Tips
- Use
graph LRfor left-to-right flow,graph TDfor top-down - Keep node labels short — detail goes in tooltips or notes
- Use subgraphs to group related components
- For dark backgrounds, use
-t dark -b transparent - Scale
-s 2or-s 3for sharp images on retina displays - Always use
pty: falsewhen callingmmdc
Theming
For custom colors, create a config JSON:
{
"theme": "base",
"themeVariables": {
"primaryColor": "#f59e0b",
"primaryTextColor": "#1a1a1a",
"primaryBorderColor": "#d97706",
"lineColor": "#6b7280",
"secondaryColor": "#10b981",
"tertiaryColor": "#3b82f6"
}
}
Then: mmdc -i input.mmd -o output.png -c config.json
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。