Skip to content

Commit 403d6e1

Browse files
authored
Merge pull request #264 from terrastruct/legend
document 0.7.0 features
2 parents 4f56a0c + e7cb2a4 commit 403d6e1

31 files changed

+3297
-1957
lines changed

docs/tour/globs.md

+19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import GlobsRecursive2 from '@site/static/d2/globs-recursive-2.d2';
1111
import GlobsFilter from '@site/static/d2/globs-filter.d2';
1212
import GlobsFilter2 from '@site/static/d2/globs-filter-2.d2';
1313
import GlobsFilter3 from '@site/static/d2/globs-filter-3.d2';
14+
import GlobsFilterEndpoints from '@site/static/d2/globs-filter-endpoints.d2';
1415
import GlobsFilterGlobValue from '@site/static/d2/globs-filter-glob-value.d2';
1516
import GlobsInverseFilter from '@site/static/d2/globs-inverse-filter.d2';
1617
import GlobsNested from '@site/static/d2/globs-nested.d2';
@@ -164,6 +165,24 @@ means the key must be specified.
164165

165166
<div style={{width: 600}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/globs-filter-glob-value.svg2')}}></div>
166167

168+
169+
### Connection endpoint filters
170+
171+
Connections can be filtered by properties on their source and destination shapes.
172+
173+
<CodeBlock className="language-d2">
174+
{GlobsFilterEndpoints}
175+
</CodeBlock>
176+
177+
<div style={{width: 600}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/globs-filter-endpoints.svg2')}}></div>
178+
179+
Endpoint filters also work with IDs, e.g. `&src: b`.
180+
181+
:::info
182+
Endpoint IDs are absolute. For example, `a.c` instead of just `c`, even if the glob is
183+
declared within `a`.
184+
:::
185+
167186
## Inverse filters
168187

169188
Use `!&` to inverse-filter what globs target.

docs/tour/icons.md

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ You can use any URL as value.
2323

2424
<div style={{width: "200px", margin: "0 auto 20px auto"}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/icons-1.svg2')}}></div>
2525

26-
:::info
27-
Icons on connections coming soon.
28-
:::
29-
3026
:::info
3127
Using the D2 CLI locally? You can specify local images like `icon: ./my_cat.png`.
3228
:::

docs/tour/legend.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import CodeBlock from '@theme/CodeBlock';
2+
import Legend from '@site/static/d2/legend.d2';
3+
import LegendHidden from '@site/static/d2/legend-hidden.d2';
4+
5+
# Legend
6+
7+
Use a special variable, `d2-legend`, to declare a legend.
8+
9+
<CodeBlock className="language-d2">
10+
{Legend}
11+
</CodeBlock>
12+
13+
<div style={{width: "100%"}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/legend.svg2')}}></div>
14+
15+
## Hiding shapes
16+
17+
Since `a -> b` declares 3 things (1 connection and 2 shapes), 3 things will show up on the
18+
legend. If you only intended to show the connection in the legend, you can set the opacity
19+
of shapes to exclude them from the legend.
20+
21+
<CodeBlock className="language-d2">
22+
{LegendHidden}
23+
</CodeBlock>
24+
25+
<div style={{width: "100%"}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/legend-hidden.svg2')}}></div>

docs/tour/man.md

+102-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ NAME
1414
d2 – compiles and renders d2 diagrams into svgs.
1515
1616
SYNOPSIS
17-
d2 [--watch false] [--theme 0] file.d2 [file.svg | file.png]
17+
d2 [--watch false] [--theme 0] [--salt string] file.d2
18+
[file.svg | file.png]
1819
d2 layout [name]
1920
d2 fmt file.d2 ...
21+
d2 play file.d2
22+
d2 validate file.d2
2023
2124
DESCRIPTION
2225
d2 compiles and renders file.d2 to file.svg | file.png.
@@ -88,7 +91,7 @@ OPTIONS
8891
--animate-interval 0
8992
If given, multiple boards are packaged as 1 SVG which
9093
transitions through each board at the interval (in
91-
milliseconds). Can only be used with SVG exports.
94+
milliseconds). Can only be used with SVG and GIF exports.
9295
9396
--browser true
9497
Browser executable that watch opens. Setting to 0 opens no
@@ -125,11 +128,30 @@ OPTIONS
125128
out and exiting. When rendering a large diagram, it is
126129
recommended to increase this value.
127130
131+
--check false
132+
Check that the specified files are formatted correctly.
133+
134+
--salt string
135+
Add a salt value to ensure the output uses unique IDs. This
136+
is useful when generating multiple identical diagrams to be
137+
included in the same HTML doc, so that duplicate id's do not
138+
cause invalid HTML. The salt value is a string that will be
139+
appended to IDs in the output..
140+
128141
-h, --help Print usage information and exit.
129142
130143
-v, --version
131144
Print version information and exit.
132145
146+
--stdout-format string
147+
Set the output format when writing to stdout. Supported
148+
formats are: png, svg. Only used when output is set to stdout
149+
(-).
150+
151+
--no-xml-tag false
152+
Omit XML tag (<?xml ...?>) from output SVG files. Useful when
153+
generating SVGs for direct HTML embedding.
154+
133155
SUBCOMMANDS
134156
layout Lists available layout engine options with short help.
135157
@@ -140,11 +162,88 @@ SUBCOMMANDS
140162
themes Lists available themes.
141163
142164
fmt file.d2 ...
143-
Format all passed files.
165+
Format all passed files
166+
167+
play file.d2
168+
Opens the file in playground, an online web viewer
169+
(https://play.d2lang.com)
170+
171+
validate file.d2
172+
Validates file.d2
173+
174+
ENVIRONMENT VARIABLES
175+
Many flags can also be set with environment variables.
176+
177+
D2_WATCH
178+
See -w[atch] flag.
179+
180+
D2_LAYOUT
181+
See -l[ayout] flag.
182+
183+
D2_THEME
184+
See -t[heme] flag.
185+
186+
D2_DARK_THEME
187+
See --dark-theme flag.
188+
189+
D2_PAD See --pad flag.
190+
191+
D2_CENTER
192+
See --center flag.
193+
194+
D2_SKETCH
195+
See -s[ketch] flag.
196+
197+
D2_BUNDLE
198+
See -b[undle] flag.
199+
200+
D2_FORCE_APPENDIX
201+
See --force-appendix flag.
202+
203+
D2_FONT_REGULAR
204+
See --font-regular flag.
205+
206+
D2_FONT_ITALIC
207+
See --font-italic flag.
208+
209+
D2_FONT_BOLD
210+
See --font-bold flag.
211+
212+
D2_FONT_SEMIBOLD
213+
See --font-semibold flag.
214+
215+
D2_ANIMATE_INTERVAL
216+
See --animate-interval flag.
217+
218+
D2_TIMEOUT
219+
See --timeout flag.
220+
221+
D2_CHECK
222+
See --check flag.
223+
224+
DEBUG See -d[ebug] flag.
225+
226+
IMG_CACHE
227+
See --img-cache flag.
228+
229+
HOST See -h[ost] flag.
230+
231+
PORT See -p[ort] flag.
232+
233+
BROWSER
234+
See --browser flag.
235+
236+
D2_STDOUT_FORMAT
237+
See --stdout-format flag.
238+
239+
D2_NO_XML_TAG
240+
See --no-xml-tag flag.
144241
145242
SEE ALSO
146243
d2plugin-tala(1)
147244
148245
AUTHORS
149246
Terrastruct Inc.
247+
248+
macOS 14.1 March 12, 2025 macOS 14.1
150249
```

docs/tour/models.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import CodeBlock from '@theme/CodeBlock';
2+
import Suspend from '@site/static/d2/suspend.d2';
3+
import Suspend2 from '@site/static/d2/suspend-2.d2';
4+
import Suspend3 from '@site/static/d2/suspend-3.d2';
5+
import Suspend4 from '@site/static/d2/suspend-4.d2';
6+
7+
# Models
8+
9+
Let's say you want to define all models and relationships between models once, then
10+
display them in different ways.
11+
12+
First, let's define the models and relationships.
13+
14+
<CodeBlock className="language-d2-incomplete">
15+
{Suspend}
16+
</CodeBlock>
17+
18+
<div className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/suspend.svg2')}}></div>
19+
20+
We will use this as the example for multiple views of the same model. How do we treat
21+
these as models and not just ordinary shapes and connections? We use the following
22+
keywords:
23+
24+
1. `suspend`: This marks the shape or connection for deletion
25+
2. `unsuspend`: This restores the shape or connection
26+
27+
Since models are meant to be invisible until used, we define models at the top of our
28+
diagram and `suspend` all of them with the following globs.
29+
30+
```d2
31+
**: suspend
32+
(** -> **)[*]: suspend
33+
```
34+
35+
We then use globs to selectively `unsuspend` the models we want to show. Let's take a look
36+
at some use cases.
37+
38+
:::info
39+
If you haven't yet, please familiarize yourself with [globs](globs.md).
40+
:::
41+
42+
## Show only top-level
43+
44+
<CodeBlock className="language-d2-incomplete">
45+
{Suspend2}
46+
</CodeBlock>
47+
48+
<div className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/suspend-2.svg2')}}></div>
49+
50+
## Show only connection to X
51+
52+
<CodeBlock className="language-d2-incomplete">
53+
{Suspend3}
54+
</CodeBlock>
55+
56+
<div className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/suspend-3.svg2')}}></div>
57+
58+
## Show only likes
59+
60+
<CodeBlock className="language-d2-incomplete">
61+
{Suspend4}
62+
</CodeBlock>
63+
64+
<div className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/suspend-4.svg2')}}></div>
65+
66+
## Utilizing imports
67+
68+
As you may have noticed, each of these examples repeated the initial models. You can
69+
further apply the "one model" principle by defining models once in their own file and
70+
importing them. See the [import model-view pattern](model-view) for more.

docs/tour/text.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pagination_next: tour/icons
44
---
55
import CodeBlock from '@theme/CodeBlock';
66
import Markdown from '@site/static/d2/markdown.d2';
7+
import MarkdownLabel from '@site/static/d2/markdown-label.d2';
78
import Text2 from '@site/static/d2/text-2.d2';
89
import Code2 from '@site/static/d2/code-2.d2';
910
import NonMarkdownText from '@site/static/d2/non-markdown-text.d2';
@@ -19,6 +20,16 @@ import Latex from '@site/static/d2/latex.d2';
1920

2021
<div style={{width: 300, margin: "0 auto"}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/markdown.svg2')}}></div>
2122

23+
## Markdown label
24+
25+
If you want to set a Markdown label on a shape, you must explicitly declare the shape.
26+
27+
<CodeBlock className="language-d2">
28+
{MarkdownLabel}
29+
</CodeBlock>
30+
31+
<div style={{width: 300, margin: "0 auto"}} className="embedSVG" dangerouslySetInnerHTML={{__html: require('@site/static/img/generated/markdown-label.svg2')}}></div>
32+
2233
## Most languages are supported
2334

2435
D2 most likely supports any language you want to use, including non-Latin ones like
@@ -57,11 +68,6 @@ does not have linebreaks. You can kind of get around this with the `displaylines
5768
See below.
5869
:::
5970

60-
:::note
61-
Currently cannot be applied to labels, which is why the above example nests an object.
62-
This is coming soon.
63-
:::
64-
6571
## Code
6672

6773
Change `md` to a programming language for code blocks

sidebars.js

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ const sidebars = {
7676
"tour/globs",
7777
"tour/comments",
7878
"tour/overrides",
79+
"tour/models",
80+
"tour/legend",
7981
"tour/auto-formatter",
8082
],
8183
},

static/d2/globs-filter-endpoints.d2

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
a: {
2+
shape: circle
3+
style: {
4+
fill: blue
5+
opacity: 0.8
6+
}
7+
}
8+
b: {
9+
shape: rectangle
10+
style: {
11+
fill: red
12+
opacity: 0.5
13+
}
14+
}
15+
c: {
16+
shape: diamond
17+
style.fill: green
18+
style.opacity: 0.8
19+
}
20+
(* -> *)[*]: {
21+
&src.style.fill: blue
22+
style.stroke-dash: 3
23+
}
24+
(* -> *)[*]: {
25+
&dst.style.opacity: 0.8
26+
style.stroke: cyan
27+
}
28+
(* -> *)[*]: {
29+
&src.shape: rectangle
30+
&dst.style.fill: green
31+
style.stroke-width: 5
32+
}
33+
a -> b
34+
b -> c
35+
a -> c

static/d2/icons-1.d2

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
my network: {
2-
icon: https://icons.terrastruct.com/infra/019-network.svg
1+
deploy: {
2+
icon: https://icons.terrastruct.com/aws%2FDeveloper%20Tools%2FAWS-CodeDeploy.svg
3+
}
4+
5+
backup: {
6+
icon: https://icons.terrastruct.com/aws%2FStorage%2FAWS-Backup.svg
7+
}
8+
9+
deploy -> backup: {
10+
icon: https://icons.terrastruct.com/infra%2F002-backup.svg
311
}

static/d2/legend-hidden.d2

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
vars: {
2+
d2-legend: {
3+
a <-> b: Good relationship {
4+
style.stroke: red
5+
style.stroke-dash: 2
6+
}
7+
a.style.opacity: 0
8+
b.style.opacity: 0
9+
}
10+
}
11+
12+
api-1 <-> api-2: {
13+
style.stroke: red
14+
style.stroke-dash: 2
15+
}
16+
api-1 -> api-3: {
17+
target-arrowhead.shape: circle
18+
}

0 commit comments

Comments
 (0)