Skip to content

Commit 3e9e79d

Browse files
committed
graphviz: use <img> elements instead of <svg> ones
There are some issues when adding SVGs inside <svg> HTML elements, e.g. that positioning can be harder than with <img> ones, as pointed out at git/git-scm.com#2052 (comment). Let's use <img> elements instead, passing the SVG via data URLs. There are more benefits to that: For example, most modern browsers allow copying an image into the clipboard that is specified as an `<img>` element, but not `<svg>` ones. Likewise, the "Open Image in New Tab" functionality typically only works with the former but not with the latter. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 17ef958 commit 3e9e79d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

layouts/_default/baseof.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@
2020
[...document.querySelectorAll("pre[class=graphviz]")].forEach(async (x) => {
2121
if (!vizInstance) vizInstance = await Viz.instance()
2222
if (x.style.display === 'none') return
23-
const svg = (await Viz.instance()).renderSVGElement(x.innerText, { engine })
24-
x.parentNode.insertBefore(svg, x)
23+
const options = {
24+
format: "svg",
25+
}
26+
const svg = vizInstance.renderString(x.innerText, options)
27+
const img = document.createElement('img')
28+
img.setAttribute(
29+
'src',
30+
`data:image/svg+xml;utf8,${encodeURIComponent(svg.substring(svg.indexOf('<svg')))}`
31+
)
32+
x.parentNode.insertBefore(img, x);
2533
x.style.display = 'none'
2634
})
2735
})

0 commit comments

Comments
 (0)