forked from mjbvz/vscode-markdown-mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
39 lines (31 loc) · 1.1 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//@ts-check
import mermaid from 'mermaid';
function init() {
mermaid.initialize({
startOnLoad: false,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
// Delete existing mermaid outputs
for (const possibleMermaidErrorOut of document.getElementsByTagName('svg')) {
const parent = possibleMermaidErrorOut.parentElement;
if (parent && parent.id.startsWith('dmermaid')) {
parent.remove();
}
}
let i = 0;
for (const mermaidContainer of document.getElementsByClassName('mermaid')) {
const id = `mermaid-${Date.now()}-${i++}`;
const source = mermaidContainer.textContent;
const out = document.createElement('div');
out.id = id;
mermaidContainer.innerHTML = '';
mermaidContainer.appendChild(out);
mermaid.render(id, source, (out) => {
mermaidContainer.innerHTML = out;
});
}
}
window.addEventListener('vscode.markdown.updateContent', init);
init();