Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/formats/html/mermaid/mermaid-postprocess-shim.js
12923 views
1
// mermaid-postprocess-shim.js
2
// code to postprocess mermaid pregenerated HTML, to fix https://github.com/quarto-dev/quarto-cli/issues/1622
3
//
4
// Copyright (C) 2022 Posit Software, PBC
5
6
const _quartoMermaid = {
7
postProcess(el) {
8
// recompute foreignObj's height and displacement
9
// based on the actual computed style with the existing CSS.
10
// this won't be perfect but will be an improvement.
11
for (const foreignObj of Array.from(el.querySelectorAll("foreignObject"))) {
12
const div = foreignObj.querySelector("div");
13
const computedStyle = window.getComputedStyle(div);
14
const divHeight = computedStyle.height;
15
foreignObj.setAttribute("height", divHeight);
16
17
// this fix is not going to work for animated SVGs coming from mermaid
18
// shrug
19
20
// this syntax gives us a string
21
const transform = foreignObj.parentElement.getAttribute("transform");
22
const m = transform.match(/translate\((.+),(.+)\)/);
23
foreignObj.parentElement.setAttribute(
24
"transform",
25
`translate(${m[1]},${-Number(divHeight.slice(0, -2) / 2)})`
26
);
27
}
28
},
29
};
30
31
// deno-lint-ignore no-window-prefix
32
window.addEventListener(
33
"load",
34
function () {
35
for (const svgEl of Array.from(
36
document.querySelectorAll("div.cell-output-display svg")
37
).filter(
38
(el) =>
39
el.querySelector("desc").id &&
40
el.querySelector("desc").id.startsWith("chart-desc-mermaid")
41
)) {
42
_quartoMermaid.postProcess(svgEl);
43
}
44
},
45
false
46
);
47
48