summaryrefslogtreecommitdiff
path: root/static/js/mermaid.js
blob: c348dc63e053a9f11070346d07cfceb92c3a54ea (plain)
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
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';

const this_js_script = document.getElementById('mermaid_script');
const light_theme = this_js_script.getAttribute('data-light-theme');
const dark_theme = this_js_script.getAttribute('data-dark-theme');

function runmermaid() {
  const theme = (document.body.classList.contains('dark') ? dark_theme : light_theme)
  mermaid.initialize({ startOnLoad: false, theme: theme});
  const items = document.querySelectorAll('.mermaid');
  let counter = 0;
  for (const item of items) {
    const id = counter++;
    if(item.originalCode === undefined) {
      item.originalCode = item.textContent.trim();
    }
    mermaid.render("mermaid"+id, item.originalCode).then((val) => {
      item.innerHTML = val.svg
    }, (err) => {
        console.log(err);
        // Workaround: move incorrectly placed error messages into their diagram
        item.innerHTML = "";
        item.appendChild(document.getElementById("mermaid" + id));
      });
  }
}
document.addEventListener('DOMContentLoaded', runmermaid);
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', runmermaid);