Skip to content

Commit 12ac91b

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 9a5e2a2 commit 12ac91b

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

layouts/_default/baseof.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
'src',
3434
`data:image/svg+xml;utf8,${encodeURIComponent(svg.substring(svg.indexOf('<svg')))}`
3535
)
36+
const alt = x.getAttribute("alt")
37+
if (alt) img.setAttribute("alt", alt)
3638
x.parentNode.insertBefore(img, x);
3739
x.style.display = 'none'
3840
})
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<pre class="graphviz"{{ if (and (isset "engine" .Attributes) (ne "" .Attributes.engine)) }} engine="{{ .Attributes.engine }}"{{ end }}>
1+
<pre class="graphviz"
2+
{{- if (and (isset "engine" .Attributes) (ne "" .Attributes.engine)) }} engine="{{ .Attributes.engine }}"{{ end -}}
3+
{{- if (and (isset "alt" .Attributes) (ne "" .Attributes.alt)) }} alt="{{ .Attributes.alt }}"{{ end -}}
4+
>
25
{{- .Inner | htmlEscape | safeHTML -}}
36
</pre>
47
{{- .Page.Store.Set "hasGraphviz" true }}

script/graphviz-ssr.js

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

0 commit comments

Comments
 (0)