From 2cd97c28940944fa5bf3af6804a60d2a7a598b0e Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Sat, 23 Aug 2025 16:15:26 -0400 Subject: [PATCH 1/6] D2: Add D2 support --- README.md | 4 +++- _extensions/diagram/diagram.lua | 25 +++++++++++++++++++++++++ test/expected-d2.html | 3 +++ test/input-d2.md | 7 +++++++ test/test-d2.yaml | 10 ++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/expected-d2.html create mode 100644 test/input-d2.md create mode 100644 test/test-d2.yaml diff --git a/README.md b/README.md index 3b18348..e7316de 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Diagram Generator This Lua filter is used to create figures from code blocks: images are generated from the code with the help of external programs. The filter processes diagram code for Asymptote, Graphviz, -Mermaid, PlantUML, and Ti*k*Z. +Mermaid, PlantUML, Ti*k*Z, and D2. Usage @@ -103,6 +103,7 @@ that can be used to specify a specific executable. | [PlantUML] | `plantuml` | `plantuml` | `PLANTUML_BIN` | | [Ti*k*Z] | `tikz` | `pdflatex` | `PDFLATEX_BIN` | | [cetz] | `cetz` | `typst` | `TYPST_BIN` | +| [D2] | `d2` | `d2` | `D2_BIN` | ### Other diagram engines @@ -115,6 +116,7 @@ The filter can be extended with local packages; see [PlantUML]: https://plantuml.com/ [Ti*k*Z]: https://github.com/pgf-tikz/pgf [Cetz]: https://github.com/cetz-package/cetz +[D2]: https://d2lang.com/ Figure options -------------- diff --git a/_extensions/diagram/diagram.lua b/_extensions/diagram/diagram.lua index 37f4a62..051a2c1 100644 --- a/_extensions/diagram/diagram.lua +++ b/_extensions/diagram/diagram.lua @@ -291,6 +291,30 @@ local cetz = { end, } +--- D2 engine for the D2 language +local d2 = { + line_comment_start = '#', + mime_types = mime_types_set{'png', 'svg'}, + + compile = function (self, code, user_opts) + return with_temporary_directory('diagram', function (tmpdir) + return with_working_directory(tmpdir, function () + -- D2 format identifiers correspond to common file extensions. + local mime_type = self.mime_type or 'image/svg+xml' + local file_extension = extension_for_mimetype[mime_type] + local infile = 'diagram.d2' + local outfile = 'diagram.' .. file_extension + + write_file(infile, code) + + pipe(self.execpath or 'd2', {infile, outfile}, '') + + return read_file(outfile), mime_type + end) + end) + end, +} + local default_engines = { asymptote = asymptote, dot = graphviz, @@ -298,6 +322,7 @@ local default_engines = { plantuml = plantuml, tikz = tikz, cetz = cetz, + d2 = d2, } -- diff --git a/test/expected-d2.html b/test/expected-d2.html new file mode 100644 index 0000000..ebfe7a9 --- /dev/null +++ b/test/expected-d2.html @@ -0,0 +1,3 @@ +

D2

+

D2 is a Go-based diagramming and charting tool.

+ diff --git a/test/input-d2.md b/test/input-d2.md new file mode 100644 index 0000000..448fc6a --- /dev/null +++ b/test/input-d2.md @@ -0,0 +1,7 @@ +### D2 + +D2 is a Go-based diagramming and charting tool. + +``` d2 +x -> y: hello world +``` diff --git a/test/test-d2.yaml b/test/test-d2.yaml new file mode 100644 index 0000000..256b656 --- /dev/null +++ b/test/test-d2.yaml @@ -0,0 +1,10 @@ +input-files: ['test/input-d2.md'] +filters: + - diagram.lua +to: html +metadata: + pagetitle: D2 diagram + diagram: + cache: false +variables: + document-css: false From e52da6328efa8a6c4617a3884db6255fb79273de Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Sat, 23 Aug 2025 19:55:21 -0400 Subject: [PATCH 2/6] Add caption and filename to D2 test --- test/expected-d2.html | 5 ++++- test/input-d2.md | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/expected-d2.html b/test/expected-d2.html index ebfe7a9..0a202d8 100644 --- a/test/expected-d2.html +++ b/test/expected-d2.html @@ -1,3 +1,6 @@

D2

