Prevent double escaping of image alt-text in Goldmar typographer

Fixes #11045
This commit is contained in:
Bjørn Erik Pedersen
2023-05-30 13:21:45 +02:00
parent 32585696be
commit 3f497d496f
2 changed files with 39 additions and 1 deletions

View File

@@ -225,7 +225,7 @@ func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, nod
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_, _ = w.WriteString(`" alt="`)
_, _ = w.Write(util.EscapeHTML(n.Text(source)))
_, _ = w.Write(nodeToHTMLText(n, source))
_ = w.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
@@ -475,3 +475,18 @@ func (e *links) Extend(m goldmark.Markdown) {
util.Prioritized(newLinkRenderer(e.cfg), 100),
))
}
// Borrowed from Goldmark.
func nodeToHTMLText(n ast.Node, source []byte) []byte {
var buf bytes.Buffer
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
if s, ok := c.(*ast.String); ok && s.IsCode() {
buf.Write(s.Text(source))
} else if !c.HasChildren() {
buf.Write(util.EscapeHTML(c.Text(source)))
} else {
buf.Write(nodeToHTMLText(c, source))
}
}
return buf.Bytes()
}