Skip to content

Commit

Permalink
Fix <br />s not self-closing in mermaid svg (causes issues with certa…
Browse files Browse the repository at this point in the history
…in svg renderers like Chrome's)
  • Loading branch information
christhekeele committed Oct 14, 2024
1 parent 4eb23ce commit c4a4794
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions assets/packs/mermaid/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import mermaid from "mermaid";
import "./main.css";

mermaid.initialize({ startOnLoad: false });
mermaid.initialize({ startOnLoad: false, htmlLabels: false });

export function init(ctx, {diagram, caption, download}) {
ctx.importCSS("main.css")

function render() {
mermaid.render("diagram", diagram).then(({ svg, bindFunctions }) => {
// Fix for: https://github.com/mermaid-js/mermaid/issues/1766
const renderedSvg = svg.replace(/<br>/gi, "<br />")

let contents = document.createElement("div");
contents.id = "contents";
ctx.root.appendChild(contents);

let figure = document.createElement("figure");
figure.id = "figure";
figure.innerHTML = svg;
figure.innerHTML = renderedSvg;
contents.appendChild(figure);

if (caption) {
Expand All @@ -31,9 +34,9 @@ export function init(ctx, {diagram, caption, download}) {
contents.prepend(downloadButton);

contents.querySelector("#download").addEventListener("click", (event) => {
var binaryData = [];
binaryData.push(svg);
const downloadBlob = URL.createObjectURL(new Blob(binaryData, {type: "image/svg+xml"}));
var downloadData = [];
downloadData.push(renderedSvg);
const downloadBlob = URL.createObjectURL(new Blob(downloadData, {type: "image/svg+xml"}));

const downloadLink = document.createElement("a");
downloadLink.href = downloadBlob;
Expand Down

0 comments on commit c4a4794

Please sign in to comment.