}
+// 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 }}|Plain: {{ .PlainText | 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>|Plain: should be boldEND</p>
+<p>Some regular <strong>markup</strong>.</p>
+`)
+
+}
+
func TestRenderString(t *testing.T) {
b := newTestSitesBuilder(t)
package goldmark
import (
- "bufio"
"bytes"
"fmt"
+ "math/bits"
"path/filepath"
"runtime/debug"
return c.ids
}
+type bufWriter struct {
+ *bytes.Buffer
+}
+
+const maxInt = 1<<(bits.UintSize-1) - 1
+
+func (b *bufWriter) Available() int {
+ return maxInt
+}
+
+func (b *bufWriter) Buffered() int {
+ return b.Len()
+}
+
+func (b *bufWriter) Flush() error {
+ return nil
+}
+
type renderContext struct {
- util.BufWriter
+ *bufWriter
+ pos int
renderContextData
}
}
}()
- buf := &bytes.Buffer{}
+ buf := &bufWriter{Buffer: &bytes.Buffer{}}
result = buf
pctx := newParserContext(ctx)
reader := text.NewReader(ctx.Src)
ids: identity.NewManager(converterIdentity),
}
- w := renderContext{
- BufWriter: bufio.NewWriter(buf),
+ w := &renderContext{
+ bufWriter: buf,
renderContextData: rcx,
}
destination string
title string
text string
+ plainText string
}
func (ctx linkContext) Destination() string {
return ctx.text
}
+func (ctx linkContext) PlainText() string {
+ return ctx.plainText
+}
+
func (ctx linkContext) Title() string {
return ctx.title
}
return ast.WalkContinue, nil
}
+ text := string(n.Text(source))
+
err := h.ImageRenderer.Render(
w,
linkContext{
page: ctx.DocumentContext().Document,
destination: string(n.Destination),
title: string(n.Title),
- text: string(n.Text(source)),
+ text: text,
+ plainText: text,
},
)
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
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),
+ plainText: string(n.Text(source)),
},
)
ctx.AddIdentity(h.LinkRenderer.GetIdentity())
- // Do not render the inner text.
- return ast.WalkSkipChildren, err
+ return ast.WalkContinue, err
}