From: Bjørn Erik Pedersen Date: Thu, 19 Dec 2019 16:43:07 +0000 (+0100) Subject: Preserve HTML Text for image render hooks X-Git-Tag: v0.62.0~14 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=a67d95fe1a033ca4934957b5a98b12ecc8a9edbd;p=brevno-suite%2Fhugo Preserve HTML Text for image render hooks Fixes #6639 --- diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go index deccbff4..d206013b 100644 --- a/hugolib/content_render_hooks_test.go +++ b/hugolib/content_render_hooks_test.go @@ -217,6 +217,16 @@ P3:

P3. xml-link: https://www.example.org|

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" }} @@ -224,6 +234,7 @@ 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", `--- @@ -233,6 +244,11 @@ title: "p1" START: [**should be bold**](https://gohugo.io)END Some regular **markup**. + +Image: + +![Hello
Goodbye](image.jpg)END + `) b.Build(BuildCfg{}) @@ -240,6 +256,7 @@ Some regular **markup**. b.AssertFileContent("public/index.html", ` P1:

START: html-link: https://gohugo.io|Text: should be bold|Plain: should be boldEND

Some regular markup.

+

html-image: image.jpg|Text: Hello
Goodbye|Plain: Hello GoodbyeEND

`) } @@ -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", `--- diff --git a/markup/goldmark/render_link.go b/markup/goldmark/render_link.go index cefb786a..c0269bed 100644 --- a/markup/goldmark/render_link.go +++ b/markup/goldmark/render_link.go @@ -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 @@ -147,11 +147,14 @@ 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, @@ -159,14 +162,14 @@ func (r *linkRenderer) renderImage(w util.BufWriter, source []byte, node ast.Nod 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 }