Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mermaid markdown documentation & visualization enhancement #4170

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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