d2 README

The project's README from github.com/d2lang/d2. As one markdown file: README.md.

D2

A modern diagram scripting language that turns text to diagrams.

Docs | Cheat sheet | Comparisons | Playground

ci weekly race tests release changelog npm version discord license

D2 Playground button

https://user-images.githubusercontent.com/3120367/206125010-bd1fea8e-248a-43e7-8f85-0bbfca0c6e2a.mp4

Table of Contents

What does D2 look like?

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

Open in playground

For more examples, see ./docs/examples.

Quickstart

The most convenient way to use D2 is to just run it as a CLI executable to produce SVGs from .d2 files.

# First, install D2
curl -fsSL https://d2lang.com/install.sh | sh -s --

echo 'x -> y -> z' > in.d2
d2 --watch in.d2 out.svg

A browser window will open with out.svg and live-reload on changes to in.d2.

Install

The easiest way to install is with our install script:

curl -fsSL https://d2lang.com/install.sh | sh -s --

You can run the install script with --dry-run to see the commands that will be used to install without executing them.

Or if you have Go installed you can install from source though you won't get the manpage:

go install github.com/d2lang/d2@latest

You can also install a release from source which will include manpages. See ./docs/INSTALL.md#source-release.

To uninstall with the install script:

curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall

For detailed installation docs, see ./docs/INSTALL.md. We demonstrate alternative methods and examples for each OS.

As well, the functioning of the install script is described in detail to alleviate any concern of its use. We recommend using your OS's package manager directly instead for improved security but the install script is by no means insecure.

D2 as a library

In addition to being a runnable CLI tool, D2 can also be used to produce diagrams from Go programs.

For examples, see ./docs/examples/lib.

Themes

D2 includes a variety of official themes to style your diagrams beautifully right out of the box. See ./d2themes to browse the available themes and make or contribute your own creation.

Fonts

D2 ships with "Source Sans Pro" as the font in renders. If you wish to use a different one, please see ./d2renderers/d2fonts.

Export file types

D2 supports SVG, PNG, GIF, PDF, and PPTX exports.

Language tooling

D2 is designed with language tooling in mind. D2's parser can parse multiple errors from a broken program, has an autoformatter, syntax highlighting, and we have plans for LSP's and more. Good language tooling is necessary for creating and maintaining large diagrams.

The extensions for VSCode and Vim can be found in the Related section.

Layout engines

D2 bundles three layout engines. Select one with --layout=dagre, --layout=elk, or --layout=tala. You can also set D2_LAYOUT or vars.d2-config.layout-engine in D2 source. Run d2 layout to list the engines or d2 layout <name> for their configuration options.

D2 also handles specialized layouts such as sequence diagrams and grids. Go applications can use the layout packages directly or provide layout and routing functions through d2lib.

Sketch-mode rendering uses rough-go, a native Go compatibility port of the Rough.js 4.6.6 rendering surface used by D2.

LaTeX labels use mathjax-go, a native Go port of the MathJax 3.2.2 TeX-to-SVG pipeline.

Comparison

For a comparison against other popular text-to-diagram tools, see https://text-to-diagram.com.

Contributing

Contributions are welcome! See ./docs/CONTRIBUTING.md.

License

Open sourced under the Mozilla Public License 2.0. See ./LICENSE.txt.

We are constantly working on new plugins, integrations, extensions. Contributions are welcome in any official or community plugins. If you have somewhere in your workflow that you want to use D2, feel free to open a discussion. We have limited bandwidth and usually choose the most high-demand ones to work on. If you make something cool with D2 yourself, let us know and we'll be happy to include it here!

Official plugins

Community plugins

Misc

FAQ

Notable open-source projects documenting with D2

Do you have or see an open-source project with .d2 files? Please submit a PR adding to this selected list of featured projects using D2.

d2 (d2lang/d2)

Searching Database…

25.6K ·GitHub
这是干什么的?D2 是一种开源的图表脚本语言:你用一个 .d2 文件以纯文本的方式描述图表,它会把这段文本渲染成真正的图。
那 D2 到底是我要写的一种语言,还是我要运行的一个工具,还是一个库?既是语言也是命令行工具:你编写 .d2 文件,运行 D2 可执行程序来生成图片;Go 程序也可以把 D2 作为库来嵌入使用。
我怎样才能渲染出第一张图?先用官方安装脚本安装命令行工具(用 --dry-run 可以预览它会做什么),然后对一个 .d2 文件运行它——浏览器会打开一个标签页显示 SVG,并在你编辑时实时刷新。
用之前需要先装好 Go 吗?不需要。安装脚本会提供一个现成的二进制文件;只有从源码安装时才需要 Go,而且那种方式还不会附带 manpage 手册页。
它是给写软件文档的开发者用的,还是面向一般画图需求的?主要是给为软件系统写文档的开发者用的:README 强调了维护大型图表的工具能力,ElasticSearch、Sourcegraph、Temporal、Tauri 等项目都在自己的代码仓库里保存 .d2 文件。
预览是在浏览器里打开的——那 D2 本身运行时需要浏览器或图形界面吗?不需要浏览器或图形界面——浏览器标签页只是一个可选的实时预览,D2 完全可以在服务器端运行。这个命令行工具支持 Windows、macOS 和 Linux。
这个命令行工具会偷偷联网或收集遥测数据吗?没有任何遥测:安装完成后,D2 除了偶尔到 GitHub 检查版本更新之外,不会建立任何网络连接。
它能导出成哪些文件格式?SVG、PNG、GIF、PDF 和 PPTX,同一份图表源文件可以同时用于文档、网页和幻灯片。
图表能做成手绘风格,或者使用数学公式标签吗?可以——草图模式通过一个 Go 版的 Rough.js 渲染出手绘风格,数学标签则通过原生的 MathJax 移植版支持 LaTeX。
我不安装任何东西就能试用吗?可以,README 里链接了一个在线演练场(playground),你可以在浏览器里编写 D2 并看到渲染出来的图表。
Creator
d2lang
Size
513.2 MB
Stars
25.6K
Age
4y
Commits · 90 Days