Skip to content

Commit 74160cc

Browse files
authored
docs: add mdbook-mermaid plugin (#1382)
1 parent a8d1217 commit 74160cc

File tree

7 files changed

+2256
-17
lines changed

7 files changed

+2256
-17
lines changed

.github/workflows/mdbook.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,8 @@ jobs:
1616
- uses: actions/checkout@v4
1717
with:
1818
fetch-depth: 0
19-
- name: Install latest mdbook
20-
run: |
21-
tag=$(curl 'https://api.github.com/repos/rust-lang/mdbook/releases/latest' | jq -r '.tag_name')
22-
url="https://github.com/rust-lang/mdbook/releases/download/${tag}/mdbook-${tag}-x86_64-unknown-linux-gnu.tar.gz"
23-
mkdir mdbook
24-
curl -sSL $url | tar -xz --directory=./mdbook
25-
echo `pwd`/mdbook >> $GITHUB_PATH
2619
- name: Build Book
27-
run: mdbook build
20+
run: make build # also installs deps
2821
working-directory: docs/spec
2922
- name: Setup Pages
3023
uses: actions/configure-pages@v4

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ protoc: clean
3232
./api/builder/protoc-docker.sh
3333
./api/builder/generate-docs.sh
3434

35-
# Serves the mdbook docs located in ./docs/spec.
36-
# Will open a browser window to view the docs.
37-
mdbook-serve:
38-
mdbook serve ./docs/spec --open
39-
4035
# Builds the protobuf files locally (i.e. without docker).
4136
protoc-local: clean
4237
./api/builder/protoc.sh
@@ -95,3 +90,7 @@ docker-release-build:
9590

9691
semver:
9792
echo "${SEMVER}"
93+
94+
##### Proxies to other local Makefiles #####
95+
mdbook-serve:
96+
$(MAKE) -C docs/spec serve

docs/spec/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Serves the mdbook docs located in this directory.
2+
# Will open a browser window to view the docs.
3+
serve: install-deps
4+
mdbook serve . --open
5+
6+
build: install-deps
7+
mdbook build
8+
9+
install-deps:
10+
cargo install mdbook [email protected]

docs/spec/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ The protobuf documentation for all of our services is automatically generated by
1212
1313
## Preview
1414

15-
To preview the book locally, run (from the rootdir):
15+
To preview the book locally, run:
1616

1717
```bash
18-
make mdbook-serve
18+
make serve
1919
```
2020

2121
which will start a local server at `http://localhost:3000` and open your browser to preview the result.
2222

2323
## Github Pages
2424

2525
The book is automatically built and deployed to Github Pages on every push to the `main` branch.
26-
This is done by the Github Actions workflow defined in [../../.github/workflows/mdbook.yaml](../../.github/workflows/mdbook.yaml)
26+
This is done by the Github Actions workflow defined in [../../.github/workflows/mdbook.yaml](../../.github/workflows/mdbook.yaml)
27+
28+
## Mermaid Diagrams
29+
30+
We use mdbook-mermaid to render mermaid diagrams in the book. It is installed along with mdbook when running `make install-deps`. The 2 js files `mermaid-init.js` and `mermaid.min.js` were installed from `mdbook-mermaid install .` which was ran with mdbook-mermaid v0.14.1. These two files are copied into the built book and needed to render the images. Haven't found a way to only generate the images and then update the markdown files to reference the images, so we are stuck with this dependency for now.

docs/spec/book.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ src = "src"
66
title = "EigenDA Spec"
77

88
[output.html]
9-
mathjax-support = true
9+
mathjax-support = true
10+
additional-js = ["mermaid.min.js", "mermaid-init.js"]
11+
12+
[preprocessor]
13+
14+
[preprocessor.mermaid]
15+
# Preprocesses the mermaid diagrams (see src/integration/proxy.md for an example)
16+
# and generates the corresponding SVG files.
17+
# See https://github.com/badboy/mdbook-mermaid for more information.
18+
# This requires the mdbook-mermaid crate to be installed.
19+
command = "mdbook-mermaid"

docs/spec/mermaid-init.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// You can modify this file to customize mdbook-mermaid.
2+
// See https://github.com/badboy/mdbook-mermaid?tab=readme-ov-file#configure-your-mdbook-to-use-mdbook-mermaid
3+
(() => {
4+
const darkThemes = ['ayu', 'navy', 'coal'];
5+
const lightThemes = ['light', 'rust'];
6+
7+
const classList = document.getElementsByTagName('html')[0].classList;
8+
9+
let lastThemeWasLight = true;
10+
for (const cssClass of classList) {
11+
if (darkThemes.includes(cssClass)) {
12+
lastThemeWasLight = false;
13+
break;
14+
}
15+
}
16+
17+
const theme = lastThemeWasLight ? 'default' : 'dark';
18+
mermaid.initialize({ startOnLoad: true, theme });
19+
20+
// Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page
21+
22+
for (const darkTheme of darkThemes) {
23+
document.getElementById(darkTheme).addEventListener('click', () => {
24+
if (lastThemeWasLight) {
25+
window.location.reload();
26+
}
27+
});
28+
}
29+
30+
for (const lightTheme of lightThemes) {
31+
document.getElementById(lightTheme).addEventListener('click', () => {
32+
if (!lastThemeWasLight) {
33+
window.location.reload();
34+
}
35+
});
36+
}
37+
})();

docs/spec/mermaid.min.js

Lines changed: 2186 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)