Skip to content

Commit 037d18c

Browse files
d-desiatkindelan
authored andcommitted
Update src/hacking/contributing-to-servo-book.md
mermaid-init.js is a custom version written for Servo. My previous auto update change must be reverted Co-authored-by: delan azabani <delan@azabani.com> Signed-off-by: Dmitrii Desiatkin <47791504+d-desiatkin@users.noreply.github.com>
1 parent d9378df commit 037d18c

2 files changed

Lines changed: 27 additions & 31 deletions

File tree

src/hacking/contributing-to-servo-book.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Servo book uses [Mermaid Diagramming and charting tool](https://mermaid.js.org/)
3232
You can find the example of such diagram in section [Architecture overview](architecture/overview.md). Sometimes you will want to use the newest syntax, and that tool is not automatically updated, so you may use the following steps to update the version within the project:
3333
```bash
3434
mdbook-mermaid install .
35-
mv mermaid-init.js src/mermaid-init.js
35+
rm mermaid-init.js
3636
mv mermaid.min.js src/mermaid.min.js
3737
```
3838

src/mermaid-init.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1+
// NOTE: this is a custom version of mermaid-init.js written for the Servo book!
2+
// When updating mermaid, do not overwrite this file with the mdbook-mermaid version.
13
(() => {
2-
const darkThemes = ['ayu', 'navy', 'coal'];
3-
const lightThemes = ['light', 'rust'];
4+
// When the mdBook theme changes, rerender diagrams with a suitable Mermaid theme.
5+
const mermaids = [...document.querySelectorAll(".mermaid")];
6+
const texts = mermaids.map(x => x.textContent);
7+
const observer = new MutationObserver((mutations, observer) => {
8+
reinitMermaid();
9+
});
10+
observer.observe(document.documentElement, {
11+
attributeFilter: ["class"],
12+
});
13+
reinitMermaid();
414

5-
const classList = document.getElementsByTagName('html')[0].classList;
6-
7-
let lastThemeWasLight = true;
8-
for (const cssClass of classList) {
9-
if (darkThemes.includes(cssClass)) {
10-
lastThemeWasLight = false;
11-
break;
15+
function reinitMermaid() {
16+
// Restore the original state of each diagram.
17+
for (const [i, pre] of mermaids.entries()) {
18+
delete pre.dataset.processed;
19+
pre.textContent = texts[i];
1220
}
13-
}
14-
15-
const theme = lastThemeWasLight ? 'default' : 'dark';
16-
mermaid.initialize({ startOnLoad: true, theme });
17-
18-
// Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page
19-
20-
for (const darkTheme of darkThemes) {
21-
document.getElementById(darkTheme).addEventListener('click', () => {
22-
if (lastThemeWasLight) {
23-
window.location.reload();
24-
}
25-
});
26-
}
27-
28-
for (const lightTheme of lightThemes) {
29-
document.getElementById(lightTheme).addEventListener('click', () => {
30-
if (!lastThemeWasLight) {
31-
window.location.reload();
32-
}
21+
// Select a theme based on the mdBook theme, then rerender the diagrams.
22+
const rootClasses = document.documentElement.classList;
23+
mermaid.initialize({
24+
startOnLoad: false,
25+
theme: rootClasses.contains("light") || rootClasses.contains("rust") ? "default"
26+
: rootClasses.contains("coal") || rootClasses.contains("navy") || rootClasses.contains("ayu") ? "dark"
27+
: "default",
3328
});
29+
mermaid.run();
3430
}
35-
})();
31+
})();

0 commit comments

Comments
 (0)