Skip to content

Commit 4efe474

Browse files
committed
content: Add diagrams.D2 documentation
1 parent 2ec625a commit 4efe474

File tree

6 files changed

+256
-0
lines changed

6 files changed

+256
-0
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"Pastorius",
145145
"Samsa",
146146
"Stucki",
147+
"Terrastruct",
147148
"Thénardier",
148149
"Vitter",
149150
"WASI",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
_comment: Do not remove front matter.
3+
---
4+
5+
center
6+
: (`bool`) Whether to center the diagram within the viewport, applicable only when the viewport's aspect ratio is different than that of the SVG `viewBox` attribute. When `true`, sets the `preserveAspectRatio` attribute to `xMidYMid meet`. When `false`, sets the `preserveAspectRatio` attribute to `xMinYMin meet`. Default is `false`.
7+
8+
darkTheme
9+
: (`string`) The D2 theme to use if the system is in dark mode. This value is case-insensitive. See [this list] of available themes. Default is `Dark Flagship Terrastruct`.
10+
11+
layoutEngine
12+
: (`string`) The D2 layout engine to use when automatically arranging diagram elements, either `dagre` or `elk`. This value is case-insensitive. Default is `dagre`. See the [D2 documentation] for details.
13+
14+
lightTheme
15+
: (`string`) The D2 theme to use if the system is in light mode or has no preference. This value is case-insensitive. See [this list] of available themes. Default is `Neutral Default`.
16+
17+
minify
18+
: (`bool`) Whether to minify the SVG markup. Default is `true`.
19+
20+
padding
21+
: (`int`) The number of pixels with which to pad each side of the diagram. This value must be within the bounds of 0 and 1000, inclusive. Default is `0`.
22+
23+
salt
24+
: (`string`) A salt value used to generate a unique ID, preventing conflicts when embedding multiple identical diagrams in the same HTML document.
25+
26+
scale
27+
: (`float`) How much to reduce or enlarge the diagram. Values less than 1 reduce the diagram, while values greater than 1 enlarge the diagram. This value must be greater than 0 and less than or equal to 100. Default is `1`.
28+
29+
sketch
30+
: (`bool`) Whether to render the diagram as if sketched by hand. Default is `false`.
31+
32+
[this list]: https://d2lang.com/tour/themes/
33+
[D2 documentation]: https://d2lang.com/tour/layouts

content/en/configuration/markup.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ This is the default configuration.
314314

315315
{{% include "/_common/syntax-highlighting-options.md" %}}
316316

317+
## Diagrams
318+
319+
{{< new-in 0.153.0 />}}
320+
321+
This is the default configuration for rendering [D2 diagrams] with the [`diagrams.D2`] template function:
322+
323+
{{< code-toggle config=markup.diagrams />}}
324+
325+
{{% include "/_common/diagram-d2-options.md" %}}
326+
317327
## Table of contents
318328

319329
This is the default configuration for the table of contents, applicable to Goldmark and Asciidoctor:
@@ -329,13 +339,15 @@ endLevel
329339
ordered
330340
: (`bool`) Whether to generates an ordered list instead of an unordered list. Default is `false`.
331341

