-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
38 lines (34 loc) · 1.63 KB
/
index.ts
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
36
37
38
import {CMBlockMarkerHelper} from "../../../utils/CMBlockMarkerHelper";
import mermaid from 'mermaid'
import {LineHandle} from "codemirror";
const ENHANCEMENT_MERMAID_SPAN_MARKER_CLASS = 'enhancement-mermaid-block-marker';
const ENHANCEMENT_MERMAID_SPAN_MARKER_LINE_CLASS = 'enhancement-mermaid-block-marker-line';
// Initialise the mermaid API.
mermaid.initialize({ startOnLoad: false })
export default function mermaidRender(cm) {
// Block Katex Math Render
new CMBlockMarkerHelper(cm, null, /^\s*```mermaid\s*$/, /^\s*```\s*$/, (beginMatch, endMatch, content, fromLine, toLine) => {
// code from zettlr
let svg = document.createElement('span')
svg.innerText = "..."
svg.classList.add('mermaid-chart')
mermaid.render(`graphDivL${fromLine}-L${toLine}${Date.now()}`, content).then((renderResult) => {
svg.innerHTML = renderResult.svg
}, (err) => {
svg.classList.add('error')
// TODO: Localise!
svg.innerText = `Could not render Graph:\n\n${err.message as string}`
})
return svg;
}, () => {
const span = document.createElement('span');
span.textContent = '===> Folded Mermaid Code Block <===';
span.style.cssText = 'color: lightgray; font-size: smaller; font-style: italic;';
return span;
},ENHANCEMENT_MERMAID_SPAN_MARKER_CLASS, true);
cm.on('renderLine', (editor, line: LineHandle, element: Element) => {
if (element.getElementsByClassName(ENHANCEMENT_MERMAID_SPAN_MARKER_CLASS).length > 0) {
element.classList.add(ENHANCEMENT_MERMAID_SPAN_MARKER_LINE_CLASS);
}
})
}