
Mermaid CLI
CLIMermaid 官方命令行工具 mmdc:把流程图、时序图等 Mermaid 定义渲染成 SVG、PNG 或 PDF,也能把 Markdown 里的 mermaid 代码块批量转成图片。需要 Node.js。
mermaid-cli
This is a command-line interface (CLI) for mermaid. It takes a mermaid definition file as input and generates an SVG/PNG/PDF file as output. There's basic support for converting mermaid code blocks embedded within Markdown files.
Installation
npm install -g @mermaid-js/mermaid-cli
Usage
Convert Mermaid mmd Diagram File To SVG
mmdc -i input.mmd -o output.svg
Note
See Alternative installations if you don't want to install the package globally.
Locate how to call the mmdc executable for your preferred method i.e. Docker, Yarn, NPM, local install, etc.
Examples
Create A PNG With A Dark Theme And Transparent Background
mmdc -i input.mmd -o output.png -t dark -b transparent
Adding custom icon packs to Mermaid
mermaid-cli supports two different ways to add custom icon packs to mermaid:
- Icon packs installed via
npm install, e.g. @iconify-json/logos. These can be used via the--iconPacks @iconify-json/logos. - Icon packs accessible via URI. These can either be URLs or local
file://URIs to local files. For example, to download the above @iconify-json/logos package from a CDN like jsDelivr, you can do--iconPacksAndUrls logos#https://cdn.jsdelivr.net/npm/@iconify-json/logos@1.2.14/icons.json.
Animating an SVG file with custom CSS
The --cssFile option can be used to inline some custom CSS. It's passed to Mermaid's themeCSS config option.
Please see ./test-positive/flowchart1.css for an example of a CSS file that has animations.
Warning: Inline CSS files may be blocked by your browser, depending on the HTTP Content-Security-Policy header of the website that hosts your SVG.
mmdc --input test-positive/flowchart1.mmd --cssFile test-positive/flowchart1.css -o docs/animated-flowchart.svg
Example output: docs/animated-flowchart.svg
Selecting the size of the created file
The --size value can be used to attempt to create a PNG of the given size,
by setting a max-width on the SVG.
For narrow diagrams, this would decide set the max height, and for wide diagrams, this will set the max width.
For example, the following command will create a PNG of height 4000px:
mmdc --input test-positive/flowchart1.mmd --size 4000 -o output.png
![Note]
This may not work with all diagram types and/or may require
useMaxWidth: trueto be set in the diagram's config.
Transform a markdown file with mermaid diagrams
mmdc -i readme.template.md -o readme.md
This command transforms a markdown file itself. The mermaid-cli will find the mermaid diagrams, create SVG files from them and refer to those in the markdown output.
This:
### Some markdown
```mermaid
graph
[....]
```
### Some more markdown
```mermaid
sequenceDiagram
[....]
```
### Mermaid with custom title/desc
```mermaid
graph
accTitle: My title here
accDescr: My description here
A-->B
```
Becomes:
### Some markdown

### Some more markdown

### Mermaid with custom title/desc

Piping from stdin
You can easily pipe input from stdin. This example shows how to use a heredoc to send a diagram as stdin to mermaid-cli (mmdc).
cat << EOF | mmdc --input -
graph TD
A[Client] --> B[Load Balancer]
EOF
See All Available Options
mmdc -h
Alternative installations
Use Docker/Podman:
docker pull minlag/mermaid-cli
or pull from Github Container Registry
docker pull ghcr.io/mermaid-js/mermaid-cli/mermaid-cli
or e.g. version 8.8.0
docker pull minlag/mermaid-cli:8.8.0
The container looks for input files in /data. So for example, if you have a
diagram defined on your system in /path/to/diagrams/diagram.mmd, you can use
the container to generate an SVG file as follows:
docker run --rm -u `id -u`:`id -g` -v /path/to/diagrams:/data minlag/mermaid-cli -i diagram.mmd
Or, if using Podman, instead do:
podman run --userns keep-id --user ${UID} --rm -v /path/to/diagrams:/data:z ghcr.io/mermaid-js/mermaid-cli/mermaid-cli -i diagram.mmd
The key differences in the podman command versus the docker command are:
- The addition of the
--userns keep-idargument. This allows the container to keep the same UID as the current user's UID in the container namespace instead of mapping to a subuid. To learn more, see theuserns=modedocumentation. - The addition of
:zto the end of the volume mapping. This instructs podman to relabel the files in the volume with the SELinux labelcontainer_file_t, which allows processes in the container to access the files. See the "Labeling Volume Mounts" section of podman-run (1) for more info.
In previous version, the input files were mounted in /home/mermaidcli. You can
restore this behaviour with the --workdir option:
docker run [...] --workdir=/home/mermaidcli minlag/mermaid-cli [...]
Use Node.JS API
It's possible to call mermaid-cli via a Node.JS API.
Please be aware that the NodeJS API is not covered by semver, as mermaid-cli follows
mermaid's versioning.
import { run } from "@mermaid-js/mermaid-cli";
await run(
"input.mmd",
"output.svg", // {optional options},
);
Install locally
Some people are having issues installing this tool globally. Installing it locally is an alternative solution:
npm install @mermaid-js/mermaid-cli
./node_modules/.bin/mmdc -h
Run with npx
npx is installed by default with NPM. It
downloads and runs commands at the same time. To use Mermaid CLI with npx, you
need to use the -p flag because the package name is different than the command
it installs (mmdc). npx -p @mermaid-js/mermaid-cli mmdc -h
Install with brew
Warning
This method of installation is no longer supported. For more details, see the discussion. An old version of mermaid-cli can be installed with brew.
brew install mermaid-cli
Known issues
- Linux sandbox issue
- Docker permission denied issue
- How to setup up mermaid to use already installed chromium?
For contributors
Contributions are welcome. See the contribution guide.
相關資源

Archify
Skill把自然语言描述、Mermaid 或真实代码仓库转成可交互的单文件 HTML 图表,覆盖架构图、流程图、时序图、数据流与状态机,生成后自动校验,可导出 PNG/SVG。
tt-a1i/archify84.4k
visual-explainer
Skill把架构说明、diff 评审、计划审查、数据对比等内容生成自包含的 HTML 页面或幻灯片,支持 Mermaid 图与深浅色主题,替代终端里的 ASCII 图表。
nicobailon/visual-explainer10.5k
Excalidraw MCP Server
MCP给 agent 一块本地实时 Excalidraw 画布:按元素增删改绘制架构图与流程图,能截图自查、转换 Mermaid、导出 .excalidraw 文件。本地运行无需 API Key,同时提供 CLI 与 Agent Skill。
yctimlin/mcp_excalidraw2.5k
MCP Mermaid
MCP用 Mermaid 语法生成流程图、时序图、类图等各类图表,可配置主题与背景色,导出 SVG、PNG 文件或 mermaid.ink 分享链接,并校验语法便于模型多轮修正。
hustcc/mcp-mermaid644
D2
CLI声明式图表脚本语言:用简洁文本描述架构图、流程图、时序图并自动布局,d2 命令可渲染并导出 SVG、PNG、PDF 或 PPTX,便于 agent 生成可版本管理的图。
d2lang/d225.6k
Diagram Design
Skill生成自包含 HTML+SVG 的杂志风图表:架构图、流程图、时序图、泳道图、甘特图等数十种类型,可读取网站配色匹配品牌,支持导入 Mermaid、draw.io 与 Excalidraw 源文件重绘。
cathrynlavery/diagram-design56.4k