From 4dd209cf732355099c51f9ed796a8185a712bf95 Mon Sep 17 00:00:00 2001 From: Nicholas Bollweg Date: Mon, 5 Feb 2024 08:49:38 -0600 Subject: [PATCH 1/2] handle xhtml void elements in mermaid diagrams --- share/templates/lab/mermaidjs.html.j2 | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/share/templates/lab/mermaidjs.html.j2 b/share/templates/lab/mermaidjs.html.j2 index f75e33ffb..0f7158894 100644 --- a/share/templates/lab/mermaidjs.html.j2 +++ b/share/templates/lab/mermaidjs.html.j2 @@ -84,7 +84,8 @@ url="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs" let results = null; let output = null; try { - const { svg } = await mermaid.render(id, raw, el); + let { svg } = await mermaid.render(id, raw, el); + svg = cleanMermaidSvg(svg); results = makeMermaidImage(svg); output = document.createElement("figure"); results.map(output.appendChild, output); @@ -99,6 +100,38 @@ url="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs" parent.appendChild(output); } + + /** + * Post-process to ensure mermaid diagrams contain only valid SVG and XHTML. + */ + function cleanMermaidSvg(svg) { + return svg.replace(RE_VOID_ELEMENT, replaceVoidElement); + } + + + /** + * A regular expression for all void elements, which may include attributes and + * a slash. + * + * @see https://developer.mozilla.org/en-US/docs/Glossary/Void_element + * + * Of these, only `
` is generated by Mermaid in place of `\n`, + * but _any_ "malformed" tag will break the SVG rendering entirely. + */ + const RE_VOID_ELEMENT = + /<\s*(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s*([^>]*?)\s*>/gi; + + /** + * Ensure a void element is closed with a slash, preserving any attributes. + */ + function replaceVoidElement(match, tag, rest) { + rest = rest.trim(); + if (!rest.endsWith('/')) { + rest = `${rest} /`; + } + return `<${tag} ${rest}>`; + } + void Promise.all([...diagrams].map(renderOneMarmaid)); }); From 8633ffc1b303418e9eb01669f195b97e9ee2479d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:09:43 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb7ec57f0..c38a87d48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1483,6 +1483,7 @@ raw template {%- endblock in_prompt -%} """ + exporter_attr = AttrExporter() output_attr, _ = exporter_attr.from_notebook_node(nb) assert "raw template" in output_attr