Skip to content

Commit 6aa72ac

Browse files
committed
Fix render hook's PlainText with typographer extension enabled
Fixes #13286 Fixes #13292
1 parent 9885e70 commit 6aa72ac

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

markup/goldmark/goldmark_integration_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,39 @@ a <!-- b --> c
527527
)
528528
}
529529

530+
// Issue 13286
531+
func TestImageAltApostrophesWithTypographer(t *testing.T) {
532+
t.Parallel()
533+
534+
files := `
535+
-- hugo.toml --
536+
[markup.goldmark.extensions.typographer]
537+
disable = false
538+
[markup.goldmark.renderHooks.image]
539+
enableDefault = true
540+
-- content/p1.md --
541+
---
542+
title: "p1"
543+
---
544+
545+
## Image
546+
547+
![A's is > than B's](some-image.png)
548+
549+
550+
-- layouts/_default/single.html --
551+
{{ .Content }}
552+
`
553+
554+
b := hugolib.Test(t, files)
555+
556+
b.AssertFileContentExact("public/p1/index.html",
557+
// Note that this markup is slightly different than the one produced by the default Goldmark renderer,
558+
// see issue 13292.
559+
"alt=\"A’s is &gt; than B’s\">",
560+
)
561+
}
562+
530563
// Issue #7332
531564
// Issue #11587
532565
func TestGoldmarkEmojiExtension(t *testing.T) {

markup/goldmark/internal/render/context.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/gohugoio/hugo/markup/converter"
2828
"github.com/gohugoio/hugo/markup/converter/hooks"
2929
"github.com/yuin/goldmark/ast"
30+
"github.com/yuin/goldmark/util"
3031
)
3132

3233
type BufWriter struct {
@@ -264,6 +265,8 @@ func (c *hookBase) PositionerSourceTarget() []byte {
264265
}
265266

266267
// TextPlain returns a plain text representation of the given node.
268+
// This will resolve any lefover HTML entities. This will typically be
269+
// entities inserted by e.g. the typographer extension.
267270
// Goldmark's Node.Text was deprecated in 1.7.8.
268271
func TextPlain(n ast.Node, source []byte) string {
269272
buf := bp.GetBuffer()
@@ -272,7 +275,7 @@ func TextPlain(n ast.Node, source []byte) string {
272275
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
273276
textPlainTo(c, source, buf)
274277
}
275-
return buf.String()
278+
return string(util.ResolveEntityNames(buf.Bytes()))
276279
}
277280

278281
func textPlainTo(c ast.Node, source []byte, buf *bytes.Buffer) {
@@ -283,6 +286,8 @@ func textPlainTo(c ast.Node, source []byte, buf *bytes.Buffer) {
283286
case *ast.RawHTML:
284287
s := strings.TrimSpace(tpl.StripHTML(string(c.Segments.Value(source))))
285288
buf.WriteString(s)
289+
case *ast.String:
290+
buf.Write(c.Value)
286291
case *ast.Text:
287292
buf.Write(c.Segment.Value(source))
288293
default:

0 commit comments

Comments
 (0)