summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/render_mermaid.js
blob: a253601f8e8f53541d0dc2e7bd9ed69ebf60eac5 (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
// 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';

$.fn.renderMermaid = function renderMermaid() {
  if (this.length === 0) return;

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

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