mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-01 12:41:43 +08:00
28 lines
605 B
JavaScript
28 lines
605 B
JavaScript
|
let idCount = 0;
|
||
|
let getId = () => `mermaid-graph-${idCount++}`;
|
||
|
|
||
|
let mermaidInitialized = false;
|
||
|
|
||
|
function importMermaid() {
|
||
|
return import(
|
||
|
/* webpackChunkName: "mermaid" */
|
||
|
"mermaid"
|
||
|
).then(({ default: mermaid }) => {
|
||
|
if (!mermaidInitialized) {
|
||
|
mermaid.initialize({ startOnLoad: false });
|
||
|
mermaidInitialized = true;
|
||
|
}
|
||
|
return mermaid;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function renderMermaid(value) {
|
||
|
return importMermaid().then((mermaid) => {
|
||
|
try {
|
||
|
return mermaid.render(getId(), value);
|
||
|
} catch (e) {
|
||
|
return `<pre><code>${e.message}</code></pre>`;
|
||
|
}
|
||
|
});
|
||
|
}
|