D2 is a Go-based diagramming and charting tool.

- +
+Hello, World! + +
diff --git a/test/input-d2.md b/test/input-d2.md index 448fc6a..7b7455c 100644 --- a/test/input-d2.md +++ b/test/input-d2.md @@ -2,6 +2,6 @@ D2 is a Go-based diagramming and charting tool. -``` d2 +```{.d2 caption="Hello, World!" filename="hello-world.svg"} x -> y: hello world ``` From e0ef2f2cb77dc4ef0dd1d925b39bd62329dc139f Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Sun, 24 Aug 2025 20:49:31 -0400 Subject: [PATCH 3/6] D2: Specify --bundle/--pad=0/--scale=1 in engine --- _extensions/diagram/diagram.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/_extensions/diagram/diagram.lua b/_extensions/diagram/diagram.lua index 051a2c1..3981be5 100644 --- a/_extensions/diagram/diagram.lua +++ b/_extensions/diagram/diagram.lua @@ -305,9 +305,18 @@ local d2 = { local infile = 'diagram.d2' local outfile = 'diagram.' .. file_extension + args = {} + + table.insert(args, '--bundle') + table.insert(args, '--pad=0') + table.insert(args, '--scale=1') + + table.insert(args, infile) + table.insert(args, outfile) + write_file(infile, code) - pipe(self.execpath or 'd2', {infile, outfile}, '') + pipe(self.execpath or 'd2', args, '') return read_file(outfile), mime_type end) From c20a24e6d91fa7cea868c5c3e55f7b7dc7a03f04 Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Thu, 28 Aug 2025 21:01:55 -0400 Subject: [PATCH 4/6] D2: Simplify how args are constructed --- _extensions/diagram/diagram.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/_extensions/diagram/diagram.lua b/_extensions/diagram/diagram.lua index 3981be5..0223bcb 100644 --- a/_extensions/diagram/diagram.lua +++ b/_extensions/diagram/diagram.lua @@ -305,11 +305,7 @@ local d2 = { local infile = 'diagram.d2' local outfile = 'diagram.' .. file_extension - args = {} - - table.insert(args, '--bundle') - table.insert(args, '--pad=0') - table.insert(args, '--scale=1') + args = {'--bundle', '--pad=0', '--scale=1'} table.insert(args, infile) table.insert(args, outfile) From bbb318e2a6c3cf2ebd01c1552e264eb83ddf784a Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Sat, 30 Aug 2025 15:58:10 -0400 Subject: [PATCH 5/6] CI: Run D2 test --- .github/workflows/ci.yaml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac493a7..03645ae 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -173,6 +173,30 @@ jobs: - name: Render run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-cetz + D2: + runs-on: ubuntu-latest + strategy: + fail-fast: true + container: + image: ghcr.io/quarto-dev/quarto:latest + + env: + D2_BIN: $HOME/go/bin/d2 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + apt-get -q --no-allow-insecure-repositories update && \ + apt-get install --no-install-recommends --assume-yes \ + ca-certificates make go \ + go install oss.terrastruct.com/d2@latest + + - name: Render + run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-d2 + Quarto: runs-on: ubuntu-latest strategy: From 1c54369ff320e0d1cdbb40af8e27619dd017ba74 Mon Sep 17 00:00:00 2001 From: Chad Paradis Date: Sat, 30 Aug 2025 16:25:11 -0400 Subject: [PATCH 6/6] CI: Fix D2 test Fix D2 test by installing `d2` with https://d2lang.com/tour/install/#install-script instead of https://d2lang.com/tour/install/#install-from-source. --- .github/workflows/ci.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 03645ae..474916c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -180,9 +180,6 @@ jobs: container: image: ghcr.io/quarto-dev/quarto:latest - env: - D2_BIN: $HOME/go/bin/d2 - steps: - name: Checkout uses: actions/checkout@v4 @@ -191,8 +188,8 @@ jobs: run: | apt-get -q --no-allow-insecure-repositories update && \ apt-get install --no-install-recommends --assume-yes \ - ca-certificates make go \ - go install oss.terrastruct.com/d2@latest + ca-certificates curl make && \ + curl -fsSL https://d2lang.com/install.sh | sh -s -- - name: Render run: make PANDOC=/opt/quarto/bin/tools/x86_64/pandoc test-d2