D2 diagram generator plugin for MdBook
Find a file
dependabot[bot] fbf65b6e97
ci(deps): bump actions/checkout from 5 to 6 (#168)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-11-21 05:07:03 +00:00
.github ci(deps): bump actions/checkout from 5 to 6 (#168) 2025-11-21 05:07:03 +00:00
example feat: add --theme and --dark-theme to CLI args (#124) 2025-02-15 16:03:00 +00:00
src feat: update to mdbook 0.5 (#165) 2025-11-20 08:22:20 +00:00
tests feat: update to mdbook 0.5 (#165) 2025-11-20 08:22:20 +00:00
.gitignore initial commmit 2022-12-24 09:56:50 +00:00
.rustfmt.toml feat: update to mdbook 0.5 (#165) 2025-11-20 08:22:20 +00:00
Cargo.lock chore: release v0.3.7 (#166) 2025-11-20 08:27:53 +00:00
Cargo.toml chore: release v0.3.7 (#166) 2025-11-20 08:27:53 +00:00
CHANGELOG.md chore: release v0.3.7 (#166) 2025-11-20 08:27:53 +00:00
deny.toml chore(ci): add cargo-deny check (#130) 2025-02-09 17:40:24 +00:00
LICENSE initial commmit 2022-12-24 09:56:50 +00:00
README.md chore: add sponsorship info (#128) 2025-02-04 10:58:43 +00:00

D2 preprocessor for mdbook

codecov Continuous integration

A preprocessor for mdbook to convert d2 codeblocks into SVG images using D2.

Installation

Install with cargo:

cargo install mdbook-d2 --locked

Or to install from git:

cargo install --git https://github.com/danieleades/mdbook-d2 --locked

Requirements

This preprocessor assumes that D2 is installed locally and on the path. D2 installation instructions can be found here.

Check that the local installation is working correctly using

d2 --version

Usage

Configuration

Add this to your book.toml:

[preprocessor.d2]

# path to d2 binary.
# optional. default is "d2" (ie. on the path).
path = "d2"

# layout engine for diagrams. See https://github.com/terrastruct/d2#plugins.
# optional. default is "dagre".
layout = "dagre"

# whether to use inline svg when rendering.
# if 'false', separate files will be generated in src/<output-dir> and referenced.
# optional. default is 'true'
inline = true

# output directory relative to `src/` for generated diagrams.
# This is ignored if 'inline' is 'true'.
# optional. default is "d2".
output-dir = "d2"

Code Blocks

Use in your 'book' by annotating D2 code blocks-


## My Diagram

```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
```

The code block will be replaced with the D2 diagram in the rendered document.

Thanks

The code in this preprocessor is based on that from https://github.com/matthiasbeyer/mdbook-svgbob2


Was this useful? Buy me a coffee