Skip to content

Commit 48f14e3

Browse files
committed
fix: mermaid occasionally fails to initialize (#536)
1 parent 6774e0e commit 48f14e3

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

_includes/mermaid.html

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
<script src="{{ site.data.assets[origin].mermaid.js | relative_url }}"></script>
66

77
<script>
8-
$(function() {
8+
(function () {
9+
910
function updateMermaid(event) {
1011
if (event.source === window && event.data &&
11-
event.data.direction === ModeToggle.ID) {
12+
event.data.direction === ModeToggle.ID) {
1213

1314
const mode = event.data.message;
1415

1516
if (typeof mermaid === "undefined") {
1617
return;
1718
}
1819

19-
let expectedTheme = (mode === ModeToggle.DARK_MODE? "dark" : "default");
20-
let config = { theme: expectedTheme };
20+
let expectedTheme = (mode === ModeToggle.DARK_MODE ? "dark" : "default");
21+
let config = {theme: expectedTheme};
2122

2223
/* Re-render the SVG › <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344> */
23-
$(".mermaid").each(function() {
24+
$(".mermaid").each(function () {
2425
let svgCode = $(this).prev().children().html();
2526
$(this).removeAttr("data-processed");
2627
$(this).html(svgCode);
@@ -35,23 +36,24 @@
3536

3637
if ($("html[data-mode=dark]").length > 0
3738
|| ($("html[data-mode]").length == 0
38-
&& window.matchMedia("(prefers-color-scheme: dark)").matches ) ) {
39+
&& window.matchMedia("(prefers-color-scheme: dark)").matches)) {
3940
initTheme = "dark";
4041
}
4142

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

46-
/* Markdown converts to HTML */
47-
$("pre").has("code.language-mermaid").each(function() {
47+
/* Create mermaid tag */
48+
$("pre").has("code.language-mermaid").each(function () {
4849
let svgCode = $(this).children().html();
4950
$(this).addClass("unloaded");
50-
$(this).after(`<div class=\"mermaid\">${svgCode}</div>`);
51+
$(this).after(`<pre class=\"mermaid\">${svgCode}</pre>`);
5152
});
5253

5354
mermaid.initialize(mermaidConf);
5455

5556
window.addEventListener("message", updateMermaid);
56-
});
57+
})();
58+
5759
</script>

0 commit comments

Comments
 (0)