Skip to content

Commit

Permalink
Add mermaid support for our mdbook (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning authored and magick93 committed Jan 13, 2025
1 parent d329077 commit 390d02d
Show file tree
Hide file tree
Showing 5 changed files with 2,232 additions and 3 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ jobs:
- uses: actions/checkout@v4

- name: Setup mdBook
uses: peaceiris/actions-mdbook@v1
uses: jontze/action-mdbook@v3
with:
mdbook-version: 'latest'
token: ${{secrets.GITHUB_TOKEN}}
use-mermaid: true
use-linkcheck: true

- run: mdbook build
working-directory: book
Expand Down
6 changes: 6 additions & 0 deletions book/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ author = "Sigma Prime"
[output.html]
additional-css =["src/css/custom.css"]
default-theme = "coal"
additional-js = ["mermaid.min.js", "mermaid-init.js"]

[preprocessor]

[preprocessor.mermaid]
command = "mdbook-mermaid"

35 changes: 35 additions & 0 deletions book/mermaid-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(() => {
const darkThemes = ['ayu', 'navy', 'coal'];
const lightThemes = ['light', 'rust'];

const classList = document.getElementsByTagName('html')[0].classList;

let lastThemeWasLight = true;
for (const cssClass of classList) {
if (darkThemes.includes(cssClass)) {
lastThemeWasLight = false;
break;
}
}

const theme = lastThemeWasLight ? 'default' : 'dark';
mermaid.initialize({ startOnLoad: true, theme });

// Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page

for (const darkTheme of darkThemes) {
document.getElementById(darkTheme).addEventListener('click', () => {
if (lastThemeWasLight) {
window.location.reload();
}
});
}

for (const lightTheme of lightThemes) {
document.getElementById(lightTheme).addEventListener('click', () => {
if (!lastThemeWasLight) {
window.location.reload();
}
});
}
})();
Loading

0 comments on commit 390d02d

Please sign in to comment.