1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-18 05:41:31 +00:00

Decouple theme-mode toggle & mermaid

This commit is contained in:
Cotes Chung
2021-12-13 02:37:10 +08:00
parent 09e8eb4274
commit c02c9c6a04
2 changed files with 38 additions and 17 deletions

View File

@@ -3,8 +3,34 @@
-->
<script src="https://cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js"></script>
<script>
$(function() {
function updateMermaid(event) {
if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {
const mode = event.data.message;
if (typeof mermaid === "undefined") {
return;
}
let expectedTheme = (mode === ModeToggle.DARK_MODE? "dark" : "default");
let config = { theme: expectedTheme };
/* Re-render the SVG <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344> */
$(".mermaid").each(function() {
let svgCode = $(this).prev().children().html();
$(this).removeAttr("data-processed");
$(this).html(svgCode);
});
mermaid.initialize(config);
mermaid.init(undefined, ".mermaid");
}
}
let initTheme = "default";
if ($("html[mode=dark]").length > 0
@@ -25,5 +51,7 @@
});
mermaid.initialize(mermaidConf);
window.addEventListener("message", updateMermaid);
});
</script>