]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Rename hstring.RenderedHTML => hstring.HTML
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 1 Sep 2024 08:03:10 +0000 (10:03 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 1 Sep 2024 08:04:20 +0000 (10:04 +0200)
And add a comment about why it exists.

common/types/hstring/stringtypes.go
common/types/hstring/stringtypes_test.go
hugolib/page__content.go
markup/converter/hooks/hooks.go
markup/goldmark/blockquotes/blockquotes.go
markup/goldmark/render_hooks.go
markup/goldmark/tables/tables.go

index 05977ddceeb10887a4dc71017ab8d6547b37a22e..53ce2068f0179728e8cf63d706d1e016a19fd435 100644 (file)
@@ -19,16 +19,18 @@ import (
        "github.com/gohugoio/hugo/common/types"
 )
 
-var _ types.PrintableValueProvider = RenderedHTML("")
+var _ types.PrintableValueProvider = HTML("")
 
-// RenderedHTML is a string that represents rendered HTML.
-// When printed in templates it will be rendered as template.HTML and considered safe.
-type RenderedHTML string
+// HTML is a string that represents rendered HTML.
+// When printed in templates it will be rendered as template.HTML and considered safe so no need to pipe it into `safeHTML`.
+// This type was introduced as a wasy to prevent a common case of inifinite recursion in the template rendering
+// when the `linkify` option is enabled with a common (wrong) construct like `{{ .Text | .Page.RenderString }}` in a hook template.
+type HTML string
 
-func (s RenderedHTML) String() string {
+func (s HTML) String() string {
        return string(s)
 }
 
-func (s RenderedHTML) PrintableValue() any {
+func (s HTML) PrintableValue() any {
        return template.HTML(s)
 }
index 75b7af13c25cb52f2605a1b7ef45a525ae56badd..05e2c22b9b61dac63e2da8550de5236dcd72a4af 100644 (file)
@@ -25,6 +25,6 @@ func TestRenderedString(t *testing.T) {
        c := qt.New(t)
 
        // Validate that it will behave like a string in Hugo settings.
-       c.Assert(cast.ToString(RenderedHTML("Hugo")), qt.Equals, "Hugo")
-       c.Assert(template.HTML(RenderedHTML("Hugo")), qt.Equals, template.HTML("Hugo"))
+       c.Assert(cast.ToString(HTML("Hugo")), qt.Equals, "Hugo")
+       c.Assert(template.HTML(HTML("Hugo")), qt.Equals, template.HTML("Hugo"))
 }
index 31080b929ae2a13e6c46759cf249564bbcec4631..cc93fe5d5cf15308188c31f58cbcab35e9762be7 100644 (file)
@@ -928,7 +928,7 @@ func (c *cachedContentScope) RenderString(ctx context.Context, args ...any) (tem
 
        contentToRenderv := args[sidx]
 
-       if _, ok := contentToRenderv.(hstring.RenderedHTML); ok {
+       if _, ok := contentToRenderv.(hstring.HTML); ok {
                // This content is already rendered, this is potentially
                // a infinite recursion.
                return "", errors.New("text is already rendered, repeating it may cause infinite recursion")
index 0232b619f970dc3c3dd83ca84ced72a71a58f604..96c165321cea5e6169a49dafaea4acfb7df54caa 100644 (file)
@@ -41,7 +41,7 @@ type LinkContext interface {
        Title() string
 
        // The rendered (HTML) text.
-       Text() hstring.RenderedHTML
+       Text() hstring.HTML
 
        // The plain variant of Text.
        PlainText() string
@@ -100,7 +100,7 @@ type BlockquoteContext interface {
 
        // The blockquote text.
        // If type is "alert", this will be the alert text.
-       Text() hstring.RenderedHTML
+       Text() hstring.HTML
 
        /// Returns the blockquote type, one of "regular" and "alert".
        // Type "alert" indicates that this is a GitHub type alert.
@@ -166,7 +166,7 @@ type HeadingContext interface {
        // Anchor is the HTML id assigned to the heading.
        Anchor() string
        // Text is the rendered (HTML) heading text, excluding the heading marker.
-       Text() hstring.RenderedHTML
+       Text() hstring.HTML
        // PlainText is the unrendered version of Text.
        PlainText() string
 
@@ -213,7 +213,7 @@ const (
 type GetRendererFunc func(t RendererType, id any) any
 
 type TableCell struct {
-       Text      hstring.RenderedHTML
+       Text      hstring.HTML
        Alignment string // left, center, or right
 }
 
index f68cccd06e7104b7abb4a53c4861730d0bbc55c4..bf1e848b807ef055b454ded23190d15f590cba46 100644 (file)
@@ -95,7 +95,7 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
                BaseContext:      render.NewBaseContext(ctx, renderer, n, src, nil, ordinal),
                typ:              typ,
                alertType:        alertType,
-               text:             hstring.RenderedHTML(text),
+               text:             hstring.HTML(text),
                AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
        }
 
@@ -134,7 +134,7 @@ func (r *htmlRenderer) renderBlockquoteDefault(
 type blockquoteContext struct {
        hooks.BaseContext
 
-       text      hstring.RenderedHTML
+       text      hstring.HTML
        alertType string
        typ       string
 
@@ -149,7 +149,7 @@ func (c *blockquoteContext) AlertType() string {
        return c.alertType
 }
 
-func (c *blockquoteContext) Text() hstring.RenderedHTML {
+func (c *blockquoteContext) Text() hstring.HTML {
        return c.text
 }
 
index d72300434d9d2b4a7d758c19f1d92ccb55490a1b..bacb41a37fb6612fdfaf11d1cb54e958fccdef29 100644 (file)
@@ -52,7 +52,7 @@ type linkContext struct {
        pageInner   any
        destination string
        title       string
-       text        hstring.RenderedHTML
+       text        hstring.HTML
        plainText   string
        *attributes.AttributesHolder
 }
@@ -69,7 +69,7 @@ func (ctx linkContext) PageInner() any {
        return ctx.pageInner
 }
 
-func (ctx linkContext) Text() hstring.RenderedHTML {
+func (ctx linkContext) Text() hstring.HTML {
        return ctx.text
 }
 
@@ -100,7 +100,7 @@ type headingContext struct {
        pageInner any
        level     int
        anchor    string
-       text      hstring.RenderedHTML
+       text      hstring.HTML
        plainText string
        *attributes.AttributesHolder
 }
@@ -121,7 +121,7 @@ func (ctx headingContext) Anchor() string {
        return ctx.anchor
 }
 
-func (ctx headingContext) Text() hstring.RenderedHTML {
+func (ctx headingContext) Text() hstring.HTML {
        return ctx.text
 }
 
@@ -199,7 +199,7 @@ func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.N
                                pageInner:        pageInner,
                                destination:      string(n.Destination),
                                title:            string(n.Title),
-                               text:             hstring.RenderedHTML(text),
+                               text:             hstring.HTML(text),
                                plainText:        string(n.Text(source)),
                                AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
                        },
@@ -288,7 +288,7 @@ func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.No
                        pageInner:        pageInner,
                        destination:      string(n.Destination),
                        title:            string(n.Title),
-                       text:             hstring.RenderedHTML(text),
+                       text:             hstring.HTML(text),
                        plainText:        string(n.Text(source)),
                        AttributesHolder: attributes.Empty,
                },
@@ -355,7 +355,7 @@ func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node as
                        page:             page,
                        pageInner:        pageInner,
                        destination:      url,
-                       text:             hstring.RenderedHTML(label),
+                       text:             hstring.HTML(label),
                        plainText:        label,
                        AttributesHolder: attributes.Empty,
                },
@@ -442,7 +442,7 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
                        pageInner:        pageInner,
                        level:            n.Level,
                        anchor:           string(anchor),
-                       text:             hstring.RenderedHTML(text),
+                       text:             hstring.HTML(text),
                        plainText:        string(n.Text(source)),
                        AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
                },
index 943da974ea1e007ed70c262051b0d2bb867fa7a4..0aa6ee26947f6a6d8a4a02f5f2a042440ffccb4d 100644 (file)
@@ -132,7 +132,7 @@ func (r *htmlRenderer) renderCell(w util.BufWriter, source []byte, node ast.Node
                alignment = "left"
        }
 
-       cell := hooks.TableCell{Text: hstring.RenderedHTML(text), Alignment: alignment}
+       cell := hooks.TableCell{Text: hstring.HTML(text), Alignment: alignment}
 
        if node.Parent().Kind() == gast.KindTableHeader {
                table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell)