Skip to main content

rewrite_mermaid_blocks

Function rewrite_mermaid_blocks 

Source
pub fn rewrite_mermaid_blocks(html: &str) -> String
Expand description

Rewrite mermaid fenced blocks for client-side rendering.

The CommonMark engine emits \u{60}\u{60}\u{60}mermaid fenced blocks as <pre><code class="language-mermaid">…</code></pre>. The mermaid.js bundle, however, looks for <pre class="mermaid">…</pre> (or <div class="mermaid">). This function rewrites every such block so a page that includes <script type="module">import mermaid from "https://…/mermaid.esm.mjs"; mermaid.initialize({startOnLoad:true});</script> renders the diagrams without further work.

The diagram body is passed through verbatim (HTML-escaped — the markdown engine has already done that). mermaid.js handles the unescaping for the diagram parser.

Fast-path: returns immediately when the input contains no language-mermaid substring (SIMD-backed str::contains).

§Examples

use html_generator::math::rewrite_mermaid_blocks;

let input = r#"<p>Diagram below:</p>
<pre><code class="language-mermaid">graph LR
A --&gt; B</code></pre>"#;
let out = rewrite_mermaid_blocks(input);
assert!(out.contains(r#"<pre class="mermaid">"#));
assert!(!out.contains("<code class=\"language-mermaid\""));