D2 is a modern diagram scripting language that turns text to diagrams.
Find a file
Anmol Sethi 087398a3b8 ci: Fix
2022-11-14 14:49:28 -08:00
.github ci: Set $TERM explicitly 2022-11-08 14:30:11 -08:00
ci ci: Fix 2022-11-14 14:49:28 -08:00
cmd ci/release: Checkpoint 2022-11-13 22:49:45 -08:00
d2ast oss 2022-11-03 06:54:49 -07:00
d2chaos oss 2022-11-03 06:54:49 -07:00
d2compiler oss 2022-11-03 06:54:49 -07:00
d2exporter oss 2022-11-03 06:54:49 -07:00
d2format oss 2022-11-03 06:54:49 -07:00
d2graph markdown code tabsize and measurement accuracy 2022-11-07 18:07:04 -08:00
d2layouts k 2022-11-11 20:47:03 -08:00
d2oracle oss 2022-11-03 06:54:49 -07:00
d2parser oss 2022-11-03 06:54:49 -07:00
d2plugin lowercase 2022-11-12 19:45:36 -08:00
d2renderers 2022-11-12 01:31:08PM 2022-11-12 13:31:08 -08:00
d2target oss 2022-11-03 06:54:49 -07:00
d2themes change to warm neutral 2022-11-10 11:01:08 -08:00
docs update theme graphic 2022-11-10 11:19:13 -08:00
e2etests 2022-11-12 01:31:08PM 2022-11-12 13:31:08 -08:00
lib install.sh: Get installs working 2022-11-14 02:13:37 -08:00
testdata oss 2022-11-03 06:54:49 -07:00
.gitignore oss 2022-11-03 06:54:49 -07:00
.gitmodules oss 2022-11-03 06:54:49 -07:00
c.go oss 2022-11-03 06:54:49 -07:00
d2.go oss 2022-11-03 06:54:49 -07:00
go.mod 2022-11-12 09:07:11pm 2022-11-13 22:49:46 -08:00
go.sum 2022-11-12 09:07:11pm 2022-11-13 22:49:46 -08:00
install.sh release: Improve logging 2022-11-14 14:46:52 -08:00
LICENSE.txt oss 2022-11-03 06:54:49 -07:00
make.sh oss 2022-11-03 06:54:49 -07:00
Makefile oss 2022-11-03 06:54:49 -07:00
README.md README: Add install.sh docs 2022-11-13 22:51:13 -08:00

D2

A modern DSL that turns text into diagrams.

Language docs | Cheat sheet

ci release discord twitter license

D2 CLI

Table of Contents

Quickstart (CLI)

To install:

# With --dryrun the install script will print the commands it will use
# to install without actually installing so you know what it's going to do.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --dryrun
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s --

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

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.

Installing from source

go install oss.terrastruct.com/d2

Install

We have precompiled binaries on the releases page for macOS and Linux. For both amd64 and arm64. We will release package manager distributions like .rpm, .deb soon. We also want to get D2 on Homebrew for macOS and release a docker image

For now, if you don't want to install from source, just use our install script: Pass --tala if you want to install our improved but closed source layout engine tala. See the docs on layout engine below.

# With --dryrun the install script will print the commands it will use
# to install without actually installing so you know what it's going to do.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --dryrun
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s --

To uninstall:

curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall --dryrun
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall

warn: Our binary releases aren't fully portable like normal Go binaries due to the C dependency on v8go for executing dagre.

Quickstart (library)

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

import (
	"github.com/terrastruct/d2/d2compiler"
	"github.com/terrastruct/d2/d2exporter"
	"github.com/terrastruct/d2/d2layouts/d2dagrelayout"
	"github.com/terrastruct/d2/d2renderers/textmeasure"
	"github.com/terrastruct/d2/d2themes/d2themescatalog"
)

func main() {
  graph, err := d2compiler.Compile("", strings.NewReader("x -> y"), &d2compiler.CompileOptions{ UTF16: true })
  ruler, err := textmeasure.NewRuler()
  err = graph.SetDimensions(nil, ruler)
  err = d2dagrelayout.Layout(ctx, graph)
  diagram, err := d2exporter.Export(ctx, graph, d2themescatalog.NeutralDefault)
  ioutil.WriteFile(filepath.Join("out.svg"), d2svg.Render(*diagram), 0600)
}

D2 is built to be hackable -- the language has an API built on top of it to make edits programmatically.

import (
  "github.com/terrastruct/d2/d2oracle"
  "github.com/terrastruct/d2/d2format"
)

// ...modifying the diagram `x -> y` from above
// Create a shape with the ID, "meow"
graph, err = d2oracle.Create(graph, "meow")
// Style the shape green
graph, err = d2oracle.Set(graph, "meow.style.fill", "green")
// Create a shape with the ID, "cat"
graph, err = d2oracle.Create(graph, "cat")
// Move the shape "meow" inside the container "cat"
graph, err = d2oracle.Move(graph, "meow", "cat.meow")
// Prints formatted D2 code
println(d2format.Format(graph.AST))

This makes it easy to build functionality on top of D2. Terrastruct uses the above API to implement editing of D2 from mouse actions in a visual interface.

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 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.

Layout engine

D2 currently uses the open-source library dagre as its default layout engine. D2 includes a wrapper around dagre to work around one of its biggest limitations -- the inability to make container-to-container edges.

Dagre was chosen due to its popularity in other tools, but 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.

Terrastruct has created a proprietary layout engine called TALA. It has been designed specifically for software architecture diagrams, though it's good for other domains too. TALA has many advantages over other layout engines, the biggest being that it isn't constrained to hierarchies, or any single type like "radial" or "tree" (as almost all layout engines are). For more information and to download & try TALA, see https://github.com/terrastruct/TALA.

You can just pass --tala to the install script to install tala as well:

curl -fsSL https://d2lang.com/install.sh | sh -s -- --tala --dryrun
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --tala

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

Copyright © 2022 Terrastruct, Inc. Open-source licensed under the Mozilla Public License 2.0.

VSCode extension

https://github.com/terrastruct/d2-vscode

Vim extension

https://github.com/terrastruct/d2-vim

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.
  • I have a question or need help.
    • The best way to get help is to open an Issue, so that it's searchable by others in the future. If you prefer synchronous or just want to chat, you can pop into the help channel of the D2 Discord as well.
  • I have a feature request or proposal.
    • D2 uses Github Issues for everything. Just add a "discussion" label to your Issue.
  • I have a private inquiry.