Skip to content

Commit a7cac47

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 #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 cda28bd commit a7cac47

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

assets/js/application.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,16 @@ var Graphviz = {
787787
let vizInstance
788788
[...document.querySelectorAll("pre[class=graphviz]")].forEach(async (x) => {
789789
if (!vizInstance) vizInstance = await Viz.instance()
790-
const svg = vizInstance.renderSVGElement(x.innerText)
791-
x.parentNode.insertBefore(svg, x);
790+
const options = {
791+
format: "svg",
792+
}
793+
const svg = vizInstance.renderString(x.innerText, options)
794+
const img = document.createElement('img')
795+
img.setAttribute(
796+
'src',
797+
`data:image/svg+xml;utf8,${encodeURIComponent(svg.substring(svg.indexOf('<svg')))}`
798+
)
799+
x.parentNode.insertBefore(img, x);
792800
x.style.display = 'none'
793801
});
794802
}

0 commit comments

Comments
 (0)