Skip to content

Commit 29e5146

Browse files
committed
forgot to add #/ to js.ipynb cell.
1 parent 462f1c7 commit 29e5146

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

fasthtml/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
'fasthtml.js': { 'fasthtml.js.HighlightJS': ('api/js.html#highlightjs', 'fasthtml/js.py'),
118118
'fasthtml.js.KatexMarkdownJS': ('api/js.html#katexmarkdownjs', 'fasthtml/js.py'),
119119
'fasthtml.js.MarkdownJS': ('api/js.html#markdownjs', 'fasthtml/js.py'),
120+
'fasthtml.js.MermaidJS': ('api/js.html#mermaidjs', 'fasthtml/js.py'),
120121
'fasthtml.js.SortableJS': ('api/js.html#sortablejs', 'fasthtml/js.py'),
121122
'fasthtml.js.dark_media': ('api/js.html#dark_media', 'fasthtml/js.py'),
122123
'fasthtml.js.light_media': ('api/js.html#light_media', 'fasthtml/js.py')},

fasthtml/js.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/03_js.ipynb.
44

55
# %% 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']
78

89
# %% ../nbs/api/03_js.ipynb
910
import re
@@ -87,3 +88,43 @@ def SortableJS(
8788
proc_htmx('%s', Sortable.create);
8889
""" % sel
8990
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+

nbs/api/03_js.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
"metadata": {},
276276
"outputs": [],
277277
"source": [
278+
"#| export\n",
278279
"def MermaidJS(\n",
279280
" sel='.language-mermaid', # CSS selector for mermaid elements\n",
280281
" theme='base', # Mermaid theme to use\n",

0 commit comments

Comments
 (0)