Skip to content
forked from terrastruct/d2

D2 is a modern diagram scripting language that turns text to diagrams.

License

Notifications You must be signed in to change notification settings

CodeNoobKing/d2

 
 

Repository files navigation

D2

A modern diagram scripting language that turns text to diagrams.

Language docs | Cheat sheet

ci release discord twitter license

D2 CLI

Table of Contents

What does D2 look like?

# Actors
hans: Hans Niemann

defendants: {
  mc: Magnus Carlsen
  playmagnus: Play Magnus Group
  chesscom: Chess.com
  naka: Hikaru Nakamura

  mc -> playmagnus: Owns majority
  playmagnus <-> chesscom: Merger talks
  chesscom -> naka: Sponsoring
}

# Accusations
hans -> defendants: 'sueing for $100M'

# Offense
defendants.naka -> hans: Accused of cheating on his stream
defendants.mc -> hans: Lost then withdrew with accusations
defendants.chesscom -> hans: 72 page report of cheating

There is syntax highlighting with the editor plugins linked below.

D2 render example

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

To uninstall:

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

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

D2 as a library

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

import (
  "context"
  "io/ioutil"
  "path/filepath"
  "strings"

  "oss.terrastruct.com/d2/d2compiler"
  "oss.terrastruct.com/d2/d2exporter"
  "oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
  "oss.terrastruct.com/d2/d2renderers/d2svg"
  "oss.terrastruct.com/d2/d2renderers/textmeasure"
  "oss.terrastruct.com/d2/d2themes/d2themescatalog"
)

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

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

import (
  "oss.terrastruct.com/d2/d2renderers/textmeasure"
  "oss.terrastruct.com/d2/d2themes/d2themescatalog"
)

// Create a shape with the ID, "meow"
graph, _, _ = d2oracle.Create(graph, "meow")
// Style the shape green
color := "green"
graph, _ = d2oracle.Set(graph, "meow.style.fill", nil, &color)
// Create a shape with the ID, "cat"
graph, _, _ = d2oracle.Create(graph, "cat")
// Move the shape "meow" inside the container "cat"
graph, _ = d2oracle.Move(graph, "meow", "cat.meow")
// Prints formatted D2 script
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.

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

VSCode extension

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

Vim extension

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

Language docs

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

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

About

D2 is a modern diagram scripting language that turns text to diagrams.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 89.7%
  • Shell 9.4%
  • JavaScript 0.4%
  • Roff 0.2%
  • HTML 0.1%
  • Makefile 0.1%
  • Other 0.1%