Rename hstring.RenderedHTML => hstring.HTML

And add a comment about why it exists.
This commit is contained in:
Bjørn Erik Pedersen
2024-09-01 10:03:10 +02:00
parent 96afea4acc
commit 469124823c
7 changed files with 27 additions and 25 deletions

View 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)
}

View 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"))
}