Skip to content

Commit bf5fd4d

Browse files
author
fingerballet
committed
fix mermaid
1 parent 252eae8 commit bf5fd4d

File tree

3 files changed

+2659
-0
lines changed

3 files changed

+2659
-0
lines changed

book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ additional-css = ["theme/pdf-report.css"] # 额外的 CSS 文件
1212
default-theme = "light" # 默认主题
1313
preferred-dark-theme = "navy" # 默认深色主题
1414
mathjax-support = true # 支持数学公式
15+
additional-js = ["mermaid.min.js", "mermaid-init.js"]
1516
# 启用搜索功能
1617
[output.html.search]
1718
enable = true

mermaid-init.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
const getThemeButton = (themeName) =>
25+
document.getElementById(themeName) || document.getElementById(`mdbook-theme-${themeName}`);
26+
27+
const bindClick = (themeName, onClick) => {
28+
const el = getThemeButton(themeName);
29+
if (el) {
30+
el.addEventListener('click', onClick);
31+
}
32+
};
33+
34+
for (const darkTheme of darkThemes) {
35+
bindClick(darkTheme, () => {
36+
if (lastThemeWasLight) {
37+
window.location.reload();
38+
}
39+
});
40+
}
41+
42+
for (const lightTheme of lightThemes) {
43+
bindClick(lightTheme, () => {
44+
if (!lastThemeWasLight) {
45+
window.location.reload();
46+
}
47+
});
48+
}
49+
})();

0 commit comments

Comments
 (0)