I have a dynamic webpage that can contain several mermaid diagrams. These diagrams are embedded into a webpage as a source code (e.g. in <pre>
tags) and then should be processed by mermaid to be replaced with the actual diagrams. Currently, I have two problems:
The approach that I found (adapting answers to this question) does not work with the latest version of mermaid.
It does not work with several diagrams, only the last one is processed.
Here is what I have now. I mock the script that adds diagrams with add_diagram
. Then, if I run make_diagrams()
, only the second one is rendered. Also, it doesn't work correctly if I switch to the latest version of mermaid.
function add_diagram() {
diagram_code = `
<pre class="mermaid">
graph TD
A[First diagram] --> B[Something]
</pre>
<pre class="mermaid">
graph TD
A[Second diagram] --> B[Something]
</pre>
`;
document.getElementById("output").innerHTML += diagram_code;
}
function make_diagrams() {
$("pre.mermaid").each(function() {
mermaid.render(
"body",
this.textContent,
(function (code) {this.outerHTML = code}).bind(this))
})
}
<div id="output"/>
<button onclick="add_diagram()">add diagram</button>
<button onclick="make_diagrams()">make diagrams</button>
<script src="https://unpkg.com/mermaid@8/dist/mermaid.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
How to make it correctly?