mermaid: initialize in build script

This commit is contained in:
2026-01-19 13:33:38 +08:00
committed by morgan
parent 39e2f3f3a2
commit 9a61aef414
4 changed files with 6 additions and 2651 deletions

View File

@@ -9,8 +9,3 @@ title = "Sinara Assembly"
[preprocessor.mermaid]
command = "mdbook-mermaid"
[output]
[output.html]
additional-js = ["mermaid.min.js", "mermaid-init.js"]

View File

@@ -13,8 +13,12 @@
packages.${system}.default = pkgs.stdenv.mkDerivation {
name = "book";
src = self;
buildInputs = [ pkgs.mdbook ];
buildPhase = "mdbook build";
buildInputs = [ pkgs.mdbook pkgs.mdbook-mermaid ];
buildPhase = "
cp -r ${pkgs.mdbook-mermaid.src}/src/bin/assets .
export MDBOOK_OUTPUT__HTML__ADDITIONAL_JS='[\"assets/mermaid.min.js\", \"assets/mermaid-init.js\"]'
mdbook build
";
installPhase = "mkdir -p $out/book; cp -R book $out/";
};
devShell.${system} = pkgs.mkShell {

View File

@@ -1,35 +0,0 @@
(() => {
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();
}
});
}
})();

2609
mermaid.min.js vendored

File diff suppressed because one or more lines are too long