2022-11-03 13:54:49 +00:00
|
|
|
<div align="center">
|
2022-11-22 00:49:00 +00:00
|
|
|
<img src="./docs/assets/banner.png" alt="D2" />
|
|
|
|
|
<h2>
|
2022-11-22 00:48:22 +00:00
|
|
|
A modern diagram scripting language that turns text to diagrams.
|
2022-11-22 00:49:00 +00:00
|
|
|
</h2>
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-11-08 18:24:32 +00:00
|
|
|
[Language docs](https://d2lang.com) | [Cheat sheet](./docs/assets/cheat_sheet.pdf)
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
[](https://github.com/terrastruct/d2/actions/workflows/ci.yml)
|
|
|
|
|
[](https://github.com/terrastruct/d2/releases)
|
2022-11-09 21:59:00 +00:00
|
|
|
[](https://discord.gg/NF6X8K4eDq)
|
|
|
|
|
[](https://twitter.com/terrastruct)
|
2022-11-03 13:54:49 +00:00
|
|
|
[](./LICENSE.txt)
|
|
|
|
|
|
2022-11-22 00:32:58 +00:00
|
|
|
<img src="./docs/assets/cli.gif" alt="D2 CLI" />
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
# Table of Contents
|
|
|
|
|
|
|
|
|
|
<!-- toc -->
|
|
|
|
|
|
2022-11-22 17:28:45 +00:00
|
|
|
- [What does D2 look like?](#what-does-d2-look-like)
|
2022-11-15 01:05:05 +00:00
|
|
|
- [Quickstart](#quickstart)
|
|
|
|
|
- [Install](#install)
|
|
|
|
|
- [D2 as a library](#d2-as-a-library)
|
2022-11-03 13:54:49 +00:00
|
|
|
- [Themes](#themes)
|
|
|
|
|
- [Fonts](#fonts)
|
|
|
|
|
- [Export file types](#export-file-types)
|
|
|
|
|
- [Language tooling](#language-tooling)
|
2022-11-15 00:50:21 +00:00
|
|
|
- [Plugins](#plugins)
|
2022-11-03 13:54:49 +00:00
|
|
|
- [Comparison](#comparison)
|
|
|
|
|
- [Contributing](#contributing)
|
|
|
|
|
- [License](#license)
|
|
|
|
|
- [Related](#related)
|
|
|
|
|
* [VSCode extension](#vscode-extension)
|
|
|
|
|
* [Vim extension](#vim-extension)
|
|
|
|
|
* [Misc](#misc)
|
2022-11-15 00:50:21 +00:00
|
|
|
- [FAQ](#faq)
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
<!-- tocstop -->
|
|
|
|
|
|
2022-11-22 17:20:03 +00:00
|
|
|
# What does D2 look like?
|
|
|
|
|
|
|
|
|
|
```d2
|
|
|
|
|
# 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
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:31:24 +00:00
|
|
|
> There is syntax highlighting with the editor plugins linked below.
|
2022-11-22 17:28:45 +00:00
|
|
|
|
2022-11-22 17:20:56 +00:00
|
|
|
<img src="./docs/assets/syntax.png" alt="D2 render example" width="50%" />
|
2022-11-22 17:20:03 +00:00
|
|
|
|
2022-11-15 01:05:05 +00:00
|
|
|
## Quickstart
|
2022-11-09 22:46:19 +00:00
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
The most convenient way to use D2 is to just run it as a CLI executable to
|
|
|
|
|
produce SVGs from `.d2` files.
|
|
|
|
|
|
|
|
|
|
```sh
|
2022-11-14 23:23:16 +00:00
|
|
|
# First, install D2
|
|
|
|
|
curl -fsSL https://d2lang.com/install.sh | sh -s --
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
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`.
|
|
|
|
|
|
2022-11-15 01:05:05 +00:00
|
|
|
## Install
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-11-21 17:32:59 +00:00
|
|
|
The easiest way to install is with our install script:
|
2022-11-20 13:19:39 +00:00
|
|
|
|
2022-11-14 06:47:32 +00:00
|
|
|
```sh
|
|
|
|
|
curl -fsSL https://d2lang.com/install.sh | sh -s --
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To uninstall:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-21 17:33:20 +00:00
|
|
|
For detailed installation docs, with alternative methods and examples for each OS, see
|
|
|
|
|
[./docs/INSTALL.md](./docs/INSTALL.md).
|
|
|
|
|
|
2022-11-15 01:05:05 +00:00
|
|
|
## D2 as a library
|
2022-11-09 22:46:19 +00:00
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
In addition to being a runnable CLI tool, D2 can also be used to produce diagrams from
|
|
|
|
|
Go programs.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
import (
|
2022-11-15 00:50:21 +00:00
|
|
|
"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"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
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"
|
2022-11-03 18:48:36 +00:00
|
|
|
graph, err = d2oracle.Create(graph, "meow")
|
2022-11-03 13:54:49 +00:00
|
|
|
// Style the shape green
|
2022-11-03 18:48:36 +00:00
|
|
|
graph, err = d2oracle.Set(graph, "meow.style.fill", "green")
|
2022-11-03 13:54:49 +00:00
|
|
|
// Create a shape with the ID, "cat"
|
2022-11-03 18:48:36 +00:00
|
|
|
graph, err = d2oracle.Create(graph, "cat")
|
2022-11-03 13:54:49 +00:00
|
|
|
// Move the shape "meow" inside the container "cat"
|
2022-11-03 18:48:36 +00:00
|
|
|
graph, err = d2oracle.Move(graph, "meow", "cat.meow")
|
2022-11-03 13:54:49 +00:00
|
|
|
// 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](./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](./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](#related) section.
|
|
|
|
|
|
2022-11-12 18:03:32 +00:00
|
|
|
## Plugins
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-11-12 18:03:32 +00:00
|
|
|
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.
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-11-12 18:03:32 +00:00
|
|
|
**Layout engines**:
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-11-12 18:03:32 +00:00
|
|
|
- [dagre](https://github.com/dagrejs/dagre) (default, bundled): A fast, directed graph
|
|
|
|
|
layout engine that produces layered/hierarchical layouts. Based on Graphviz's DOT
|
|
|
|
|
algorithm.
|
|
|
|
|
- [ELK](https://github.com/kieler/elkjs) (bundled): A directed graph layout engine
|
|
|
|
|
particularly suited for node-link diagrams with an inherent direction and ports.
|
|
|
|
|
- [TALA](https://github.com/terrastruct/TALA) (binary): Novel layout engine designed
|
2022-11-14 23:23:16 +00:00
|
|
|
specifically for software architecture diagrams. Requires separate install, visit the
|
|
|
|
|
Github page for more.
|
2022-11-12 18:03:32 +00:00
|
|
|
|
|
|
|
|
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.
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
## Comparison
|
|
|
|
|
|
|
|
|
|
For a comparison against other popular text-to-diagram tools, see
|
|
|
|
|
[https://text-to-diagram.com](https://text-to-diagram.com).
|
|
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
|
|
Contributions are welcome! See [./docs/CONTRIBUTING.md](./docs/CONTRIBUTING.md).
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
2022-11-22 00:32:58 +00:00
|
|
|
Open sourced under the Mozilla Public License 2.0. See [./LICENSE.txt](./LICENSE.txt).
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
## Related
|
|
|
|
|
|
|
|
|
|
### VSCode extension
|
|
|
|
|
|
|
|
|
|
[https://github.com/terrastruct/d2-vscode](https://github.com/terrastruct/d2-vscode)
|
|
|
|
|
|
|
|
|
|
### Vim extension
|
|
|
|
|
|
|
|
|
|
[https://github.com/terrastruct/d2-vim](https://github.com/terrastruct/d2-vim)
|
|
|
|
|
|
2022-11-15 02:29:59 +00:00
|
|
|
### Language docs
|
|
|
|
|
|
|
|
|
|
[https://github.com/terrastruct/d2-docs](https://github.com/terrastruct/d2-docs)
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
### Misc
|
|
|
|
|
|
2022-11-15 02:29:59 +00:00
|
|
|
- [https://github.com/terrastruct/text-to-diagram-site](https://github.com/terrastruct/text-to-diagram-site)
|
2022-11-09 21:55:33 +00:00
|
|
|
|
|
|
|
|
## FAQ
|
|
|
|
|
|
|
|
|
|
- Does D2 collect telemetry?
|
|
|
|
|
- No, D2 does not use an internet connection after installation, except to check for
|
|
|
|
|
version updates from Github periodically.
|
2022-11-12 18:03:32 +00:00
|
|
|
- Does D2 need a browser to run?
|
|
|
|
|
- No, D2 can run entirely server-side.
|
2022-11-09 21:55:33 +00:00
|
|
|
- I have a question or need help.
|
2022-11-15 00:50:21 +00:00
|
|
|
- The best way to get help is to ask on [D2 Discord](https://discord.gg/NF6X8K4eDq)
|
|
|
|
|
- I have a feature request, proposal, or bug report.
|
|
|
|
|
- Please open up a Github Issue.
|
2022-11-09 22:00:12 +00:00
|
|
|
- I have a private inquiry.
|
2022-11-09 22:44:48 +00:00
|
|
|
- Please reach out at [hi@d2lang.com](hi@d2lang.com).
|