git@cosmocr.at/gitweb.git @ main · summary refs log
main / web / mermaid-init.js /
web/mermaid-init.js · 1003 B · raw
view: Source ☐ force
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
"use strict";
// Rendered markdown may contain <pre class="mermaid"> diagram blocks. This tiny
// script is loaded on every markdown/git page but does nothing unless such a
// block is present, in which case it lazy-loads the (large) mermaid bundle and
// draws the diagrams. Keeping the load conditional means ordinary pages stay
// script-light, and everything is served same-origin (no CDN).
(function () {
  if (!document.querySelector(".mermaid")) return;
  var s = document.createElement("script");
  s.src = "/_assets/mermaid.min.js";
  s.onload = function () {
    try {
      // securityLevel "strict" disables click handlers and HTML in labels, so
      // diagram source from a repo cannot inject markup or scripts.
      window.mermaid.initialize({ startOnLoad: false, securityLevel: "strict" });
      window.mermaid.init(undefined, document.querySelectorAll(".mermaid"));
    } catch (e) {
      console.error("mermaid render failed:", e);
    }
  };
  document.head.appendChild(s);
})();