Skip to content

Commit d04135c

Browse files
committed
graphviz: support the alt attribute
Since we're generating `<img>` elements anyway, might just as well keep that accessibility support. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent f2413fd commit d04135c

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

assets/js/application.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ var Graphviz = {
800800
'src',
801801
`data:image/svg+xml;utf8,${encodeURIComponent(svg.substring(svg.indexOf('<svg')))}`
802802
)
803+
const alt = x.getAttribute("alt")
804+
if (alt) img.setAttribute("alt", alt)
803805
x.parentNode.insertBefore(img, x);
804806
x.style.display = 'none'
805807
});

layouts/shortcodes/graphviz.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<pre class="graphviz"{{ if (ne "" (.Get "engine")) }} engine="{{(.Get "engine")}}"{{ end }}>
1+
<pre class="graphviz"{{ if (ne "" (.Get "engine")) }} engine="{{(.Get "engine")}}"{{ end }}{{ if (ne "" (.Get "alt")) }} alt="{{ (.Get "alt") }}"{{ end }}>
22
{{ .Inner | htmlEscape | safeHTML }}
33
</pre>
44
{{ .Page.Store.Set "hasGraphviz" true }}

script/graphviz-ssr.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import Viz from "../static/js/viz-global.js"
3232
},
3333
engine,
3434
})
35+
const alt = pre.getAttribute("alt")
36+
const altAttr = !alt ? '' : ` alt='${alt.replaceAll("&", "&amp;").replaceAll("'", "&#39;")}'`
3537
const dataURL = `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`
36-
pre.replaceWith(`<img src="${dataURL}"`)
38+
pre.replaceWith(`<img${altAttr} src="${dataURL}"`)
3739
}
3840
console.log(`Rewriting ${path}`)
3941
writeFileSync(`${path}`, html.toString())

0 commit comments

Comments
 (0)