Skip to content

Commit 985ff82

Browse files
authored
Add D2 support (#59)
1 parent 1365480 commit 985ff82

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ jobs:
173173
- name: Render
174174
run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-cetz
175175

176+
D2:
177+
runs-on: ubuntu-latest
178+
strategy:
179+
fail-fast: true
180+
container:
181+
image: ghcr.io/quarto-dev/quarto:latest
182+
183+
steps:
184+
- name: Checkout
185+
uses: actions/checkout@v4
186+
187+
- name: Install dependencies
188+
run: |
189+
apt-get -q --no-allow-insecure-repositories update && \
190+
apt-get install --no-install-recommends --assume-yes \
191+
ca-certificates curl make && \
192+
curl -fsSL https://d2lang.com/install.sh | sh -s --
193+
194+
- name: Render
195+
run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-d2
196+
176197
Quarto:
177198
runs-on: ubuntu-latest
178199
strategy:

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Diagram Generator
44
This Lua filter is used to create figures from code blocks: images
55
are generated from the code with the help of external programs.
66
The filter processes diagram code for Asymptote, Graphviz,
7-
Mermaid, PlantUML, and Ti*k*Z.
7+
Mermaid, PlantUML, Ti*k*Z, and D2.
88

99

1010
Usage
@@ -103,6 +103,7 @@ that can be used to specify a specific executable.
103103
| [PlantUML] | `plantuml` | `plantuml` | `PLANTUML_BIN` |
104104
| [Ti*k*Z] | `tikz` | `pdflatex` | `PDFLATEX_BIN` |
105105
| [cetz] | `cetz` | `typst` | `TYPST_BIN` |
106+
| [D2] | `d2` | `d2` | `D2_BIN` |
106107

107108
### Other diagram engines
108109

@@ -115,6 +116,7 @@ The filter can be extended with local packages; see
115116
[PlantUML]: https://plantuml.com/
116117
[Ti*k*Z]: https://github.com/pgf-tikz/pgf
117118
[Cetz]: https://github.com/cetz-package/cetz
119+
[D2]: https://d2lang.com/
118120

119121
Figure options
120122
--------------

_extensions/diagram/diagram.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,43 @@ local cetz = {
291291
end,
292292
}
293293

294+
--- D2 engine for the D2 language
295+
local d2 = {
296+
line_comment_start = '#',
297+
mime_types = mime_types_set{'png', 'svg'},
298+
299+
compile = function (self, code, user_opts)
300+
return with_temporary_directory('diagram', function (tmpdir)
301+
return with_working_directory(tmpdir, function ()
302+
-- D2 format identifiers correspond to common file extensions.
303+
local mime_type = self.mime_type or 'image/svg+xml'
304+
local file_extension = extension_for_mimetype[mime_type]
305+
local infile = 'diagram.d2'
306+
local outfile = 'diagram.' .. file_extension
307+
308+
args = {'--bundle', '--pad=0', '--scale=1'}
309+
310+
table.insert(args, infile)
311+
table.insert(args, outfile)
312+
313+
write_file(infile, code)
314+
315+
pipe(self.execpath or 'd2', args, '')
316+
317+
return read_file(outfile), mime_type
318+
end)
319+
end)
320+
end,
321+
}
322+
294323
local default_engines = {
295324
asymptote = asymptote,
296325
dot = graphviz,
297326
mermaid = mermaid,
298327
plantuml = plantuml,
299328
tikz = tikz,
300329
cetz = cetz,
330+
d2 = d2,
301331
}
302332

303333
--

test/expected-d2.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h3 id="d2">D2</h3>
2+
<p>D2 is a Go-based diagramming and charting tool.</p>
3+
<figure>
4+
<img src="hello-world.svg" alt="Hello, World!" />
5+
<figcaption aria-hidden="true"><p>Hello, World!</p></figcaption>
6+
</figure>

test/input-d2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### D2
2+
3+
D2 is a Go-based diagramming and charting tool.
4+
5+
```{.d2 caption="Hello, World!" filename="hello-world.svg"}
6+
x -> y: hello world
7+
```

test/test-d2.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
input-files: ['test/input-d2.md']
2+
filters:
3+
- diagram.lua
4+
to: html
5+
metadata:
6+
pagetitle: D2 diagram
7+
diagram:
8+
cache: false
9+
variables:
10+
document-css: false

0 commit comments

Comments
 (0)