drawio
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDraw.io Diagram Skill
Draw.io 图表技能
Generate draw.io diagrams as native files. Author each diagram either as Mermaid (concise text that the draw.io desktop CLI converts and lays out for you) or as draw.io XML directly. Optionally auto-layout XML-authored diagrams with ELK, export to PNG/SVG/PDF with the diagram XML embedded (so the exported file stays editable in draw.io), or generate a browser URL that opens the diagram directly in the draw.io editor.
.drawio生成原生 文件格式的draw.io图表。可以通过Mermaid(一种简洁文本格式,draw.io桌面CLI会将其转换并自动布局)或直接通过draw.io XML来创作图表。还可选择使用ELK对XML创作的图表进行自动布局,将图表导出为嵌入了XML的PNG/SVG/PDF格式(这样导出的文件仍可在draw.io中编辑),或者生成可直接在draw.io编辑器中打开图表的浏览器URL。
.drawioAuthoring: Mermaid or XML?
创作方式:选Mermaid还是XML?
The desktop CLI can convert Mermaid to a native file, so prefer Mermaid for the diagram types it handles well — its parser lays the diagram out automatically, which is far more reliable than hand-positioning cells in XML.
.drawio| Author as | Best for | Needs desktop CLI? |
|---|---|---|
| Mermaid | Flowcharts, sequence, class, state, ER, gantt, mindmap, timeline, user journey, quadrant, C4, git graph, pie, and other standard types | Yes — to convert to |
| XML | Custom styling, precise/hand positioning, specific shape libraries (AWS, Azure, network, UML detail…), or when the desktop CLI is not installed | No (optional ELK |
- Prefer Mermaid when the desktop CLI is available and the request is one of the standard types above — write terse Mermaid and let draw.io lay it out.
- Use XML for precise control, or as the universal fallback: XML needs no CLI at all, so it's the only option when the desktop app isn't installed (output a file or a
.drawio).url - For XML-authored diagrams you can ask the CLI to apply an ELK auto-layout () instead of computing coordinates yourself — the same layouts the draw.io editor's Arrange ▸ Layout menu applies, and the same engine the draw.io MCP app server uses. See ELK layout for XML.
--layout
If you're unsure whether the desktop CLI is present, detect it first (see Locating the CLI). No CLI → author as XML and deliver a file or a .
.drawiourl桌面CLI可以将Mermaid转换为原生 文件,因此优先选择Mermaid来创作它擅长处理的图表类型——其解析器会自动完成图表布局,这比手动在XML中定位单元格要可靠得多。
.drawio| 创作方式 | 适用场景 | 是否需要桌面CLI? |
|---|---|---|
| Mermaid | 流程图、时序图、类图、状态图、ER图、甘特图、思维导图、时间线、用户旅程图、四象限图、C4图、Git图、饼图及其他标准类型 | 是——用于转换为 |
| XML | 自定义样式、精准手动定位、特定形状库(AWS、Azure、网络、UML细节等),或未安装桌面CLI的场景 | 否(可选的ELK |
- 当桌面CLI可用且需求属于上述标准类型时,优先使用Mermaid——编写简洁的Mermaid代码,让draw.io自动完成布局。
- 需要精准控制时,或作为通用 fallback 方案时使用XML:XML完全不需要CLI,因此是未安装桌面应用时的唯一选择(输出 文件或
.drawio)。url - 对于XML创作的图表,你可以让CLI应用ELK自动布局(),无需自行计算坐标——这与draw.io编辑器中「排列 ▸ 布局」菜单使用的布局方式相同,也和draw.io MCP应用服务器使用的引擎一致。详见XML的ELK布局。
--layout
The pipeline
工作流程
Every diagram becomes a native file first, then is delivered in the requested output format. This keeps the delivery step identical whether you authored Mermaid or XML.
.drawio- Author →
.drawio- Mermaid: write the Mermaid to a file, then convert it with the CLI:
.mmdDelete thebashdrawio -x -f xml -o diagram.drawio diagram.mmdafterward — the.mmdis the artifact. draw.io's Mermaid parser has already laid the diagram out, so no.drawiois needed.--layout - XML: write the mxGraphModel XML to (see XML format). Optionally apply an ELK layout (see ELK layout for XML).
diagram.drawio
- Mermaid: write the Mermaid to a
- Deliver (identical for both sources):
- (no format) → keep and open it.
diagram.drawio - png / svg / pdf → export from the with embedded XML, then delete the source
.drawio:.drawiobashdrawio -x -f png -e -b 10 -o diagram.drawio.png diagram.drawio - url → build a browser URL from the XML, open it, and keep the
.drawioas a local copy (see Browser URL output)..drawio
- (no format) → keep
- Open the result — the exported file, the URL, or the . If the open command fails, print the absolute path (or URL) so the user can open it manually.
.drawio
Always convert Mermaid to first, then export — do not export a straight to an image. Direct Mermaid → PNG export with is broken in current draw.io Desktop (the embedded-XML step crashes); the two-step path (convert, then export the ) is reliable and produces an editable embed. See Troubleshooting.
.drawio.mmd-e.drawioIf Mermaid was requested but no desktop CLI is available, fall back to authoring the same diagram directly as XML.
所有图表都会先转换为原生 文件,再以请求的输出格式交付。无论你使用Mermaid还是XML创作,交付步骤都是相同的。
.drawio- 创作 → 文件
.drawio - 交付(两种创作方式的交付步骤相同):
- (未指定格式) → 保留 文件并打开它。
diagram.drawio - png / svg / pdf → 从 文件导出并嵌入XML,然后删除源
.drawio文件:.drawiobashdrawio -x -f png -e -b 10 -o diagram.drawio.png diagram.drawio - url → 从 XML构建浏览器URL,打开该URL,并保留
.drawio文件作为本地副本(详见浏览器URL输出)。.drawio
- (未指定格式) → 保留
- 打开结果——导出的文件、URL或 文件。如果打开命令失败,请打印绝对路径(或URL)以便用户手动打开。
.drawio
务必先将Mermaid转换为 文件,再进行导出——不要直接将 文件导出为图片。当前draw.io Desktop中直接从Mermaid→PNG导出(使用 )存在问题(嵌入XML步骤会崩溃);两步法(先转换为 ,再导出)更可靠,且能生成可编辑的嵌入内容。详见故障排查。
.drawio.mmd-e.drawio如果用户要求使用Mermaid但无桌面CLI可用,则直接以XML创作相同的图表。
ELK layout for XML
XML的ELK布局
XML-authored diagrams can be auto-positioned by the CLI's pass — the same ELK layouts as the editor's Arrange ▸ Layout menu and the same engine the draw.io MCP app server uses. Generate the cells with approximate (or even ) positions and let ELK place them; you only have to get the graph structure — nodes and edges — right.
--layout0,0Add to any CLI call that reads your XML. The simplest form lays out in place after you write the file (reading and overwriting the same path is supported):
--layout <name>bash
drawio -x -f xml --layout verticalFlow -o diagram.drawio diagram.drawioOr combine layout with export in a single call (works for XML input):
bash
drawio -x -f png -e -b 10 --layout verticalFlow -o diagram.drawio.png diagram.drawioXML创作的图表可通过CLI的 命令自动定位——这与编辑器「排列 ▸ 布局」菜单使用的ELK布局相同,也和draw.io MCP应用服务器使用的引擎一致。生成单元格时可使用近似位置(甚至 ),让ELK自动放置;你只需确保图表的结构——节点和连线——正确即可。
--layout0,0在任何读取XML的CLI命令中添加 。最简单的方式是写入文件后直接进行布局(支持读取并覆盖同一路径):
--layout <名称>bash
drawio -x -f xml --layout verticalFlow -o diagram.drawio diagram.drawio也可在单次调用中结合布局与导出(适用于XML输入):
bash
drawio -x -f png -e -b 10 --layout verticalFlow -o diagram.drawio.png diagram.drawioLayout presets
布局预设
| Name | Layout |
|---|---|
| Layered, top-to-bottom — flowcharts, pipelines |
| Layered, left-to-right |
| Tree, top-down — hierarchies, org charts |
| Tree, left-to-right |
| Radial tree |
| Force-directed — networks, mind-map-like graphs |
| 名称 | 布局方式 |
|---|---|
| 分层、自上而下——流程图、流水线 |
| 分层、自左至右 |
| 树形、自上而下——层级结构、组织架构图 |
| 树形、自左至右 |
| 放射状树形 |
| 力导向布局——网络图、类思维导图的图表 |
Custom layout JSON
自定义布局JSON
For finer control, pass a JSON array (starting with ) instead of a preset name — the same format as the editor's custom-layout dialog:
[bash
drawio -x -f xml --layout '[{"layout":"elkLayered","config":{"elk.direction":"RIGHT"}}]' -o diagram.drawio diagram.drawioEach entry is :
{"layout": <algorithm>, "config": { … }}- Algorithms: ,
elkLayered,elkTree,elkRadial,elkOrganic,elkStress.elkBox - : keys starting with
configare ELK options — e.g.elk.(elk.direction/UP/DOWN/LEFT),RIGHT,elk.spacing.nodeNode. The keyselk.layered.spacing.nodeNodeBetweenLayers(e.g.edgeStyle) andorthogonal(e.g.corners) control connector rendering.rounded
如需更精细的控制,可传入JSON数组(以 开头)替代预设名称——格式与编辑器的自定义布局对话框相同:
[bash
drawio -x -f xml --layout '[{"layout":"elkLayered","config":{"elk.direction":"RIGHT"}}]' -o diagram.drawio diagram.drawio每个数组项为 :
{"layout": <算法>, "config": { … }}- 算法:、
elkLayered、elkTree、elkRadial、elkOrganic、elkStress。elkBox - :以
config开头的键为ELK选项——例如elk.(elk.direction/UP/DOWN/LEFT)、RIGHT、elk.spacing.nodeNode。elk.layered.spacing.nodeNodeBetweenLayers(例如edgeStyle)和orthogonal(例如corners)控制连线的渲染方式。rounded
Orthogonal edge routing
正交连线路由
--layout libavoidbash
drawio -x -f xml --layout libavoid -o diagram.drawio diagram.drawioSkip it after a flow/tree preset — those already route their edges.
When to use it: author the graph structure as XML without worrying about coordinates, then apply / for flow-style diagrams or for networks. Mermaid-authored diagrams are already laid out — don't add .
verticalFlowhorizontalFloworganic--layout--layout libavoidbash
drawio -x -f xml --layout libavoid -o diagram.drawio diagram.drawio使用流/树形预设后无需再使用该命令——这些预设已完成连线路由。
使用时机:以XML创作图表结构时无需担心坐标,之后对流程类图表应用 / ,对网络图应用 。Mermaid创作的图表已完成布局——无需添加 。
verticalFlowhorizontalFloworganic--layoutMermaid syntax reference
Mermaid语法参考
When authoring Mermaid, fetch and follow the shared Mermaid reference (all supported diagram types plus flowchart styling — , , ):
styleclassDeflinkStyleMatch the language of the diagram labels to the user's language.
创作Mermaid图表时,请参考并遵循共享的Mermaid参考文档(包含所有支持的图表类型及流程图样式——、、):
styleclassDeflinkStyle图表标签的语言需与用户使用的语言一致。
Choosing the output format
选择输出格式
Check the user's request for a format preference. Examples:
- → Mermaid →
/drawio:drawio create a flowchartflowchart.drawio - → Mermaid →
/drawio:drawio png flowchart for loginlogin-flow.drawio.png - → Mermaid →
/drawio:drawio svg: ER diagramer-diagram.drawio.svg - → XML (needs AWS shapes) →
/drawio:drawio pdf AWS architecture overviewarchitecture-overview.drawio.pdf - → opens browser at
/drawio:drawio url flowchart for user loginwith the diagram, keepsapp.diagrams.netlocallylogin-flow.drawio
If no format is mentioned, just produce the file and open it in draw.io. The user can always ask to export later.
.drawio检查用户请求中的格式偏好。示例:
- → Mermaid →
/drawio:drawio create a flowchartflowchart.drawio - → Mermaid →
/drawio:drawio png flowchart for loginlogin-flow.drawio.png - → Mermaid →
/drawio:drawio svg: ER diagramer-diagram.drawio.svg - → XML(需要AWS形状)→
/drawio:drawio pdf AWS architecture overviewarchitecture-overview.drawio.pdf - → 在浏览器中打开
/drawio:drawio url flowchart for user login并加载该图表,本地保留app.diagrams.net文件login-flow.drawio
如果未提及格式,只需生成 文件并在draw.io中打开。用户之后可随时要求导出。
.drawioSupported export formats
支持的导出格式
| Format | Embed XML | Notes |
|---|---|---|
| Yes ( | Viewable everywhere, editable in draw.io |
| Yes ( | Scalable, editable in draw.io |
| Yes ( | Printable, editable in draw.io |
| No | Lossy, no embedded XML support |
PNG, SVG, and PDF all support — the exported file contains the full diagram XML, so opening it in draw.io recovers the editable diagram.
--embed-diagram| 格式 | 嵌入XML | 说明 |
|---|---|---|
| 是( | 可在任何地方查看,可在draw.io中编辑 |
| 是( | 可缩放,可在draw.io中编辑 |
| 是( | 可打印,可在draw.io中编辑 |
| 否 | 有损压缩,不支持嵌入XML |
PNG、SVG和PDF均支持 ——导出的文件包含完整的图表XML,在draw.io中打开即可恢复可编辑的图表。
--embed-diagramBrowser URL output
浏览器URL输出
When the user requests format, generate a draw.io URL that opens the diagram directly in the browser editor at — no draw.io Desktop required to view it. (Mermaid-authored diagrams still need the desktop CLI to convert to first; if no CLI is available, author the diagram as XML and build the URL from that.)
urlapp.diagrams.net.drawio当用户要求 格式时,生成draw.io URL,可直接在浏览器编辑器 中打开图表——查看图表无需draw.io Desktop。(Mermaid创作的图表仍需桌面CLI先转换为 文件;如果无CLI可用,则直接以XML创作图表并基于XML构建URL。)
urlapp.diagrams.net.drawioHow it works
工作原理
- The file is written to disk as usual (gives the user a persistent local copy they can re-edit)
.drawio - The XML is compressed with Node.js's built-in and base64-encoded
zlib - The result is embedded in a URL
https://app.diagrams.net/#create=... - The URL is opened in the default browser
This uses only Node.js built-in modules (, ) — no external dependencies.
zlibchild_process- 按常规将 文件写入磁盘(为用户提供可重新编辑的持久本地副本)
.drawio - 使用Node.js内置的 压缩XML并进行base64编码
zlib - 将结果嵌入 URL
https://app.diagrams.net/#create=... - 在默认浏览器中打开该URL
此过程仅使用Node.js内置模块(、)——无外部依赖。
zlibchild_processURL generation
URL生成
Run this one-liner to read the file and print the URL (replace with the actual filename):
node -e.drawioDIAGRAM.drawiobash
URL=$(node -e '
const fs = require("fs");
const zlib = require("zlib");
const xml = fs.readFileSync(process.argv[1], "utf8");
const compressed = zlib.deflateRawSync(encodeURIComponent(xml)).toString("base64");
const payload = encodeURIComponent(JSON.stringify({ type: "xml", compressed: true, data: compressed }));
console.log("https://app.diagrams.net/?grid=0&pv=0&border=10&edit=_blank#create=" + payload);
' "DIAGRAM.drawio")The URL format matches the MCP Tool Server. Node.js's and both implement RFC 1951 and produce identical output, so URLs from either source are interchangeable.
zlib.deflateRawSyncpako.deflateRaw运行以下 单行命令读取 文件并打印URL(将 替换为实际文件名):
node -e.drawioDIAGRAM.drawiobash
URL=$(node -e '
const fs = require("fs");
const zlib = require("zlib");
const xml = fs.readFileSync(process.argv[1], "utf8");
const compressed = zlib.deflateRawSync(encodeURIComponent(xml)).toString("base64");
const payload = encodeURIComponent(JSON.stringify({ type: "xml", compressed: true, data: compressed }));
console.log("https://app.diagrams.net/?grid=0&pv=0&border=10&edit=_blank#create=" + payload);
' "DIAGRAM.drawio")URL格式与MCP工具服务器一致。Node.js的 和 均实现了RFC 1951,输出结果相同,因此两种方式生成的URL可互换使用。
zlib.deflateRawSyncpako.deflateRawOpening the URL
打开URL
| Environment | Command |
|---|---|
| macOS | |
| Linux (native) | |
| WSL2 | Write a temp |
| Windows (native) | Write a temp |
Why the workaround on Windows/WSL2? 's command treats as a command separator and strips everything after in URLs. The diagram payload lives in the fragment, so passing the URL directly causes it to be silently lost. A shortcut file preserves the URL intact.
.urlcmd.exestart&##create=....urlmacOS / Linux example:
bash
open "$URL" # macOS
xdg-open "$URL" # LinuxWSL2 example:
bash
TMPFILE=$(mktemp --suffix=.url)
printf '[InternetShortcut]\r\nURL=%s\r\n' "$URL" > "$TMPFILE"
cmd.exe /c start "" "$(wslpath -w "$TMPFILE")"Windows (native) example:
Do not build the file with . The generated URL contains characters () that treats as command separators, so the shortcut is written truncated and the diagram payload is lost — the exact failure the file is meant to prevent. Let Node write the file directly (it already holds the URL string) and open only the resulting path, which never contains :
.urlecho URL=%URL%&?grid=0&pv=0&...cmd.exe.url&bash
TMPFILE=$(node -e '
const fs = require("fs");
const os = require("os");
const path = require("path");
const p = path.join(os.tmpdir(), "drawio.url");
fs.writeFileSync(p, "[InternetShortcut]\r\nURL=" + process.argv[1] + "\r\n");
process.stdout.write(p);
' "$URL")
cmd.exe /c start "" "$TMPFILE"| 环境 | 命令 |
|---|---|
| macOS | |
| Linux(原生) | |
| WSL2 | 写入临时 |
| Windows(原生) | 写入临时 |
为什么Windows/WSL2需要 变通方案? 的 命令会将 视为命令分隔符,并删除URL中 之后的所有内容。图表负载位于 片段中,因此直接传递URL会导致负载丢失。 快捷方式文件可完整保留URL。
.urlcmd.exestart&##create=....urlmacOS / Linux示例:
bash
open "$URL" # macOS
xdg-open "$URL" # LinuxWSL2示例:
bash
TMPFILE=$(mktemp --suffix=.url)
printf '[InternetShortcut]\r\nURL=%s\r\n' "$URL" > "$TMPFILE"
cmd.exe /c start "" "$(wslpath -w "$TMPFILE")"Windows(原生)示例:
请勿使用 构建 文件。生成的URL包含 字符(), 会将其视为命令分隔符,导致快捷方式文件被截断,图表负载丢失——这正是 文件要解决的问题。让Node.js直接写入文件(它已持有完整的URL字符串),然后仅打开生成的路径,该路径绝不会包含 :
echo URL=%URL%.url&?grid=0&pv=0&...cmd.exe.url&bash
TMPFILE=$(node -e '
const fs = require("fs");
const os = require("os");
const path = require("path");
const p = path.join(os.tmpdir(), "drawio.url");
fs.writeFileSync(p, "[InternetShortcut]\r\nURL=" + process.argv[1] + "\r\n");
process.stdout.write(p);
' "$URL")
cmd.exe /c start "" "$TMPFILE"After opening
打开后操作
Print the URL so the user can copy or share it, and confirm the local file path:
Opened in browser: <URL>
Local file: DIAGRAM.drawioThe file stays on disk so the user can re-edit it later, attach it elsewhere, or export it to an image format on demand.
.drawio打印URL以便用户复制或分享,并确认本地文件路径:
已在浏览器中打开:<URL>
本地文件:DIAGRAM.drawio.drawioURL length
URL长度
The URL embeds the full compressed diagram in its hash fragment. Very large diagrams may hit browser URL length limits (typically ~32K–2MB depending on the browser). For complex diagrams that exceed the limit, fall back to writing the file and opening it locally.
.drawioURL会在哈希片段中嵌入完整的压缩图表。非常大的图表可能会超出浏览器URL长度限制(通常约32K–2MB,取决于浏览器)。对于超出限制的复杂图表,退而求其次,写入 文件并在本地打开。
.drawiodraw.io CLI
draw.io CLI
The draw.io desktop app includes a command-line interface used for converting Mermaid to , applying ELK layouts (), and exporting to PNG/SVG/PDF. All three require the desktop app to be installed.
.drawio--layoutdraw.io桌面应用包含命令行界面,用于将Mermaid转换为 文件、应用ELK布局()以及导出为PNG/SVG/PDF格式。这三个功能都需要安装桌面应用。
.drawio--layoutLocating the CLI
定位CLI
First, detect the environment, then locate the CLI accordingly:
首先检测环境,然后据此定位CLI:
WSL2 (Windows Subsystem for Linux)
WSL2(Windows子系统Linux)
WSL2 is detected when contains or :
/proc/versionmicrosoftWSLbash
grep -qi microsoft /proc/version 2>/dev/null && echo "WSL2"On WSL2, use the Windows draw.io Desktop executable via :
/mnt/c/...bash
DRAWIO_CMD="/mnt/c/Program Files/draw.io/draw.io.exe"Double-quote the path so the space in is treated as part of the path. Do not wrap it in backticks — in bash, backticks are command substitution, which would try to execute the binary at locate-time instead of storing its path.
Program FilesIf draw.io is installed in a non-default location, check common alternatives:
bash
undefined当 包含 或 时,可检测到WSL2:
/proc/versionmicrosoftWSLbash
grep -qi microsoft /proc/version 2>/dev/null && echo "WSL2"在WSL2中,通过 使用Windows版draw.io Desktop可执行文件:
/mnt/c/...bash
DRAWIO_CMD="/mnt/c/Program Files/draw.io/draw.io.exe"路径需用双引号包裹,以便 中的空格被视为路径的一部分。请勿使用反引号——在bash中,反引号是命令替换,会在定位时尝试执行二进制文件,而非存储其路径。
Program Files如果draw.io安装在非默认位置,请检查常见替代路径:
bash
undefinedDefault install path
默认安装路径
"/mnt/c/Program Files/draw.io/draw.io.exe"
"/mnt/c/Program Files/draw.io/draw.io.exe"
Per-user install (if the above does not exist)
按用户安装(如果上述路径不存在)
"/mnt/c/Users/$WIN_USER/AppData/Local/Programs/draw.io/draw.io.exe"
undefined"/mnt/c/Users/$WIN_USER/AppData/Local/Programs/draw.io/draw.io.exe"
undefinedmacOS
macOS
bash
/Applications/draw.io.app/Contents/MacOS/draw.iobash
/Applications/draw.io.app/Contents/MacOS/draw.ioLinux (native)
Linux(原生)
bash
drawio # typically on PATH via snap/apt/flatpakbash
drawio # 通常通过snap/apt/flatpak安装在PATH中Windows (native, non-WSL2)
Windows(原生,非WSL2)
"C:\Program Files\draw.io\draw.io.exe"Use (or on Windows) to check if it's on PATH before falling back to the platform-specific path.
which drawiowhere draw.io"C:\Program Files\draw.io\draw.io.exe"在使用平台特定路径之前,先使用 (Windows中使用 )检查它是否在PATH中。
which drawiowhere draw.ioConvert / layout / export commands
转换/布局/导出命令
Convert Mermaid to :
.drawiobash
drawio -x -f xml -o diagram.drawio diagram.mmdApply an ELK layout to XML (see ELK layout for XML):
bash
drawio -x -f xml --layout verticalFlow -o diagram.drawio diagram.drawioExport to an image format:
bash
drawio -x -f <format> -e -b 10 -o "<output>" "<input.drawio>"WSL2 export example:
bash
"/mnt/c/Program Files/draw.io/draw.io.exe" -x -f png -e -b 10 -o "diagram.drawio.png" "diagram.drawio"Key flags:
- /
-x: export mode (also used for Mermaid conversion and layout passes)--export - /
-f: output format (--format, png, svg, pdf, jpg) — usexmlto produce axmlfrom Mermaid or a layout pass.drawio - : run a layout before writing the output — an ELK preset name, the
--layoutedge-routing pass, or a custom-layout JSON arraylibavoid - : convert Mermaid to a single static SVG image cell (the Mermaid source stays on the cell for re-editing) instead of an editable diagram — only when the user explicitly asks for a non-editable image cell
--mermaid-image 1 - /
-e: embed diagram XML in the output (PNG, SVG, PDF only)--embed-diagram - /
-o: output file path--output - /
-b: border width around diagram (default: 0)--border - /
-t: transparent background (PNG only)--transparent - /
-s: scale the diagram size--scale - /
--width: fit into specified dimensions (preserves aspect ratio)--height - /
-a: export all pages (PDF only)--all-pages - /
-p: select a specific page (1-based)--page-index
将Mermaid转换为 文件:
.drawiobash
drawio -x -f xml -o diagram.drawio diagram.mmd对XML应用ELK布局(详见XML的ELK布局):
bash
drawio -x -f xml --layout verticalFlow -o diagram.drawio diagram.drawio导出为图片格式:
bash
drawio -x -f <format> -e -b 10 -o "<output>" "<input.drawio>"WSL2导出示例:
bash
"/mnt/c/Program Files/draw.io/draw.io.exe" -x -f png -e -b 10 -o "diagram.drawio.png" "diagram.drawio"关键参数:
- /
-x:导出模式(也用于Mermaid转换和布局操作)--export - /
-f:输出格式(--format、png、svg、pdf、jpg)——从Mermaid转换或布局操作生成xml文件时使用.drawioxml - :写入输出前运行布局——ELK预设名称、
--layout连线路由操作,或自定义布局JSON数组libavoid - :将Mermaid转换为单个静态SVG图片单元格(Mermaid源会保留在单元格中以便重新编辑),而非可编辑的图表——仅当用户明确要求不可编辑的图片单元格时使用
--mermaid-image 1 - /
-e:在输出中嵌入图表XML(仅适用于PNG、SVG、PDF)--embed-diagram - /
-o:输出文件路径--output - /
-b:图表周围的边框宽度(默认:0)--border - /
-t:透明背景(仅适用于PNG)--transparent - /
-s:缩放图表大小--scale - /
--width:适配指定尺寸(保持宽高比)--height - /
-a:导出所有页面(仅适用于PDF)--all-pages - /
-p:选择特定页面(从1开始计数)--page-index
Opening the result
打开结果
| Environment | Command |
|---|---|
| macOS | |
| Linux (native) | |
| WSL2 | |
| Windows | |
WSL2 notes:
- converts a WSL2 path (e.g.
wslpath -w <file>) to a Windows path (e.g./home/user/diagram.drawio). This is required becauseC:\Users\...cannot resolvecmd.exestyle paths./mnt/c/... - The empty string after
""is required to preventstartfrom interpreting the filename as a window title.start
WSL2 example:
bash
cmd.exe /c start "" "$(wslpath -w diagram.drawio)"| 环境 | 命令 |
|---|---|
| macOS | |
| Linux(原生) | |
| WSL2 | |
| Windows | |
WSL2注意事项:
- 将WSL2路径(例如
wslpath -w <file>)转换为Windows路径(例如/home/user/diagram.drawio)。这是必需的,因为C:\Users\...无法解析cmd.exe格式的路径。/mnt/c/... - 后的空字符串
start是必需的,可防止""将文件名解释为窗口标题。start
WSL2示例:
bash
cmd.exe /c start "" "$(wslpath -w diagram.drawio)"File naming
文件命名
- Use a descriptive filename based on the diagram content (e.g., ,
login-flow)database-schema - Use lowercase with hyphens for multi-word names
- When authoring Mermaid, write it to a matching file, convert to
.mmd, then delete the.drawio— the.mmdis the artifact.drawio - For export, use double extensions: ,
name.drawio.png,name.drawio.svg— this signals the file contains embedded diagram XMLname.drawio.pdf - After a successful export, delete the intermediate file — the exported file contains the full diagram
.drawio - For mode, keep the
urlfile (no double extension) — the URL is a view/edit handle and the local file is the persistent copy.drawio
- 根据图表内容使用描述性文件名(例如 、
login-flow)database-schema - 多词名称使用小写字母加连字符
- 创作Mermaid时,将代码写入对应的 文件,转换为
.mmd文件后删除.drawio文件——.mmd文件是最终产物.drawio - 导出时使用双重扩展名:、
name.drawio.png、name.drawio.svg——这表明文件包含嵌入的图表XMLname.drawio.pdf - 导出成功后,删除中间 文件——导出的文件包含完整的图表
.drawio - 模式下,保留
url文件(不使用双重扩展名)——URL是查看/编辑入口,本地文件是持久副本.drawio
XML format
XML格式
A file is native mxGraphModel XML. When authoring as XML, generate it directly; Mermaid is converted to this same format by the CLI (), so both authoring routes end up as a native .
.drawio-f xml.drawio.drawio-f xml.drawioBasic structure
基本结构
Every diagram must have this structure:
xml
<mxGraphModel adaptiveColors="auto">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<!-- Diagram cells go here with parent="1" -->
</root>
</mxGraphModel>- Cell is the root layer
id="0" - Cell is the default parent layer
id="1" - All diagram elements use unless using multiple layers
parent="1"
(The example above uses an XML comment only to point out where cells go — never emit comments in real output; see XML well-formedness.)
每个图表必须具有以下结构:
xml
<mxGraphModel adaptiveColors="auto">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<!-- Diagram cells go here with parent="1" -->
</root>
</mxGraphModel>- 单元格 是根图层
id="0" - 单元格 是默认父图层
id="1" - 所有图表元素使用 ,除非使用多个图层
parent="1"
(上述示例中的XML注释仅用于指出单元格的位置——实际输出中绝不要包含注释;详见XML格式正确性。)
XML reference
XML参考
For the complete draw.io XML reference including common styles, edge routing, containers, layers, tags, metadata, dark mode colors, and XML well-formedness rules, fetch and follow the instructions at:
https://raw.githubusercontent.com/jgraph/drawio-mcp/main/shared/xml-reference.md
如需完整的draw.io XML参考,包括常见样式、连线路由、容器、图层、标签、元数据、深色模式颜色和XML格式规则,请参考以下链接中的说明:
https://raw.githubusercontent.com/jgraph/drawio-mcp/main/shared/xml-reference.md
Troubleshooting
故障排查
| Problem | Cause | Solution |
|---|---|---|
| draw.io CLI not found | Desktop app not installed or not on PATH | Author as XML and deliver a |
| Mermaid → PNG export crashes | Direct | Use the two-step path: convert Mermaid to |
| Blank diagram from Mermaid | Misspelled type keyword, or a syntax error (bad node ID, unquoted label) | Check the Mermaid reference; the first non-directive line's keyword selects the diagram type |
| Layout does nothing / errors | Unknown preset name, custom JSON not an array, or a desktop build too old for | Use a preset from Layout presets or a JSON array starting with |
| Export produces empty/corrupt file | Invalid XML (e.g. double hyphens in comments, unescaped special characters) | Validate XML well-formedness before writing; see the XML well-formedness section below |
| Diagram opens but looks blank | Missing root cells | Ensure the basic mxGraphModel structure is complete |
| Edges not rendering | Edge mxCell is self-closing (no child mxGeometry element) | Every edge must have |
| File won't open after export | Incorrect file path or missing file association | Print the absolute file path so the user can open it manually |
Browser opens with empty diagram in | | Use the |
| URL is too long for the browser | Very large diagram exceeds browser URL length limit | Fall back to writing the |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 未找到draw.io CLI | 未安装桌面应用或应用不在PATH中 | 以XML创作并交付 |
| Mermaid→PNG导出崩溃 | 当前draw.io Desktop中直接从 | 使用两步法:先将Mermaid转换为 |
| Mermaid生成空白图表 | 类型关键字拼写错误,或存在语法错误(节点ID无效、标签未加引号) | 查看Mermaid语法参考;第一行非指令代码的关键字决定了图表类型 |
| 布局无效果/报错 | 预设名称未知、自定义JSON不是数组,或桌面版本过旧不支持 | 使用布局预设中的预设,或使用以 |
| 导出生成空文件/损坏文件 | XML无效(例如注释中包含双连字符、特殊字符未转义) | 写入前验证XML格式正确性;详见下文的XML格式正确性部分 |
| 图表可打开但显示空白 | 缺少根单元格 | 确保mxGraphModel基本结构完整 |
| 连线未渲染 | 连线mxCell是自闭合的(无子mxGeometry元素) | 每个连线必须包含 |
| 导出后文件无法打开 | 文件路径错误或缺少文件关联 | 打印绝对文件路径以便用户手动打开 |
| | 在Windows/WSL2中使用 |
| URL过长超出浏览器限制 | 超大图表超出浏览器URL长度限制 | 退而求其次,写入 |
CRITICAL: XML well-formedness
关键:XML格式正确性
- NEVER include ANY XML comments () in the output. XML comments are strictly forbidden — they waste tokens, can cause parse errors, and serve no purpose in diagram XML.
<!-- --> - Escape special characters in attribute values: ,
&,<,>" - Always use unique values for each
idmxCell
- 输出中绝不要包含任何XML注释()。XML注释是严格禁止的——它们会浪费令牌,可能导致解析错误,且在图表XML中毫无用处。
<!-- --> - 转义属性值中的特殊字符:、
&、<、>" - 每个 必须使用唯一的
mxCell值id