D2 is a modern diagram scripting language that turns text to diagrams.
Find a file
Alexander Wang 524c089a74 oss
Co-authored-by: Anmol Sethi <hi@nhooyr.io>
2022-11-03 06:54:49 -07:00
.github oss 2022-11-03 06:54:49 -07:00
ci oss 2022-11-03 06:54:49 -07:00
cmd oss 2022-11-03 06:54:49 -07: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 oss 2022-11-03 06:54:49 -07:00
d2layouts/d2dagrelayout oss 2022-11-03 06:54:49 -07:00
d2oracle oss 2022-11-03 06:54:49 -07:00
d2parser oss 2022-11-03 06:54:49 -07:00
d2plugin oss 2022-11-03 06:54:49 -07:00
d2renderers oss 2022-11-03 06:54:49 -07:00
d2target oss 2022-11-03 06:54:49 -07:00
d2themes oss 2022-11-03 06:54:49 -07:00
docs oss 2022-11-03 06:54:49 -07:00
e2etests oss 2022-11-03 06:54:49 -07:00
lib oss 2022-11-03 06:54:49 -07: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 oss 2022-11-03 06:54:49 -07:00
go.sum oss 2022-11-03 06:54:49 -07: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 oss 2022-11-03 06:54:49 -07:00

D2

A modern DSL that turns text into diagrams.

Language docs: https://d2-lang.com

ci release discord twitter license godoc

D2 CLI

Table of Contents

Quickstart (CLI)

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

go install oss.terrastruct.com/d2

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.

MacOS

Homebrew package coming soon.

Linux/Windows

We have precompiled binaries on the releases page. D2 will be added to OS-respective package managers soon.

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.

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.

Dependencies

D2 is light on third-party dependencies in the source code. Note that these are bundled with D2, you do not have to separately install anything.

Dependency What it does
slog logging (deprecating it is a TODO)
goldmark, goquery Markdown rendering
chroma syntax highlighting code snippets
pflag, fsnotify, websocket CLI functions
v8go Run Javascript (e.g. Dagre layout engine)
gonum Bezier curve stuff (rendering)

The rest are helpers we've open-sourced. E.g. diff for our testing framework.

VSCode extension

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

Vim extension

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

Misc