MCPcopy Index your code
hub / github.com/terrastruct/d2

github.com/terrastruct/d2 @v0.7.1 sqlite

repository ↗ · DeepWiki ↗ · release v0.7.1 ↗
9,203 symbols 34,342 edges 283 files 461 documented · 5% 16 cross-repo links
README

D2

A modern diagram scripting language that turns text to diagrams.

Docs | Cheat sheet | Comparisons | Playground | IDE

ci daily release changelog npm version discord twitter license

D2 Playground button D2 Studio 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
<img width="400px" src="https://github.com/terrastruct/d2/raw/v0.7.1/docs/assets/example.svg" alt="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 oss.terrastruct.com/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. This blog post also demos a complete, runnable example of using D2 as a library for a real-world use case.

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 currently supports SVG, PNG and PDF exports. More coming soon.

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.

Plugins

D2 is designed to be extensible and composable. The plugin system allows you to change out layout engines and customize the rendering pipeline. Plugins can either be bundled with the build or separately installed as a standalone binary.

Layout engines:

  • dagre (default, bundled): A fast, directed graph layout engine that produces layered/hierarchical layouts. Based on Graphviz's DOT algorithm.
  • ELK (bundled): A directed graph layout engine particularly suited for node-link diagrams with an inherent direction and ports.
  • TALA (binary): Novel layout engine designed specifically for software architecture diagrams. Requires separate install, visit the Github page for more.

D2 intends to integrate with a variety of layout engines, e.g. dot, as well as single-purpose layout types like sequence diagrams. You can choose whichever layout engine you like and works best for the diagram you're making.

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.

Related

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

  • Does D2 collect telemetry?
  • No, D2 does not use an internet connection after installation, except to check for version updates from Github periodically.
  • Does D2 need a browser to run?
  • No, D2 can run entirely server-side.
  • What's coming in the next release?
  • See ./ci/release/changelogs/next.md.
  • I have a question or need help.
  • The best way to get help is to ask on D2 Discord
  • I have a feature request, proposal, or bug report.
  • Please open up a Github Issue.
  • I have a private inquiry.
  • Please reach out at hi@d2lang.com.

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.

Extension points exported contracts — how you extend this code

ArrayNode (Interface)
ArrayNode is implemented by nodes that may be children of Arrays. [14 implementers]
d2ast/d2ast.go
Scalar (Interface)
(no doc) [8 implementers]
d2js/js/index.d.ts
Value (Interface)
(no doc) [14 implementers]
d2ir/d2ir.go
Intersectable (Interface)
(no doc) [4 implementers]
lib/geo/segment.go
Plugin (Interface)
(no doc) [3 implementers]
d2plugin/plugin.go
JSRunner (Interface)
(no doc) [2 implementers]
lib/jsrunner/jsrunner.go
Set (Interface)
(no doc) [2 implementers]
d2renderers/d2ascii/charset/charset.go
DiagramObject (Interface)
(no doc) [2 implementers]
d2renderers/d2svg/d2svg.go

Core symbols most depended-on inside this repo

BD
called by 5974
d2layouts/d2elklayout/elk.js
bcb
called by 1841
d2layouts/d2elklayout/elk.js
mdb
called by 1738
d2layouts/d2elklayout/elk.js
Q
called by 1002
d2renderers/d2latex/mathjax.js
vNb
called by 870
d2layouts/d2elklayout/elk.js
mlb
called by 798
d2layouts/d2elklayout/elk.js
vbb
called by 740
d2layouts/d2elklayout/elk.js
GC
called by 689
d2layouts/d2elklayout/elk.js

Shape

Function 7,638
Method 1,227
Struct 233
Interface 44
Class 34
TypeAlias 22
FuncType 5

Languages

TypeScript76%
Go24%

Modules by API surface

d2layouts/d2elklayout/elk.js6,135 symbols
d2layouts/d2dagrelayout/dagre.js357 symbols
d2ast/d2ast.go247 symbols
d2renderers/d2latex/polyfills.js178 symbols
d2ir/d2ir.go165 symbols
d2renderers/d2sketch/rough.js142 symbols
d2graph/d2graph.go73 symbols
d2js/js/wasm/wasm_exec.js51 symbols
d2parser/parse.go49 symbols
d2oracle/edit.go48 symbols
lib/font/subsetFont.go47 symbols
d2target/d2target.go44 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

github.com/andybalholm/brotliv1.2.0 · 1×
github.com/andybalholm/cascadiav1.3.2 · 1×
github.com/dlclark/regexp2v1.11.4 · 1×
github.com/dop251/gojav0.0.0-2024092712342 · 1×
github.com/dsoprea/go-exif/v3v3.0.1 · 1×
github.com/dsoprea/go-loggingv0.0.0-2020071018492 · 1×
github.com/dsoprea/go-png-image-structure/v2v2.0.0-2021051221032 · 1×

For agents

$ claude mcp add d2 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact