Skip to content

Commit

Permalink
Preserve HTML Text for image render hooks
Browse files Browse the repository at this point in the history
Fixes #6639
  • Loading branch information
bep committed Dec 19, 2019
1 parent ad6504e commit a67d95f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 18 additions & 0 deletions hugolib/content_render_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,24 @@ P3: <p>P3. xml-link: https://www.example.org|</p>
func TestRenderLinkWithMarkupInText(t *testing.T) {

b := newTestSitesBuilder(t)
b.WithConfigFile("toml", `
baseURL="https://example.org"
[markup]
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
`)

b.WithTemplates("index.html", `
{{ $p := site.GetPage "p1.md" }}
P1: {{ $p.Content }}
`,
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`,
"_default/_markup/render-image.html", `html-image: {{ .Destination | safeURL }}|Text: {{ .Text | safeHTML }}|Plain: {{ .PlainText | safeHTML }}`,
)

b.WithContent("p1.md", `---
Expand All @@ -233,13 +244,19 @@ title: "p1"
START: [**should be bold**](https://gohugo.io)END
Some regular **markup**.
Image:
![Hello<br> Goodbye](image.jpg)END
`)

b.Build(BuildCfg{})

b.AssertFileContent("public/index.html", `
P1: <p>START: html-link: https://gohugo.io|Text: <strong>should be bold</strong>|Plain: should be boldEND</p>
<p>Some regular <strong>markup</strong>.</p>
<p>html-image: image.jpg|Text: Hello<br> Goodbye|Plain: Hello GoodbyeEND</p>
`)

}
Expand All @@ -256,6 +273,7 @@ RSTART:{{ "**Bold Markdown**" | $p.RenderString }}:REND
RSTART:{{ "**Bold Block Markdown**" | $p.RenderString $optBlock }}:REND
RSTART:{{ "/italic org mode/" | $p.RenderString $optOrg }}:REND
`)

b.WithContent("p1.md", `---
Expand Down
15 changes: 9 additions & 6 deletions markup/goldmark/render_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *linkRenderer) renderImage(w util.BufWriter, source []byte, node ast.Nod
n := node.(*ast.Image)
var h *hooks.Render

ctx, ok := w.(renderContextData)
ctx, ok := w.(*renderContext)
if ok {
h = ctx.RenderContext().RenderHooks
ok = h != nil && h.ImageRenderer != nil
Expand All @@ -147,26 +147,29 @@ func (r *linkRenderer) renderImage(w util.BufWriter, source []byte, node ast.Nod
return r.renderDefaultImage(w, source, node, entering)
}

if !entering {
if entering {
// Store the current pos so we can capture the rendered text.
ctx.pos = ctx.Buffer.Len()
return ast.WalkContinue, nil
}

text := string(n.Text(source))
text := ctx.Buffer.Bytes()[ctx.pos:]
ctx.Buffer.Truncate(ctx.pos)

err := h.ImageRenderer.Render(
w,
linkContext{
page: ctx.DocumentContext().Document,
destination: string(n.Destination),
title: string(n.Title),
text: text,
plainText: text,
text: string(text),
plainText: string(n.Text(source)),
},
)

ctx.AddIdentity(h.ImageRenderer.GetIdentity())

return ast.WalkSkipChildren, err
return ast.WalkContinue, err

}

Expand Down

0 comments on commit a67d95f

Please sign in to comment.