Skip to content

Commit

Permalink
fix: mermaid occasionally fails to initialize (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Nov 4, 2022
1 parent 6774e0e commit 48f14e3
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions _includes/mermaid.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
<script src="{{ site.data.assets[origin].mermaid.js | relative_url }}"></script>

<script>
$(function() {
(function () {

function updateMermaid(event) {
if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {
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 };
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() {
$(".mermaid").each(function () {
let svgCode = $(this).prev().children().html();
$(this).removeAttr("data-processed");
$(this).html(svgCode);
Expand All @@ -35,23 +36,24 @@

if ($("html[data-mode=dark]").length > 0
|| ($("html[data-mode]").length == 0
&& window.matchMedia("(prefers-color-scheme: dark)").matches ) ) {
&& window.matchMedia("(prefers-color-scheme: dark)").matches)) {
initTheme = "dark";
}

let mermaidConf = {
theme: initTheme /* <default|dark|forest|neutral> */
};

/* Markdown converts to HTML */
$("pre").has("code.language-mermaid").each(function() {
/* Create mermaid tag */
$("pre").has("code.language-mermaid").each(function () {
let svgCode = $(this).children().html();
$(this).addClass("unloaded");
$(this).after(`<div class=\"mermaid\">${svgCode}</div>`);
$(this).after(`<pre class=\"mermaid\">${svgCode}</pre>`);
});

mermaid.initialize(mermaidConf);

window.addEventListener("message", updateMermaid);
});
})();

</script>

0 comments on commit 48f14e3

Please sign in to comment.