Skip to content

Commit

Permalink
Preserve HTML Text for link render hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 18, 2019
1 parent 3e31615 commit fd5c206
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
31 changes: 31 additions & 0 deletions hugolib/content_render_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,37 @@ P3: <p>P3. xml-link: https://www.example.org|</p>

}

// https://github.com/gohugoio/hugo/issues/6629
func TestRenderLinkWithMarkupInText(t *testing.T) {

b := newTestSitesBuilder(t)

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

b.WithContent("p1.md", `---
title: "p1"
---
START: [**should be bold**](https://gohugo.io)END
Some regular **markup**.
`)

b.Build(BuildCfg{})

b.AssertFileContent("public/index.html", `
P1: <p>START: html-link: https://gohugo.io|Text: <strong>should be bold</strong>END</p>
<p>Some regular <strong>markup</strong>.</p>
`)

}

func TestRenderString(t *testing.T) {

b := newTestSitesBuilder(t)
Expand Down
27 changes: 22 additions & 5 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package goldmark

import (
"bufio"
"bytes"
"fmt"
"math"
"path/filepath"
"runtime/debug"

Expand Down Expand Up @@ -162,8 +162,25 @@ func (c converterResult) GetIdentities() identity.Identities {
return c.ids
}

type bufWriter struct {
*bytes.Buffer
}

func (b *bufWriter) Available() int {
return int(math.MaxInt64)
}

func (b *bufWriter) Buffered() int {
return b.Len()
}

func (b *bufWriter) Flush() error {
return nil
}

type renderContext struct {
util.BufWriter
*bufWriter
pos int
renderContextData
}

Expand Down Expand Up @@ -205,7 +222,7 @@ func (c *goldmarkConverter) Convert(ctx converter.RenderContext) (result convert
}
}()

buf := &bytes.Buffer{}
buf := &bufWriter{Buffer: &bytes.Buffer{}}
result = buf
pctx := newParserContext(ctx)
reader := text.NewReader(ctx.Src)
Expand All @@ -221,8 +238,8 @@ func (c *goldmarkConverter) Convert(ctx converter.RenderContext) (result convert
ids: identity.NewManager(converterIdentity),
}

w := renderContext{
BufWriter: bufio.NewWriter(buf),
w := &renderContext{
bufWriter: buf,
renderContextData: rcx,
}

Expand Down
15 changes: 10 additions & 5 deletions markup/goldmark/render_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (r *linkRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node
n := node.(*ast.Link)
var h *hooks.Render

ctx, ok := w.(renderContextData)
ctx, ok := w.(*renderContext)
if ok {
h = ctx.RenderContext().RenderHooks
ok = h != nil && h.LinkRenderer != nil
Expand All @@ -176,24 +176,29 @@ func (r *linkRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node
return r.renderDefaultLink(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 := ctx.Buffer.Bytes()[ctx.pos:]
ctx.Buffer.Truncate(ctx.pos)

err := h.LinkRenderer.Render(
w,
linkContext{
page: ctx.DocumentContext().Document,
destination: string(n.Destination),
title: string(n.Title),
text: string(n.Text(source)),
text: string(text),
//text: string(n.Text(source)), TODO1 add PlainText?
},
)

ctx.AddIdentity(h.LinkRenderer.GetIdentity())

// Do not render the inner text.
return ast.WalkSkipChildren, err
return ast.WalkContinue, err

}

Expand Down

0 comments on commit fd5c206

Please sign in to comment.