mermaid-tools
Extracts Mermaid diagrams from markdown files and generates high-quality PNG images using bundled scripts. Activates when working with Mermaid diagrams, converting diagrams to PNG, extracting diagrams from markdown, or processing markdown files with embedded Mermaid code.
1How It Works & Mechanism
Relies on two bundled scripts in sequence: extract_diagrams.py uses regex to extract ```mermaid code blocks from markdown and numbers/names them, then extract-and-generate.sh invokes mermaid-cli (mmdc) file-by-file to render PNGs.
mermaid_pattern = r'```mermaid\n(.*?)\n```'
The rendering engine is mermaid-cli + local Chrome: the script sets PUPPETEER_EXECUTABLE_PATH to /usr/bin/google-chrome-stable and passes puppeteer-config.json alongside -w/-H/-s dimensions and scale parameters.
PUPPETEER_EXECUTABLE_PATH="$CHROME_PATH" mmdc \ -i "$mmd_file" \ -o "$diagram.png" \ --puppeteerConfigFile "$CONFIG_FILE" \ -w "$width" \ -H "$height" \ -s "$SCALE_FACTOR"
High-resolution output is controlled via environment variable overrides: MERMAID_WIDTH/HEIGHT/SCALE default to 1200×800×2, and can be increased to 2400×1800×4 or SCALE=5 for presentations or print.
MERMAID_WIDTH=2400 MERMAID_HEIGHT=1800 MERMAID_SCALE=4 ./extract-and-generate.sh "<markdown_file>" "<output_directory>"
Smart sizing infers dimensions based on diagram name keywords: timeline/gantt are widened and flattened, architecture/system/caching are enlarged, workflow/sequence/api are widened, and default is 1200×800.
- **Timeline/Gantt**: 2400×400 (wide and short) - **Architecture/System/Caching**: 2400×1600 (large and detailed)
Pre-flight dependency checks are built into the flow: if mmdc, google-chrome-stable, or python3 is missing, it prompts to check references/setup_and_troubleshooting.md (including Chrome installation and dependency lists for WSL2).
1. **mermaid-cli**: `mmdc --version` 2. **Google Chrome**: `google-chrome-stable --version` 3. **Python 3**: `python3 --version`
Execution convention is 'run from the scripts/ directory': the script locates files via ${CLAUDE_SKILL_DIR} and depends on relative paths to extract_diagrams.py and puppeteer-config.json in the same directory.
cd "${CLAUDE_SKILL_DIR}/scripts" ./extract-and-generate.sh "<markdown_file>" "<output_directory>"
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | mermaid-cli (mmdc) |
| cli | Google Chrome(/usr/bin/google-chrome-stable,headless 渲染) |
| cli | python3(extract_diagrams.py) |
| network | dl.google.com(setup 参考文档:加 Chrome apt 源并 sudo apt install,一次性安装) |
| cli | file / sort -V / stat / identify(可选)(bash 工具链) |
4Recommended Governance & Evidence
- scripts/: Executes local scripts or commands: bash scripts/extract-and-generate.sh (set -e, local)
- 网络端点: Declares external network access: None at runtime; setup reference doc includes one-off installation actions: wget dl.google.com public key + sudo apt install google-chrome-stable
- 本地工作目录: File I/O and artifact output: Writes .mmd/.png to the output directory (default: <markdown_file_dir>/diagrams); mkdir -p output directory
5Second-Pass Verification
- [ok] 脚本三件套自包含 — extract-and-generate.sh / extract_diagrams.py / puppeteer-config.json 均在 scripts/ 且被 SKILL 逐一引用。
- [ok] mmdc 本地渲染 + PUPPETEER_EXECUTABLE_PATH — 脚本内逐字存在 'PUPPETEER_EXECUTABLE_PATH="$CHROME_PATH" mmdc -i ... --puppeteerConfigFile' 调用。
- [ok] smart sizing 逻辑一致 — SKILL.md 尺寸表与脚本 [[ "$diagram" =~ timeline|gantt ]] 等分支对应。
- [ok] PNG 校验存在 — 脚本 'test -s ... && file ... | grep -q "PNG image"' + identify 尺寸回退分支属实。
- [ok] 运行期无网络 — 三个脚本全文无 curl/wget/requests/http 调用;唯一网络在 setup 参考文档的 dl.google.com apt 安装(sudo,用户主动执行)。
- [ok] 无凭证读取 — 仅读 MERMAID_WIDTH/HEIGHT/SCALE/CLAUDE_SKILL_DIR 等路径/尺寸变量,无 token/secret/key 环境读取。
- [ok] --no-sandbox 浏览器参数 — puppeteer-config.json args 含 --no-sandbox/--disable-setuid-sandbox 等;渲染目标为本地生成的图表 HTML,输入为工作区 markdown。
- [ok] 元数据 — GitHub API:MIT / 1385 stars / pushed 2026-09-09T12:33:29Z;本地 HEAD==pin d5c4678。
6Verdict & Scenarios
3b3522624bae3e65…