pma-d2
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseD2 Diagram Author
D2图表创作器
Generate idiomatic source files — diagrams encoded as declarative text, rendered by the official CLI to SVG / PNG / PDF / GIF. The output is human-readable, diffable, and embeds anywhere D2 embeds (GitHub, Notion, VS Code, docs sites, Oxide's play.d2lang.com).
.d2d2Keep this entry file small. Load only the reference packs that match the current diagram.
生成符合规范的源文件——以声明式文本编码的图表,可通过官方 CLI渲染为SVG/PNG/PDF/GIF格式。输出内容可读性强、可对比差异,并且能嵌入所有支持D2的平台(GitHub、Notion、VS Code、文档站点、Oxide的play.d2lang.com)。
.d2d2请保持此入口文件简洁。仅加载与当前图表匹配的参考包。
Always-On Rules
通用规则
- Output is text, nothing else. The skill never invokes a renderer; the user runs
.d2or opens play.d2lang.com. Quality must be enforceable by reading the source.d2 input.d2 out.svg - Idiomatic D2 over clever D2. Prefer declarative node names () over IDs + labels when the name reads naturally. Reach for
api -> dbonly when the natural name would be awkward or duplicated.label: - Shape type matches concept. A database is , a queue is
shape: cylinder, an actor isshape: queue. Never default everything toshape: person. Full catalog inrectangle.references/shapes.md - Direction declared up front. Set on the root (and per container if needed). Layout direction is a communication choice, not an incidental default.
direction: down | right | up | left - Semantic edge style, not ornamental. for async / weak dependency;
stroke-dashfor streams;animated: truecolor to encode criticality / failure paths. Catalog instroke.references/connections.md - Group by container, not by proximity. If nodes belong to one domain (a subsystem, a cloud, a bounded context), nest them in a container map. Containers are D2's native grouping — use them instead of manual placement.
- One concept per diagram. If a diagram answers two questions, split into two or two files. Don't compress.
layers: - Classes for repetition. ≥ 2 nodes share styling → define a class in and apply with
classes: {}or a glob..class: name - No invented shapes. All values must come from
shape:. D2 silently falls back to rectangle on typos, which is the worst failure mode: it looks right but isn't.references/shapes.md - Commit-ready source. File ends with a newline, uses 2-space indentation, no trailing whitespace, comments explain intent not mechanics. D2 is git-friendly — keep it that way.
- 输出仅为文本,无其他内容。 本工具从不调用渲染器;用户需自行运行
.d2或打开play.d2lang.com。通过阅读源代码即可确保输出质量。d2 input.d2 out.svg - 优先使用符合规范的D2写法,而非巧妙的技巧。 当名称自然易读时,优先使用声明式节点名称(),而非ID+标签的形式。仅当自然名称会显得生硬或重复时,才使用
api -> db。label: - 图形类型与概念匹配。 数据库使用,队列使用
shape: cylinder,角色使用shape: queue。切勿默认所有图形为shape: person。完整图形目录请查看rectangle。references/shapes.md - 提前声明布局方向。 在根节点设置(必要时可为每个容器单独设置)。布局方向是一种沟通选择,而非随意的默认值。
direction: down | right | up | left - 语义化的连线样式,而非装饰性样式。 使用表示异步/弱依赖;
stroke-dash表示流;animated: true颜色用于区分关键路径/故障路径。样式目录请查看stroke。references/connections.md - 按容器分组,而非按位置 proximity。 如果节点属于同一领域(子系统、云环境、限界上下文),将它们嵌套在容器映射中。容器是D2原生的分组方式——请使用容器而非手动定位。
- 每个图表对应一个概念。 如果一个图表需要回答两个问题,拆分为两个或两个文件。不要压缩内容。
layers: - 使用类处理重复样式。 ≥2个节点共享样式时→在中定义一个类,并通过
classes: {}或通配符应用。.class: name - 不使用自定义图形。 所有的值必须来自
shape:。D2在输入错误时会静默回退为矩形,这是最糟糕的故障模式:看起来正常但实际不符合要求。references/shapes.md - 提交就绪的源代码。 文件以换行结尾,使用2空格缩进,无尾随空格,注释说明意图而非实现细节。D2对Git友好——请保持这一特性。
Core Workflow
核心工作流程
Step 1: Choose Diagram Family
步骤1:选择图表类型
| Family | D2 primitive | Use when |
|---|---|---|
| Architecture / flow | default (nodes + connections) | Systems, pipelines, dependency graphs |
| Sequence | | Protocol exchanges, API call flows, timing |
| ERD / schema | | Data model, foreign keys |
| UML class | | OO design, type relationships |
| Grid / matrix | | Tabular layouts, comparison matrices, k8s cluster views |
| Multi-board | | Before/after, drill-down, animation |
Details per family in and .
references/special-diagrams.mdreferences/composition.md| 类型 | D2原语 | 使用场景 |
|---|---|---|
| 架构/流程 | 默认(节点+连线) | 系统、流水线、依赖图 |
| 时序图 | | 协议交互、API调用流程、时序 |
| ER图/ schema | 每个表使用 | 数据模型、外键 |
| UML类图 | 每个类使用 | 面向对象设计、类型关系 |
| 网格/矩阵 | | 表格布局、对比矩阵、k8s集群视图 |
| 多面板 | | 前后对比、钻取、动画 |
各类型详情请查看和。
references/special-diagrams.mdreferences/composition.mdStep 2: Pick Layout Engine
步骤2:选择布局引擎
- dagre (default, free, bundled): DAG-oriented, good for most architecture diagrams.
- elk (free, bundled since v0.7): cleaner for nested hierarchies and orthogonal routing. Worth trying for multi-container diagrams.
- tala (paid, separate binary): best-in-class for complex, dense diagrams. Note in the file header if required so the user knows what to install.
Pick once per file via . Don't fight the engine with manual positions — D2 has no .
vars.d2-config.layout-enginex,y- dagre(默认、免费、内置):面向有向无环图,适用于大多数架构图。
- elk(免费,v0.7版本起内置):在嵌套层级和正交路由方面表现更简洁。适合多容器图表。
- tala(付费、独立二进制文件):在复杂、密集图表中表现最佳。如果需要使用,请在文件头部注明,以便用户了解需要安装的内容。
通过为每个文件选择一次布局引擎。不要通过手动位置对抗引擎——D2没有属性。
vars.d2-config.layout-enginex,yStep 3: Draft Structure
步骤3:草拟结构
- Sketch the top-level containers first: which systems / domains exist.
- Add nodes inside each container with the correct .
shape: - Add connections — direction first, labels after topology is right.
- Apply to collapse repetition.
classes: - Add block (theme, layout engine) at the top.
vars.d2-config
- 先绘制顶层容器:确定存在哪些系统/领域。
- 在每个容器内添加带有正确的节点。
shape: - 添加连线——先确定方向,拓扑正确后再添加标签。
- 应用来简化重复样式。
classes: - 在顶部添加块(主题、布局引擎)。
vars.d2-config
Step 4: Style Pass
步骤4:样式优化
- Walk every connection — is its style (solid / dashed / animated / color) semantically distinct from its neighbors'?
- Walk every node — does its and class carry information?
shape: - Remove ornamentation that doesn't encode meaning (decorative colors, gratuitous shadows).
- 检查每条连线——其样式(实线/虚线/动画/颜色)是否与相邻连线在语义上有区别?
- 检查每个节点——其和类是否传递了信息?
shape: - 删除不传递含义的装饰(装饰性颜色、多余阴影)。
Step 5: Validate & Hand Off
步骤5:验证与交付
Run the pre-flight in . Tell the user:
references/validation.md- The output path (default ).
docs/architecture/<name>.d2 - The render command: (or whatever target they need).
d2 <name>.d2 <name>.svg - The chosen layout engine and whether it requires a separate install.
- Any icons referenced by URL (so they can mirror internally if needed).
运行中的预检步骤。告知用户:
references/validation.md- 输出路径(默认)。
docs/architecture/<name>.d2 - 渲染命令:(或用户需要的其他目标格式)。
d2 <name>.d2 <name>.svg - 所选布局引擎以及是否需要单独安装。
- 所有通过URL引用的图标(以便用户在内部镜像使用)。
Section-by-Section for Large Diagrams
大型图表的分段处理
- Write the +
varsheader and one container.direction - Append one container per edit; prefix child IDs by container if they'd otherwise collide.
- Cross-container edges use dotted paths (); verify the path exists before emitting the edge.
aws.api -> gcp.auth - When a diagram grows past ~60 nodes, split into or separate
layers:files composed via.d2imports (@file).references/composition.md
- 编写+
vars头部和一个容器。direction - 每次编辑添加一个容器;如果子ID可能冲突,则在子ID前添加容器前缀。
- 跨容器连线使用点路径();在输出连线前验证路径是否存在。
aws.api -> gcp.auth - 当图表节点超过约60个时,拆分为或单独的
layers:文件,通过.d2导入组合(查看@file)。references/composition.md
Output
输出
- File: by default, or a path the user specifies.
docs/architecture/<name>.d2 - Render (user runs):
- →
d2 name.d2(default)name.svg - /
d2 name.d2 name.png/name.pdfname.gif - → live preview on localhost
d2 --watch name.d2 - → theme pairing
d2 --theme <id> --dark-theme <id> name.d2
- Web preview: paste into https://play.d2lang.com/ for instant rendering without installing D2.
- Embedding: see for MDX, GitHub READMEs (checked-in SVG), docs sites (live render at build time), and CI pipelines.
references/integration.md
- 文件:默认路径为,或用户指定的路径。
docs/architecture/<name>.d2 - 渲染(用户执行):
- →
d2 name.d2(默认)name.svg - /
d2 name.d2 name.png/name.pdfname.gif - → 在localhost上实时预览
d2 --watch name.d2 - → 主题组合
d2 --theme <id> --dark-theme <id> name.d2
- 网页预览:将内容粘贴到https://play.d2lang.com/,无需安装D2即可即时渲染。
- 嵌入:查看了解MDX、GitHub README(已提交的SVG)、文档站点(构建时实时渲染)和CI流水线的嵌入方法。
references/integration.md
Reference Packs
参考包
- Core grammar: identifiers, labels, maps, comments, escaping, the shorthand rules D2 is full of.
references/syntax.md - Every built-in shape with picture description, data semantics, and "use when" guidance.
references/shapes.md - Edge operators (
references/connections.md,->,<-,<->), chaining, arrowheads, labels, semantic styling patterns.-- - Nesting,
references/containers.mdparent refs, dotted paths, the difference between a container and a shape, when to flatten vs nest._ references/classes.mdblock, applying viaclasses:, multi-class arrays, glob-based mass application..class:- Complete style key catalog (fill, stroke, stroke-dash, shadow, 3d, multiple, animated, fill-pattern, border-radius, font-size, bold/italic/underline, opacity).
references/styles.md - Direction, grid layouts, dagre vs elk vs tala tradeoffs,
references/layouts.mdpositioning for titles and legends.near: - Sequence diagrams (actors, spans, notes, groups),
references/special-diagrams.mdERDs,sql_tableUML, markdown / code / LaTeX blocks.class references/composition.md(isolated boards),layers:(inherit + override),scenarios:(animation),steps:imports,@filespread....@filereferences/vars.mdblock,vars:substitution,${var}(theme-id, dark-theme-id, sketch, pad, center, layout-engine), nested vars for design tokens.d2-config- Copy-paste starter files: 3-tier web, microservices, data pipeline, CI/CD, sequence (OAuth), ERD, k8s cluster (grid).
references/templates.md - Concept→pattern mapping, isomorphism test, when D2 beats ReactFlow / Mermaid / Excalidraw, when it loses.
references/design.md - Pre-flight algorithm and checklists; common D2 mistakes (silent shape fallback, missing container path, label vs comment collisions).
references/validation.md - CLI flags, watch mode, playground, icon catalogs (terrastruct icons, svgrepo), theme gallery.
references/render.md - MDX / Docusaurus / Nextra embedding, GitHub README via checked-in SVG, docs-site build pipelines, pre-commit render.
references/integration.md
- 核心语法:标识符、标签、映射、注释、转义、D2包含的各种简写规则。
references/syntax.md - 所有内置图形,包含图片说明、数据语义和“使用场景”指南。
references/shapes.md - 连线运算符(
references/connections.md,->,<-,<->)、链式连线、箭头、标签、语义化样式模式。-- - 嵌套、
references/containers.md父引用、点路径、容器与图形的区别、何时扁平化 vs 嵌套。_ references/classes.md块、通过classes:应用、多类数组、基于通配符的批量应用。.class:- 完整样式键目录(填充、描边、描边虚线、阴影、3D、多重样式、动画、填充图案、圆角、字体大小、粗体/斜体/下划线、透明度)。
references/styles.md - 方向、网格布局、dagre vs elk vs tala的权衡、标题和图例的
references/layouts.md定位。near: - 时序图(角色、跨度、注释、分组)、
references/special-diagrams.mdER图、sql_tableUML、markdown/代码/LaTeX块。class references/composition.md(独立面板)、layers:(继承+覆盖)、scenarios:(动画)、steps:导入、@file展开。...@filereferences/vars.md块、vars:替换、${var}(theme-id、dark-theme-id、sketch、pad、center、layout-engine)、用于设计令牌的嵌套变量。d2-config- 可复制粘贴的启动文件:三层Web架构、微服务、数据流水线、CI/CD、时序图(OAuth)、ER图、k8s集群(网格)。
references/templates.md - 概念→模式映射、同构测试、D2优于ReactFlow/Mermaid/Excalidraw的场景、以及劣势场景。
references/design.md - 预检算法和检查清单;常见D2错误(静默图形回退、缺失容器路径、标签与注释冲突)。
references/validation.md - CLI标志、监听模式、在线编辑器、图标目录(terrastruct icons、svgrepo)、主题画廊。
references/render.md - MDX/Docusaurus/Nextra嵌入、通过已提交SVG在GitHub README中展示、文档站点构建流水线、提交前渲染。
references/integration.md
Quick Routing
快速指引
- New to D2 syntax: load +
syntax.md+shapes.md.connections.md - Architecture / system diagram: +
shapes.md+containers.md+layouts.md.templates.md - Sequence / SQL / UML: +
special-diagrams.md.templates.md - Styling pass: +
classes.md.styles.md - Multi-board story (before/after, drill-down, animation): .
composition.md - Theming / config: +
vars.md.render.md - Before delivering: .
validation.md - Telling the user how to render or embed: +
render.md.integration.md
If the project also uses for workflow control, load first, then only when a diagram is required. If the project needs interactive, in-browser diagrams with custom React node components, use instead — D2 renders to static images.
/pma/pma/pma-d2/pma-draw- 不熟悉D2语法:加载+
syntax.md+shapes.md。connections.md - 架构/系统图:+
shapes.md+containers.md+layouts.md。templates.md - 时序图/SQL/UML:+
special-diagrams.md。templates.md - 样式优化:+
classes.md。styles.md - 多面板场景(前后对比、钻取、动画):。
composition.md - 主题/配置:+
vars.md。render.md - 交付前:。
validation.md - 告知用户如何渲染或嵌入:+
render.md。integration.md
如果项目同时使用进行工作流控制,请先加载,仅在需要图表时再加载。如果项目需要带有自定义React节点组件的交互式浏览器图表,请使用替代——D2仅渲染静态图像。
/pma/pma/pma-d2/pma-draw