Skip to content

Commit 939b3a7

Browse files
committed
Fix Mermaid diagram rendering
1 parent e038aa1 commit 939b3a7

5 files changed

Lines changed: 2652 additions & 3 deletions

File tree

book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ build-dir = "book"
1111
no-section-label = true
1212
git-repository-url = "https://github.com/wagov-dtt/architecture-decision-records"
1313
edit-url-template = "https://github.com/wagov-dtt/architecture-decision-records/edit/main/{path}"
14+
additional-js = ["mermaid.min.js", "mermaid-init.js"]
1415

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

mermaid-init.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
(() => {
6+
const darkThemes = ['ayu', 'navy', 'coal'];
7+
const lightThemes = ['light', 'rust'];
8+
9+
const classList = document.getElementsByTagName('html')[0].classList;
10+
11+
let lastThemeWasLight = true;
12+
for (const cssClass of classList) {
13+
if (darkThemes.includes(cssClass)) {
14+
lastThemeWasLight = false;
15+
break;
16+
}
17+
}
18+
19+
const theme = lastThemeWasLight ? 'default' : 'dark';
20+
mermaid.initialize({ startOnLoad: true, theme });
21+
22+
// Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page
23+
24+
for (const darkTheme of darkThemes) {
25+
document.getElementById(darkTheme).addEventListener('click', () => {
26+
if (lastThemeWasLight) {
27+
window.location.reload();
28+
}
29+
});
30+
}
31+
32+
for (const lightTheme of lightThemes) {
33+
document.getElementById(lightTheme).addEventListener('click', () => {
34+
if (!lastThemeWasLight) {
35+
window.location.reload();
36+
}
37+
});
38+
}
39+
})();

0 commit comments

Comments
 (0)