terrastruct/d2

GitHub: terrastruct/d2

D2 是一种用文本描述自动生成专业图表的现代脚本语言,让架构图像代码一样易于编写、审查和版本管理。

Stars: 23547 | Forks: 636

D2

A modern diagram scripting language that turns text to diagrams.

[文档](https://d2lang.com) | [备忘录](./docs/assets/cheat_sheet.pdf) | [对比](https://text-to-diagram.com) | [演练场](https://play.d2lang.com) | [IDE](https://d2studio.ai) [![ci](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/5ffb163d7c200106.svg)](https://github.com/terrastruct/d2/actions/workflows/ci.yml) [![daily](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/5c51c59168200112.svg)](https://github.com/terrastruct/d2/actions/workflows/daily.yml) [![release](https://img.shields.io/github/v/release/terrastruct/d2)](https://github.com/terrastruct/d2/releases) [![changelog](https://img.shields.io/badge/changelog-read-blue)](./CHANGELOG.md) [![npm version](https://img.shields.io/npm/v/@terrastruct/d2)](https://www.npmjs.com/package/@terrastruct/d2) [![discord](https://img.shields.io/discord/1039184639652265985?label=discord)](https://discord.gg/NF6X8K4eDq) [![twitter](https://img.shields.io/twitter/follow/terrastruct?style=social)](https://twitter.com/terrastruct) [![license](https://img.shields.io/github/license/terrastruct/d2?color=9cf)](./LICENSE.txt) D2 Playground button D2 Studio button https://user-images.githubusercontent.com/3120367/206125010-bd1fea8e-248a-43e7-8f85-0bbfca0c6e2a.mp4
# 目录 - [D2 长什么样?](#what-does-d2-look-like) - [快速入门](#quickstart) - [安装](#install) - [D2 作为库使用](#d2-as-a-library) - [主题](#themes) - [字体](#fonts) - [导出文件类型](#export-file-types) - [语言工具](#language-tooling) - [插件](#plugins) - [对比](#comparison) - [贡献](#contributing) - [许可证](#license) - [相关项目](#related) - [官方插件](#official-plugins) - [社区插件](#community-plugins) - [杂项](#misc) - [常见问题](#faq) - [使用 D2 进行文档编写的著名开源项目](#notable-open-source-projects-documenting-with-d2) ## D2 长什么样? ``` vars: { d2-config: { layout-engine: elk # Terminal theme code theme-id: 300 } } network: { cell tower: { satellites: { shape: stored_data style.multiple: true } transmitter satellites -> transmitter: send satellites -> transmitter: send satellites -> transmitter: send } online portal: { ui: {shape: hexagon} } data processor: { storage: { shape: cylinder style.multiple: true } } cell tower.transmitter -> data processor.storage: phone logs } user: { shape: person width: 130 } user -> network.cell tower: make call user -> network.online portal.ui: access { style.stroke-dash: 3 } api server -> network.online portal.ui: display api server -> logs: persist logs: {shape: page; style.multiple: true} network.data processor -> api server ```

D2 render example

## 快速入门 使用 D2 最便捷的方式是将其作为 CLI 可执行文件运行,从而 从 `.d2` 文件生成 SVG。 ``` # 首先,安装 D2 curl -fsSL https://d2lang.com/install.sh | sh -s -- echo 'x -> y -> z' > in.d2 d2 --watch in.d2 out.svg ``` 一个浏览器窗口将会打开并显示 `out.svg`,并且当 `in.d2` 发生更改时会自动热重载。 ## 安装 最简单的安装方式是使用我们的安装脚本: ``` curl -fsSL https://d2lang.com/install.sh | sh -s -- ``` 你可以带上 `--dry-run` 参数运行安装脚本,以查看将要执行的 安装命令而无需实际运行它们。 或者,如果你已经安装了 Go,你可以从源码安装,不过这样你将无法获取 manpage: ``` go install oss.terrastruct.com/d2@latest ``` 你也可以通过发布版本的源码进行安装,这将包含 manpage。 详见 [./docs/INSTALL.md#source-release](./docs/INSTALL.md#source-release)。 如果要使用安装脚本卸载: ``` curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall ``` 获取详细的安装文档,请参阅 [./docs/INSTALL.md](./docs/INSTALL.md)。 我们演示了适用于各种操作系统的替代安装方法和示例。 同时,安装脚本的工作原理也得到了详细描述,以消除对其使用的 任何顾虑。我们建议直接使用您操作系统的包管理器以获得 更好的安全性,但安装脚本也绝对是安全的。 ## D2 作为库使用 除了作为一个可运行的 CLI 工具外,D2 还可以用于在 Go 程序中生成图表。 获取示例,请参阅 [./docs/examples/lib](./docs/examples/lib)。这篇[博客 文章](https://terrastruct.com/blog/post/generate-diagrams-programmatically/)也演示了一个 完整的、可运行的示例,展示了如何将 D2 作为库用于实际用例。 ## 主题 D2 包含多种官方主题,让您可以开箱即用地为图表设置精美的样式。 浏览可用的主题或创建和贡献您自己的作品,请参阅 [./d2themes](./d2themes)。 ## 字体 D2 在渲染时内置了 "Source Sans Pro" 字体。如果您希望使用其他的 字体,请参阅 [./d2renderers/d2fonts](./d2renderers/d2fonts)。 ## 导出文件类型 D2 目前支持导出 SVG、PNG 和 PDF 格式。更多格式即将推出。 ## 语言工具 D2 在设计时就考虑到了语言工具。D2 的解析器可以从一个 出错的程序中解析出多个错误,具有自动格式化、语法高亮功能,我们还计划支持 LSP 及 更多功能。良好的语言工具是创建和维护大型图表所必需的。 适用于 VSCode 和 Vim 的扩展可以在[相关项目](#related)部分找到。 ## 插件 D2 的设计具有可扩展性和可组合性。插件系统允许您 更换布局引擎并自定义渲染管线。插件可以与 构建捆绑在一起,或者作为独立的二进制文件单独安装。 **布局引擎**: - [dagre](https://github.com/dagrejs/dagre)(默认,内置):一个快速的、有向图 布局引擎,可生成分层/分层布局。基于 Graphviz 的 DOT 算法。 - [ELK](https://github.com/kieler/elkjs)(内置):一个有向图布局引擎, 特别适合具有固有方向和端口的节点-链接图。 - [TALA](https://github.com/terrastruct/TALA)(二进制):专为 软件架构图设计的新型布局引擎。需要单独安装,请访问 Github 页面了解更多信息。 D2 打算与各种布局引擎集成,例如 `dot`,以及 单一用途的布局类型,如序列图。您可以选择任何您喜欢且 最适合您正在制作的图表的布局引擎。 ## 对比 要与其他流行的文本转图表工具进行对比,请参阅 [https://text-to-diagram.com](https://text-to-diagram.com)。 ## 许可证 基于 Mozilla Public License 2.0 开源。详见 [./LICENSE.txt](./LICENSE.txt)。 ## 相关项目 我们一直在致力于开发新的插件、集成和扩展。欢迎对任何 官方或社区插件做出贡献。如果您在工作流程中的某个地方 想使用 D2,请随时开启一个讨论。我们的精力有限,通常会 选择需求最高的项目来开发。如果您自己使用 D2 做出了很酷的东西, 请告诉我们,我们将很乐意将其收录在这里! ### 官方插件 - **VSCode 扩展**:[https://github.com/terrastruct/d2-vscode](https://github.com/terrastruct/d2-vscode) - **Vim 扩展**:[https://github.com/terrastruct/d2-vim](https://github.com/terrastruct/d2-vim) - **Obsidian 插件**:[https://github.com/terrastruct/d2-obsidian](https://github.com/terrastruct/d2-obsidian) - **Slack 应用**:[https://d2lang.com/tour/slack](https://d2lang.com/tour/slack) - **Discord 插件**:[https://d2lang.com/tour/discord](https://d2lang.com/tour/discord) ### 社区插件 - **Tree-sitter 语法**:[https://github.com/ravsii/tree-sitter-d2](https://github.com/ravsii/tree-sitter-d2) - **Emacs major mode**:[https://github.com/andorsk/d2-mode](https://github.com/andorsk/d2-mode) - **Goldmark 扩展**:[https://github.com/FurqanSoftware/goldmark-d2](https://github.com/FurqanSoftware/goldmark-d2) - **Telegram 机器人**:[https://github.com/meinside/telegram-d2-bot](https://github.com/meinside/telegram-d2-bot) - **Postgres 导入器**:[https://github.com/zekenie/d2-erd-from-postgres](https://github.com/zekenie/d2-erd-from-postgres) - **Structurizr 转 D2 导出器**:[https://github.com/goto1134/structurizr-d2-exporter](https://github.com/goto1134/structurizr-d2-exporter) - **MdBook 预处理器**:[https://github.com/danieleades/mdbook-d2](https://github.com/danieleades/mdbook-d2) - **ROS2 D2 Exporter**:[https://github.com/Greenroom-Robotics/ros-d2](https://github.com/Greenroom-Robotics/ros-d2) - **D2 org-mode 支持**:[https://github.com/xcapaldi/ob-d2](https://github.com/xcapaldi/ob-d2) - **Python D2 图表构建器**:[https://github.com/MrBlenny/py-d2](https://github.com/MrBlenny/py-d2) - **Clojure D2 转换器**:[https://github.com/judepayne/dictim](https://github.com/judepayne/dictim) - **JavaScript D2 图表构建器**:[https://github.com/Kreshnik/d2lang-js](https://github.com/Kreshnik/d2lang-js) - **C# & dotnet SDK**:[https://github.com/Stephanvs/d2lang-cs](https://github.com/Stephanvs/d2lang-cs) - **Maven 插件**:[https://github.com/andrinmeier/unofficial-d2lang-maven-plugin](https://github.com/andrinmeier/unofficial-d2lang-maven-plugin) - **Confluence 插件**:[https://github.com/andrinmeier/unofficial-d2lang-confluence-plugin](https://github.com/andrinmeier/unofficial-d2lang-confluence-plugin) - **CIL (C#, Visual Basic, F#, C++ CLR) 转 D2**:[https://github.com/HugoVG/AppDiagram](https://github.com/HugoVG/AppDiagram) - **D2 代码片段(适用于文本编辑器)**:[https://github.com/Paracelsus-Rose/D2-Language-Code-Snippets](https://github.com/Paracelsus-Rose/D2-Language-Code-Snippets) - **Mongo 转 D2**:[https://github.com/novuhq/mongo-to-D2](https://github.com/novuhq/mongo-to-D2) - **Pandoc 过滤器 (TypeScript)**:[https://github.com/ram02z/d2-filter](https://github.com/ram02z/d2-filter) - **Pandoc 过滤器 (Python)**:[https://github.com/fliiiix/pandoc-d2-filter](https://github.com/fliiiix/pandoc-d2-filter) - **Logseq-D2**:[https://github.com/b-yp/logseq-d2](https://github.com/b-yp/logseq-d2) - **ent2d2**:[https://github.com/tmc/ent2d2](https://github.com/tmc/ent2d2) - **MkDocs 插件**:[https://github.com/landmaj/mkdocs-d2-plugin](https://github.com/landmaj/mkdocs-d2-plugin) - **Remark 插件**:[https://github.com/mech-a/remark-d2](https://github.com/mech-a/remark-d2) - **VitePress 插件**:[https://github.com/BadgerHobbs/vitepress-plugin-d2](https://github.com/BadgerHobbs/vitepress-plugin-d2) - **Zed 扩展**:[https://github.com/gabeidx/zed-d2](https://github.com/gabeidx/zed-d2) - **Hexo 博客扩展**:[https://github.com/leverimmy/hexo-d2](https://github.com/leverimmy/hexo-d2) - **Rehype 插件**:[https://github.com/stereobooster/beoe/tree/main/packages/rehype-d2](https://github.com/stereobooster/beoe/tree/main/packages/rehype-d2) - **AsyncAPI 转 D2**:[https://github.com/holydocs/messageflow](https://github.com/holydocs/messageflow) - **数据库模式转 D2**:[https://github.com/holydocs/dberd](https://github.com/holydocs/dberd) ### 杂项 - **对比站点**:[https://github.com/terrastruct/text-to-diagram-site](https://github.com/terrastruct/text-to-diagram-site) - **演练场**:[https://github.com/terrastruct/d2-playground](https://github.com/terrastruct/d2-playground) - **IDE(付费)**:[https://d2studio.ai](https://d2studio.ai) - **语言文档**:[https://github.com/terrastruct/d2-docs](https://github.com/terrastruct/d2-docs) - **托管图标**:[https://icons.terrastruct.com](https://icons.terrastruct.com) ## 常见问题 - D2 会收集遥测数据吗? - 不会,D2 在安装后不会使用网络连接,除了会定期从 Github 检查版本更新。 - D2 需要浏览器才能运行吗? - 不需要,D2 可以完全在服务器端运行。 - 下一个版本会有什么新内容? - 请参阅 [./ci/release/changelogs/next.md](./ci/release/changelogs/next.md)。 - 我有问题或需要帮助。 - 获取帮助的最佳方式是在 [D2 Discord](https://discord.gg/NF6X8K4eDq) 上提问 - 我有功能请求、提议或错误报告。 - 请提交一个 Github Issue。 - 我有私密咨询。 - 请通过 [hi@d2lang.com](hi@d2lang.com) 联系我们。 ## 使用 D2 进行文档编写的著名开源项目 你是否有或看到过包含 `.d2` 文件的开源项目?请提交一个 PR 将其添加到 这个使用 D2 的精选项目列表中。 - [ElasticSearch](https://github.com/elastic/beats/blob/main/libbeat/publisher/queue/proxy/diagrams/broker.d2) - [Sourcegraph](https://handbook.sourcegraph.com/departments/engineering/managed-services/telemetry-gateway/#dev-architecture-diagram) - [Temporal](https://github.com/temporalio/temporal/blob/0be2681c994470c7c61ea88e4fcef89bb4024e58/docs/_assets/matching-context.d2) - [Tauri](https://v2.tauri.app/concept/inter-process-communication/) - Rust GUI 框架 (78.5k 星) - [Intellij](https://github.com/JetBrains/intellij-community/blob/45bcfc17a3f3e0d8548bc69e922d4ca97ac21b2b/platform/settings/docs/topics/overview.md) - [Coder](https://coder.com/blog/managing-templates-in-coder) - [UC 伯克利](https://github.com/ucb-bar/hammer/blob/2b5c04d7b7d9ee3c73575efcd7ee0698bd5bfa88/doc/Hammer-Use/hier.d2) - [Coronacheck](https://github.com/minvws/nl-covid19-coronacheck-app-ios/blob/e1567e9d1633b3273c537a105bff0e7d3a57ecfe/Diagrams/client-side-datamodel.d2) - 荷兰用于冠状病毒入境通行证的官方应用。 - [Block 协议](https://github.com/blockprotocol/blockprotocol/blob/db4cf8d422b881e52113aa52467d53115270e2b3/libs/%40blockprotocol/type-system/crate/assets/overview.d2) - Block 协议是一个用于构建和使用数据驱动块的开放标准 (1.2k 星标)。 - [Dagger](https://github.com/dagger/dagger/tree/main/cmd/dagger-graph) - 一个可编程的 CI/CD 引擎,可在容器中运行您的流水线 (8k 星标)。 - [Ivy Wallet](https://github.com/Ivy-Apps/ivy-wallet/blob/8062624bfa65175ec143cdc4038de27a84d38b57/assets/calc_algo.d2) - 适用于 Android 的开源理财应用 (1.1k 星标)。 - [LocalStack](https://docs.localstack.cloud/references/network-troubleshooting/) - 云服务模拟器 (46k 星标) - [Queue Library](https://github.com/golang-queue/queue/tree/master/images) - Queue 是一个用于生成和管理 Goroutine 池的 Golang 库
标签:Android, D2语言, DSL, EVTX分析, Go语言, Mutation, 前端工具, 可视化, 图表库, 图表生成器, 图表脚本语言, 威胁情报, 开发者工具, 文本转图表, 文档编写, 日志审计, 架构图, 标记语言, 流程图, 程序破解, 绘图工具, 绘图引擎, 自动布局, 调试插件