342+
[`diagrams.D2`]: /functions/diagrams/d2/
332343
[`Fragments.Identifiers`]: /methods/page/fragments/#identifiers
333344
[`TableOfContents`]: /methods/page/tableofcontents/
334345
[anchorize]: /functions/urls/anchorize
335346
[AsciiDoc]: https://asciidoc.org/
336347
[asciidoctor-diagram]: https://asciidoctor.org/docs/asciidoctor-diagram/
337348
[attributes]: https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#attributes-and-substitutions
338349
[CommonMark]: https://spec.commonmark.org/current/
350+
[D2 diagrams]: https://d2lang.com/
339351
[deleted text]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/del
340352
[duplication of shared page resources]: /configuration/markup/#duplicateresourcefiles
341353
[Emacs Org Mode]: https://orgmode.org/
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
title: diagrams.D2
3+
description: Returns an SVGDiagram object created from the given D2 markup and options.
4+
categories: []
5+
keywords: []
6+
params:
7+
functions_and_methods:
8+
aliases: []
9+
returnType: diagrams.SVGDiagram
10+
signatures: ['diagrams.D2 MARKUP [OPTIONS]']
11+
---
12+
13+
{{< new-in v0.153.0 />}}
14+
15+
Useful in a [code block render hook][], the `diagrams.D2` function returns an SVGDiagram object created from the given [D2][] markup and options.
16+
17+
```go-html-template
18+
{{ $markup := `x -> y` }}
19+
{{ $options := dict "scale" 1.5 "padding" 10 }}
20+
{{ $d := diagrams.D2 $markup $options }}
21+
{{ $d.Wrapped }}
22+
```
23+
24+
## Methods
25+
26+
Diagrams rendered by D2 consist of an `svg` element that is subsequently wrapped within another `svg` element. Conceptually, the inner `svg` element is the diagram, and the outer `svg` element is a container.
27+
28+
The SVGDiagram object has the following methods:
29+
30+
Wrapped
31+
: (`template.HTML`) Returns the inner `svg` element wrapped within an outer `svg` element, effectively reconstructing the original SVG diagram created by D2.
32+
33+
Inner
34+
: (`template.HTML`) Returns the inner `svg` element.
35+
36+
Width
37+
: (`int`) Returns the `width` attribute of the outer `svg` element, which may differ from the inner `svg` element's width if scaled during rendering.
38+
39+
Height
40+
: (`int`) Returns the `height` attribute of the outer `svg` element, which may differ from the inner `svg` element's height if scaled during rendering.
41+
42+
ViewBox
43+
: (`string`) Returns the `viewBox` attribute of the outer `svg` element. The `viewBox` coordinates are not affected by scaling.
44+
45+
PreserveAspectRatio
46+
: (`string`) Returns the `preserveAspectRatio` attribute of the outer `svg` element.
47+
48+
## Options
49+
50+
The `diagrams.D2` function takes an options map with zero or more of the following options:
51+
52+
{{% include "/_common/diagram-d2-options.md" %}}
53+
54+
## Render hook
55+
56+
Hugo natively supports D2 diagrams with an [embedded code block render hook][], allowing you to use fenced code blocks for D2 markup within your Markdown.
57+
58+
The render hook accepts the options for the `diagrams.D2` function as described above, and those described below:
59+
60+
class
61+
: (`string`) One or more CSS classes to append to the hardcoded `class` attribute of the outer `div` element. The hardcoded `class` attribute of the outer `div` element is `d2-svg-container`.
62+
63+
file
64+
: (`string`) The path to a file containing D2 markup, typically with a `.d2` extension. If this value is non-empty, Hugo ignores any content between the opening and closing code fences. The render hook resolves the path by looking for a matching [page resource](g), falling back to a matching [global resource](g). It throws an error and fails the build if the file does not exist.
65+
66+
id
67+
: (`string`) The `id` attribute of the outer `div` element.
68+
69+
title
70+
: (`string`) The `title` attribute of the outer `div` element.
71+
72+
## Examples
73+
74+
To create a D2 diagram using the default options:
75+
76+
````text
77+
```d2svg
78+
direction: right
79+
x -> y
80+
```
81+
````
82+
83+
```d2svg
84+
direction: right
85+
x -> y
86+
```
87+
88+
To enlarge and render as a sketch, specify the options as comma-separated or space-separated key-value pairs wrapped within braces:
89+
90+
````text
91+
```d2svg {scale=1.5, sketch=true}
92+
direction: right
93+
x -> y
94+
```
95+
````
96+
97+
```d2svg {scale=1.5, sketch=true}
98+
direction: right
99+
x -> y
100+
```
101+
102+
To change the theme and add one or more CSS classes to the outer `div` element:
103+
104+
````text
105+
```d2svg {scale=1.5, sketch=true, lightTheme="Everglade Green" class="foo bar"}
106+
direction: right
107+
x -> y: hello {
108+
style.animated: true
109+
}
110+
```
111+
````
112+
113+
```d2svg {scale=1.5, sketch=true, lightTheme="Everglade Green" class="foo bar"}
114+
direction: right
115+
x -> y: hello {
116+
style.animated: true
117+
}
118+
```
119+
120+
## Caching
121+
122+
Hugo employs caching to optimize diagram rendering, storing results in memory and on disk. The default disk cache location is `:cacheDir/:project`, defined in your site configuration:
123+
124+
{{< code-toggle config=caches.misc />}}
125+
126+
Alternatively, you can configure Hugo to cache diagrams within the project's `resources` directory as shown below:
127+
128+
{{< code-toggle file=hugo >}}
129+
[caches.misc]
130+
dir = ':resourceDir'
131+
maxAge = -1
132+
{{< /code-toggle >}}
133+
134+
Learn more about [file cache configuration].
135+
136+
## Configuration
137+
138+
To achieve a consistent look and feel for all diagrams on your site, define default settings within your site's configuration file.
139+
140+
See [configure diagrams](/configuration/markup/#diagrams).
141+
142+
## Themes
143+
144+
D2 themes customize visual elements like colors, fill patterns, and border radii. Two [theme catalogs][] exist: one for light and one for dark modes. When your system is in dark mode, the configured dark theme is applied.
145+
146+
By default, Hugo uses:
147+
148+
- Light theme: Neutral Default
149+
- Dark theme: Dark Flagship Terrastruct
150+
151+
You can override these defaults by specifying a light and dark theme in your site configuration, within the options map passed to the `d2.Diagram` function, or in fenced D2 code blocks.
152+
153+
### Light themes
154+
155+
{{< list-d2-themes catalog=light >}}
156+
157+
### Dark themes
158+
159+
{{< list-d2-themes catalog=dark >}}
160+
161+
## Miscellaneous
162+
163+
### Responsive diagrams
164+
165+
Create a CSS ruleset to make your D2 diagrams responsive:
166+
167+
```css
168+
.d2-svg-container > svg {
169+
max-width: 100%;
170+
height: auto;
171+
}
172+
```
173+
174+
### Visual Studio Code
175+
176+
Install the [D2 extension][] to help you create D2 diagrams when using Visual Studio Code.
177+
178+
[code block render hook]: /render-hooks/code-blocks/
179+
[D2]: https://d2lang.com/
180+
[D2 extension]: https://marketplace.visualstudio.com/items?itemName=terrastruct.d2
181+
[embedded code block render hook]: <{{% eturl render-codeblock-d2 %}}>
182+
[file cache configuration]: /configuration/caches/
183+
[theme catalogs]: https://d2lang.com/tour/themes/

data/embedded_template_urls.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'twitter_cards' = '_partials/twitter_cards.html'
1414

1515
# Render hooks
16+
'render-codeblock-d2' = '_markup/render-codeblock-d2.html'
1617
'render-codeblock-goat' = '_markup/render-codeblock-goat.html'
1718
'render-image' = '_markup/render-image.html'
1819
'render-link' = '_markup/render-link.html'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{/* prettier-ignore-start */ -}}
2+
{{/*
3+
Renders an unordered list of D2 themes in the given catalog.
4+
5+
@param {string} catalog The D2 theme catalog, either light or dark.
6+
7+
@returns {template.HTML}
8+
9+
@example {{< list-d2-themes catagog=light
10+
>}}
11+
*/}}
12+
{{/* prettier-ignore-end */ -}}
13+
{{- $catalogs := slice "dark" "light" }}
14+
{{- with .Get "catalog" }}
15+
{{- if in $catalogs . }}
16+
<ul>
17+
{{- range index site.Data.docs.diagrams.d2.themes . }}
18+
<li>{{ . }}</li>
19+
{{- end }}
20+
</ul>
21+
{{- else }}
22+
{{- errorf "The 'catalog' argument passed to the %q shortcode must be one . See %s" $.Name (delimit $catalogs ", " " or ") $.Position }}
23+
{{- end }}
24+
{{- else }}
25+
{{- errorf "The %q shortcode requires a 'catalog' argument. See %s" .Name .Position }}
26+
{{- end }}

0 commit comments

Comments
 (0)