All files / markdown / plugins / mermaid.ts

100.00% Branches 2/2
100.00% Functions 2/2
100.00% Lines 8/8
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
33
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x20
x1
x1
x2
x2
x1
x1
x1

































// Imports
import type { MarkdownIt } from "../types.ts"

/**
 * Add support for Mermaid diagrams.
 *
 * Diagrams are rendered as `<pre class="mermaid">` elements intended for client-side processing:
 * ```html
 * <script type="module">
 *   import mermaid from "https://esm.sh/mermaid"
 *   mermaid.run()
 * </script>
 * ```
 *
 * {@link https://mermaid-js.github.io/mermaid | See Mermaid documentation for more information}.
 *
 * ````md
 * ```mermaid
 * graph TD;
 *   A-->B;
 * ```
 * ````
 * ```html
 * <pre class="mermaid">graph TD;
 *   A-->B;</pre>
 * ```
 */
export default function mermaid(engine: MarkdownIt): void {
  const fence = engine.renderer.rules.fence!
  engine.renderer.rules.fence = (tokens, index, options, env, self) => {
    if (tokens[index].info.trim() === "mermaid")
      return `<pre class="mermaid">${engine.utils.escapeHtml(tokens[index].content.trim())}</pre>\n`
    return fence(tokens, index, options, env, self)
  }
}