web/_includes/mermaid.html

63 lines
2 KiB
HTML
Raw Normal View History

<!-- mermaid-js loader -->
<script type="text/javascript">
function updateMermaid(event) {
if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
const mode = event.data.message;
if (typeof mermaid === 'undefined') {
return;
2021-12-12 21:37:10 +03:00
}
let expectedTheme = mode === ModeToggle.DARK_MODE ? 'dark' : 'default';
let config = { theme: expectedTheme };
2024-04-28 22:25:26 +03:00
{%- comment -%}
Re-render the SVG <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344>
{%- endcomment -%}
const mermaidList = document.getElementsByClassName('mermaid');
[...mermaidList].forEach((elem) => {
const svgCode = elem.previousSibling.children.item(0).innerHTML;
elem.innerHTML = svgCode;
elem.removeAttribute('data-processed');
});
mermaid.initialize(config);
mermaid.init(undefined, '.mermaid');
2021-12-12 21:37:10 +03:00
}
}
2021-12-12 21:37:10 +03:00
(function () {
let initTheme = 'default';
const html = document.documentElement;
2020-12-09 21:42:46 +03:00
if (
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
initTheme = 'dark';
2020-12-10 14:49:43 +03:00
}
2020-12-09 21:42:46 +03:00
2020-12-10 14:49:43 +03:00
let mermaidConf = {
2024-04-28 22:25:26 +03:00
theme: initTheme {%- comment -%} <default | dark | forest | neutral> {%- endcomment -%}
2020-12-10 14:49:43 +03:00
};
2020-12-09 21:42:46 +03:00
2024-04-28 22:25:26 +03:00
{%- comment -%} Create mermaid tag {%- endcomment -%}
const basicList = document.getElementsByClassName('language-mermaid');
[...basicList].forEach((elem) => {
const svgCode = elem.textContent;
const backup = elem.parentElement;
2024-04-20 19:00:50 +03:00
backup.classList.add('d-none');
2024-04-28 22:25:26 +03:00
{%- comment -%} create mermaid node {%- endcomment -%}
let mermaid = document.createElement('pre');
mermaid.classList.add('mermaid');
const text = document.createTextNode(svgCode);
mermaid.appendChild(text);
backup.after(mermaid);
2020-12-10 14:49:43 +03:00
});
2020-12-09 21:42:46 +03:00
2020-12-10 14:49:43 +03:00
mermaid.initialize(mermaidConf);
window.addEventListener('message', updateMermaid);
})();
2020-12-09 21:42:46 +03:00
</script>