diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-11-14 10:16:52 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-11-14 14:31:04 +0100 |
commit | 588c9019cfdf80cf69b287ea6cd53fd8d7c4ae02 (patch) | |
tree | 233d8f6c7f139c4ddde496513aec50af6e5adeb7 /markup | |
parent | ce9cf882a54e9c55dcc7970398ae79c3b251f79e (diff) | |
download | hugo-588c9019cfdf80cf69b287ea6cd53fd8d7c4ae02.tar.gz hugo-588c9019cfdf80cf69b287ea6cd53fd8d7c4ae02.zip |
deps: Upgrade github.com/yuin/goldmark v1.7.4 => v1.7.8
Closes #12958
Diffstat (limited to 'markup')
-rw-r--r-- | markup/goldmark/convert.go | 1 | ||||
-rw-r--r-- | markup/goldmark/internal/render/context.go | 31 | ||||
-rw-r--r-- | markup/goldmark/render_hooks.go | 104 |
3 files changed, 111 insertions, 25 deletions
diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go index ea3bbc4ae..823a43c9d 100644 --- a/markup/goldmark/convert.go +++ b/markup/goldmark/convert.go @@ -43,6 +43,7 @@ import ( ) const ( + // Don't change this. This pattern is lso used in the image render hooks. internalAttrPrefix = "_h__" ) diff --git a/markup/goldmark/internal/render/context.go b/markup/goldmark/internal/render/context.go index b8cf9ba54..cd64fc944 100644 --- a/markup/goldmark/internal/render/context.go +++ b/markup/goldmark/internal/render/context.go @@ -16,9 +16,13 @@ package render import ( "bytes" "math/bits" + "strings" "sync" + bp "github.com/gohugoio/hugo/bufferpool" + htext "github.com/gohugoio/hugo/common/text" + "github.com/gohugoio/hugo/tpl" "github.com/gohugoio/hugo/markup/converter" "github.com/gohugoio/hugo/markup/converter/hooks" @@ -258,3 +262,30 @@ func (c *hookBase) Position() htext.Position { func (c *hookBase) PositionerSourceTarget() []byte { return c.getSourceSample() } + +// TextPlain returns a plain text representation of the given node. +// Goldmark's Node.Text was deprecated in 1.7.8. +func TextPlain(n ast.Node, source []byte) string { + buf := bp.GetBuffer() + defer bp.PutBuffer(buf) + + for c := n.FirstChild(); c != nil; c = c.NextSibling() { + textPlainTo(c, source, buf) + } + return buf.String() +} + +func textPlainTo(c ast.Node, source []byte, buf *bytes.Buffer) { + if c == nil { + return + } + switch c := c.(type) { + case *ast.RawHTML: + s := strings.TrimSpace(tpl.StripHTML(string(c.Segments.Value(source)))) + buf.WriteString(s) + case *ast.Text: + buf.Write(c.Segment.Value(source)) + default: + textPlainTo(c.FirstChild(), source, buf) + } +} diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go index bacb41a37..12cf00455 100644 --- a/markup/goldmark/render_hooks.go +++ b/markup/goldmark/render_hooks.go @@ -200,7 +200,7 @@ func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.N destination: string(n.Destination), title: string(n.Title), text: hstring.HTML(text), - plainText: string(n.Text(source)), + plainText: render.TextPlain(n, source), AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral), }, ordinal: ordinal, @@ -223,7 +223,7 @@ func (r *hookedRenderer) filterInternalAttributes(attrs []ast.Attribute) []ast.A } // Fall back to the default Goldmark render funcs. Method below borrowed from: -// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404 +// https://github.com/yuin/goldmark func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { return ast.WalkContinue, nil @@ -234,7 +234,7 @@ func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, nod _, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true))) } _, _ = w.WriteString(`" alt="`) - _, _ = w.Write(nodeToHTMLText(n, source)) + r.renderTexts(w, source, n) _ = w.WriteByte('"') if n.Title != nil { _, _ = w.WriteString(` title="`) @@ -242,8 +242,7 @@ func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, nod _ = w.WriteByte('"') } if n.Attributes() != nil { - attrs := r.filterInternalAttributes(n.Attributes()) - attributes.RenderASTAttributes(w, attrs...) + html.RenderAttributes(w, n, html.ImageAttributeFilter) } if r.XHTML { _, _ = w.WriteString(" />") @@ -289,7 +288,7 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No destination: string(n.Destination), title: string(n.Title), text: hstring.HTML(text), - plainText: string(n.Text(source)), + plainText: render.TextPlain(n, source), AttributesHolder: attributes.Empty, }, ) @@ -297,6 +296,79 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No return ast.WalkContinue, err } +// Borrowed from Goldmark's HTML renderer. +func (r *hookedRenderer) renderTexts(w util.BufWriter, source []byte, n ast.Node) { + for c := n.FirstChild(); c != nil; c = c.NextSibling() { + if s, ok := c.(*ast.String); ok { + _, _ = r.renderString(w, source, s, true) + } else if t, ok := c.(*ast.Text); ok { + _, _ = r.renderText(w, source, t, true) + } else { + r.renderTexts(w, source, c) + } + } +} + +// Borrowed from Goldmark's HTML renderer. +func (r *hookedRenderer) renderString(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return ast.WalkContinue, nil + } + n := node.(*ast.String) + if n.IsCode() { + _, _ = w.Write(n.Value) + } else { + if n.IsRaw() { + r.Writer.RawWrite(w, n.Value) + } else { + r.Writer.Write(w, n.Value) + } + } + return ast.WalkContinue, nil +} + +// Borrowed from Goldmark's HTML renderer. +func (r *hookedRenderer) renderText(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return ast.WalkContinue, nil + } + n := node.(*ast.Text) + segment := n.Segment + if n.IsRaw() { + r.Writer.RawWrite(w, segment.Value(source)) + } else { + value := segment.Value(source) + r.Writer.Write(w, value) + if n.HardLineBreak() || (n.SoftLineBreak() && r.HardWraps) { + if r.XHTML { + _, _ = w.WriteString("<br />\n") + } else { + _, _ = w.WriteString("<br>\n") + } + } else if n.SoftLineBreak() { + // TODO(bep) we use these methods a fallback to default rendering when no image/link hooks are defined. + // I don't think the below is relevant in these situations, but if so, we need to create a PR + // upstream to export softLineBreak. + /*if r.EastAsianLineBreaks != html.EastAsianLineBreaksNone && len(value) != 0 { + sibling := node.NextSibling() + if sibling != nil && sibling.Kind() == ast.KindText { + if siblingText := sibling.(*ast.Text).Value(source); len(siblingText) != 0 { + thisLastRune := util.ToRune(value, len(value)-1) + siblingFirstRune, _ := utf8.DecodeRune(siblingText) + if r.EastAsianLineBreaks.softLineBreak(thisLastRune, siblingFirstRune) { + _ = w.WriteByte('\n') + } + } + } + } else { + _ = w.WriteByte('\n') + }*/ + _ = w.WriteByte('\n') + } + } + return ast.WalkContinue, nil +} + // Fall back to the default Goldmark render funcs. Method below borrowed from: // https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404 func (r *hookedRenderer) renderLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { @@ -443,7 +515,7 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast level: n.Level, anchor: string(anchor), text: hstring.HTML(text), - plainText: string(n.Text(source)), + plainText: render.TextPlain(n, source), AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral), }, ) @@ -478,21 +550,3 @@ func (e *links) Extend(m goldmark.Markdown) { util.Prioritized(newLinkRenderer(e.cfg), 100), )) } - -// Borrowed from Goldmark. -func nodeToHTMLText(n ast.Node, source []byte) []byte { - var buf bytes.Buffer - for c := n.FirstChild(); c != nil; c = c.NextSibling() { - if s, ok := c.(*ast.String); ok && s.IsCode() { - buf.Write(s.Text(source)) - } else if !c.HasChildren() { - buf.Write(util.EscapeHTML(c.Text(source))) - if t, ok := c.(*ast.Text); ok && t.SoftLineBreak() { - buf.WriteByte('\n') - } - } else { - buf.Write(nodeToHTMLText(c, source)) - } - } - return buf.Bytes() -} |