|
3 | 3 | # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/03_js.ipynb.
|
4 | 4 |
|
5 | 5 | # %% auto 0
|
6 |
| -__all__ = ['marked_imp', 'npmcdn', 'light_media', 'dark_media', 'MarkdownJS', 'KatexMarkdownJS', 'HighlightJS', 'SortableJS'] |
| 6 | +__all__ = ['marked_imp', 'npmcdn', 'light_media', 'dark_media', 'MarkdownJS', 'KatexMarkdownJS', 'HighlightJS', 'SortableJS', |
| 7 | + 'MermaidJS'] |
7 | 8 |
|
8 | 9 | # %% ../nbs/api/03_js.ipynb
|
9 | 10 | import re
|
@@ -87,3 +88,43 @@ def SortableJS(
|
87 | 88 | proc_htmx('%s', Sortable.create);
|
88 | 89 | """ % sel
|
89 | 90 | return Script(src, type='module')
|
| 91 | + |
| 92 | +# %% ../nbs/api/03_js.ipynb |
| 93 | +def MermaidJS( |
| 94 | + sel='.language-mermaid', # CSS selector for mermaid elements |
| 95 | + theme='base', # Mermaid theme to use |
| 96 | + ): |
| 97 | + "Implements browser-based Mermaid diagram rendering." |
| 98 | + src = """ |
| 99 | +import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs'; |
| 100 | +
|
| 101 | +mermaid.initialize({ |
| 102 | + startOnLoad: false, |
| 103 | + theme: '%s', |
| 104 | + securityLevel: 'loose', |
| 105 | + flowchart: { useMaxWidth: false, useMaxHeight: false } |
| 106 | +}); |
| 107 | +
|
| 108 | +function renderMermaidDiagrams(element, index) { |
| 109 | + try { |
| 110 | + const graphDefinition = element.textContent; |
| 111 | + const graphId = `mermaid-diagram-${index}`; |
| 112 | + mermaid.render(graphId, graphDefinition) |
| 113 | + .then(({svg, bindFunctions}) => { |
| 114 | + element.innerHTML = svg; |
| 115 | + bindFunctions?.(element); |
| 116 | + }) |
| 117 | + .catch(error => { |
| 118 | + console.error(`Error rendering Mermaid diagram ${index}:`, error); |
| 119 | + element.innerHTML = `<p>Error rendering diagram: ${error.message}</p>`; |
| 120 | + }); |
| 121 | + } catch (error) { |
| 122 | + console.error(`Error processing Mermaid diagram ${index}:`, error); |
| 123 | + } |
| 124 | +} |
| 125 | +
|
| 126 | +// Assuming proc_htmx is a function that triggers rendering |
| 127 | +proc_htmx('%s', renderMermaidDiagrams); |
| 128 | +""" % (theme, sel) |
| 129 | + return Script(src, type='module') |
| 130 | + |
0 commit comments