Skip to content

Commit

Permalink
* Added a demo using mermaid in Markdown
Browse files Browse the repository at this point in the history
* Fixed a bug which caused mermaid source code to be temporarily visible upon page refresh
  • Loading branch information
Alyxion committed Jan 3, 2025
1 parent 09f4c34 commit 5cd416a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
22 changes: 15 additions & 7 deletions nicegui/elements/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ export default {
await loadResource(window.path_prefix + this.codehilite_css_url);
if (this.use_mermaid) {
this.mermaid = (await import("mermaid")).default;
this.renderMermaid();
await this.mermaid.initialize({ startOnLoad: false });
await this.renderMermaid();
}
},
data() {
return {
mermaid: null,
};
},
updated() {
this.renderMermaid();
async updated() {
if (this.mermaid) {
await this.renderMermaid();
}
},
methods: {
renderMermaid() {
this.$el.querySelectorAll(".mermaid-pre").forEach(async (pre, i) => {
await this.mermaid.run({ nodes: [pre.children[0]] });
});
async renderMermaid() {
const elements = this.$el.querySelectorAll(".mermaid-pre");
for (const pre of elements) {
try {
await this.mermaid.run({ nodes: [pre.children[0]] });
} catch (error) {
console.error('Failed to render mermaid diagram:', error);
}
}
},
},
props: {
Expand Down
8 changes: 8 additions & 0 deletions nicegui/static/nicegui.css
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,11 @@ h6.q-timeline__title {
position: absolute;
left: 1.5em;
}

/* Hide mermaid diagrams until rendered, fixes mermaid source code being temporarily visible on load */
.mermaid {
display: none;
}
.mermaid[data-processed="true"] {
display: block;
}
20 changes: 18 additions & 2 deletions website/documentation/content/markdown_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ def markdown_tables():
''', extras=['tables'])


@doc.demo('Mermaid diagrams', '''
You can use mermaid diagrams with the "mermaid" extra.
See the [markdown2 documentation](https://github.com/trentm/python-markdown2/wiki/Extras#implemented-extras) for a list of available extras.
''')
def mermaid():
md = ui.markdown('''
```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```
''', extras=['mermaid'])


@doc.demo('LaTeX formulas', '''
By activating the "latex" extra, you can use LaTeX formulas.
This requires markdown2 version >=2.5 as well as latex2mathml to be installed.
Expand All @@ -65,8 +81,8 @@ def markdown_latex():
$$e^{i\pi} = -1$$
''', extras=['latex'])


@doc.demo('Change Markdown content', '''
You can change the content of a Markdown element by setting its `content` property or calling `set_content`.
''')
Expand Down

0 comments on commit 5cd416a

Please sign in to comment.