krata/_includes/mermaid.html

58 lines
1.6 KiB
HTML
Raw Normal View History

2020-12-09 19:42:46 +01:00
<!--
mermaid-js loader
-->
2021-01-23 08:07:18 +01:00
<script src="{{ site.data.assets[origin].mermaid.js | relative_url }}"></script>
2021-12-12 19:37:10 +01:00
2020-12-09 19:42:46 +01:00
<script>
2020-12-10 12:49:43 +01:00
$(function() {
2021-12-12 19:37:10 +01:00
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");
}
}
2020-12-10 12:49:43 +01:00
let initTheme = "default";
2020-12-09 19:42:46 +01:00
if ($("html[data-mode=dark]").length > 0
|| ($("html[data-mode]").length == 0
2020-12-10 12:49:43 +01:00
&& window.matchMedia("(prefers-color-scheme: dark)").matches ) ) {
initTheme = "dark";
}
2020-12-09 19:42:46 +01:00
2020-12-10 12:49:43 +01:00
let mermaidConf = {
theme: initTheme /* <default|dark|forest|neutral> */
};
2020-12-09 19:42:46 +01:00
2020-12-10 12:49:43 +01:00
/* Markdown converts to HTML */
$("pre").has("code.language-mermaid").each(function() {
let svgCode = $(this).children().html();
$(this).addClass("unloaded");
$(this).after(`<div class=\"mermaid\">${svgCode}</div>`);
});
2020-12-09 19:42:46 +01:00
2020-12-10 12:49:43 +01:00
mermaid.initialize(mermaidConf);
2021-12-12 19:37:10 +01:00
window.addEventListener("message", updateMermaid);
2020-12-10 12:49:43 +01:00
});
2020-12-09 19:42:46 +01:00
</script>