summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/render_mermaid.js
blob: 41942c04a4ee7045b876106a2aba546fc9b63773 (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
29
30
31
32
// Renders diagrams and flowcharts from text using Mermaid in any element with the
// `js-render-mermaid` class.
//
// Example markup:
//
// <pre class="js-render-mermaid">
//  graph TD;
//    A-- > B;
//    A-- > C;
//    B-- > D;
//    C-- > D;
// </pre>
//

import Flash from './flash';

export default function renderMermaid($els) {
  if (!$els.length) return;

  import(/* webpackChunkName: 'mermaid' */ 'blackst0ne-mermaid').then((mermaid) => {
    mermaid.initialize({
      loadOnStart: false,
      theme: 'neutral',
    });

    $els.each((i, el) => {
      mermaid.init(undefined, el);
    });
  }).catch((err) => {
    Flash(`Can't load mermaid module: ${err}`);
  });
}