aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/markdown/mermaid.js
blob: 1fda101dc0795c8968a959b24057b1e3a3d7f77b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import {random} from '../utils.js';

export async function renderMermaid(els) {
  if (!els || !els.length) return;

  const {mermaidAPI} = await import(/* webpackChunkName: "mermaid" */'mermaid');

  mermaidAPI.initialize({
    startOnLoad: false,
    theme: 'neutral',
    securityLevel: 'strict',
  });

  for (const el of els) {
    mermaidAPI.render(`mermaid-${random(12)}`, el.textContent, (svg, bindFunctions) => {
      const div = document.createElement('div');
      div.classList.add('mermaid-chart');
      div.innerHTML = svg;
      if (typeof bindFunctions === 'function') bindFunctions(div);
      el.closest('pre').replaceWith(div);
    });
  }
}