diff options
author | Ole Mussmann <gitlab+account@ole.mn> | 2025-02-20 21:00:16 +0100 |
---|---|---|
committer | Ole Mussmann <gitlab+account@ole.mn> | 2025-02-21 08:57:59 +0100 |
commit | 2310f6160404914c7245c3178bc8f0f0532b2caf (patch) | |
tree | 05fd70b3c49e72e1e7f5fe09e1e5ef8f8a5fecc4 /static/js/mermaid.js | |
parent | a05a5b2704eec66e84c4fc3045127e354cf38e1a (diff) |
make mermaid themes configurable
Diffstat (limited to 'static/js/mermaid.js')
-rw-r--r-- | static/js/mermaid.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/static/js/mermaid.js b/static/js/mermaid.js new file mode 100644 index 0000000..c348dc6 --- /dev/null +++ b/static/js/mermaid.js @@ -0,0 +1,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); |