Skip to content

Commit 35b2e95

Browse files
authored
Update mermaid-init.js
1 parent c3567e1 commit 35b2e95

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

assets/js/mermaid-init.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
1-
document.addEventListener("DOMContentLoaded", function() {
2-
if (window.mermaid) {
3-
mermaid.initialize({ startOnLoad: true });
1+
(function () {
2+
function transformAndRender() {
3+
if (!document.querySelectorAll) return;
4+
5+
// Find fenced code blocks (common output: <pre><code class="language-mermaid">...</code></pre>)
6+
var codes = document.querySelectorAll('pre > code.language-mermaid, code.language-mermaid');
7+
if (codes.length === 0) return;
8+
9+
codes.forEach(function (code) {
10+
var pre = code.parentNode;
11+
var container = document.createElement('div');
12+
container.className = 'mermaid';
13+
// keep textContent (not innerHTML) to avoid HTML escaping issues
14+
container.textContent = code.textContent.trim();
15+
// Replace the <pre> with the mermaid div
16+
pre.parentNode.replaceChild(container, pre);
17+
});
18+
19+
// Try to initialize / render mermaid across versions
20+
try {
21+
if (window.mermaid) {
22+
// v8/v9/v10 variants
23+
if (typeof window.mermaid.initialize === 'function') {
24+
// common initialize
25+
window.mermaid.initialize({ startOnLoad: false });
26+
}
27+
if (typeof window.mermaid.init === 'function') {
28+
// Some versions expose init
29+
window.mermaid.init(undefined, document.querySelectorAll('.mermaid'));
30+
} else if (typeof window.mermaid.run === 'function') {
31+
// older helper
32+
window.mermaid.run();
33+
} else if (window.mermaid.mermaidAPI && typeof window.mermaid.mermaidAPI.render === 'function') {
34+
// low-level render fallback
35+
document.querySelectorAll('.mermaid').forEach(function (el, i) {
36+
var id = 'mermaid-svg-' + i + '-' + Date.now();
37+
var code = el.textContent;
38+
window.mermaid.mermaidAPI.render(id, code, function (svg) {
39+
var wrapper = document.createElement('div');
40+
wrapper.innerHTML = svg;
41+
el.parentNode.replaceChild(wrapper, el);
42+
});
43+
});
44+
}
45+
}
46+
} catch (err) {
47+
console.error('Mermaid render error:', err);
48+
}
449
}
5-
});
50+
51+
// If mermaid loads after DOMContentLoaded, use a MutationObserver + timeout fallback
52+
function waitForMermaidThenRender() {
53+
if (window.mermaid) {
54+
transformAndRender();
55+
return;
56+
}
57+
var mo = new MutationObserver(function (mutations, obs) {
58+
if (window.mermaid) {
59+
obs.disconnect();
60+
transformAndRender();
61+
}
62+
});
63+
mo.observe(document.documentElement || document.body, { childList: true, subtree: true });
64+
// fallback after 2s
65+
setTimeout(transformAndRender, 2000);
66+
}
67+
68+
if (document.readyState === 'loading') {
69+
document.addEventListener('DOMContentLoaded', waitForMermaidThenRender);
70+
} else {
71+
waitForMermaidThenRender();
72+
}
73+
})();

0 commit comments

Comments
 (0)