Path: blob/main/src/resources/formats/html/mermaid/mermaid-postprocess-shim.js
12923 views
// mermaid-postprocess-shim.js1// code to postprocess mermaid pregenerated HTML, to fix https://github.com/quarto-dev/quarto-cli/issues/16222//3// Copyright (C) 2022 Posit Software, PBC45const _quartoMermaid = {6postProcess(el) {7// recompute foreignObj's height and displacement8// based on the actual computed style with the existing CSS.9// this won't be perfect but will be an improvement.10for (const foreignObj of Array.from(el.querySelectorAll("foreignObject"))) {11const div = foreignObj.querySelector("div");12const computedStyle = window.getComputedStyle(div);13const divHeight = computedStyle.height;14foreignObj.setAttribute("height", divHeight);1516// this fix is not going to work for animated SVGs coming from mermaid17// shrug1819// this syntax gives us a string20const transform = foreignObj.parentElement.getAttribute("transform");21const m = transform.match(/translate\((.+),(.+)\)/);22foreignObj.parentElement.setAttribute(23"transform",24`translate(${m[1]},${-Number(divHeight.slice(0, -2) / 2)})`25);26}27},28};2930// deno-lint-ignore no-window-prefix31window.addEventListener(32"load",33function () {34for (const svgEl of Array.from(35document.querySelectorAll("div.cell-output-display svg")36).filter(37(el) =>38el.querySelector("desc").id &&39el.querySelector("desc").id.startsWith("chart-desc-mermaid")40)) {41_quartoMermaid.postProcess(svgEl);42}43},44false45);464748