From 6d97ee711e834ad4a0d2aca44fafb042008a8429 Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Sat, 31 Aug 2024 17:25:15 +0200 Subject: Make all renderhook Text methods return template.HTML --- common/types/hstring/stringtypes.go | 18 ++++++++++++++++-- common/types/hstring/stringtypes_test.go | 4 ++-- 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/types/hstring/stringtypes.go b/common/types/hstring/stringtypes.go index 5e8e3a23d..05977ddce 100644 --- a/common/types/hstring/stringtypes.go +++ b/common/types/hstring/stringtypes.go @@ -13,8 +13,22 @@ package hstring -type RenderedString string +import ( + "html/template" -func (s RenderedString) String() string { + "github.com/gohugoio/hugo/common/types" +) + +var _ types.PrintableValueProvider = RenderedHTML("") + +// 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 + +func (s RenderedHTML) String() string { return string(s) } + +func (s RenderedHTML) PrintableValue() any { + return template.HTML(s) +} diff --git a/common/types/hstring/stringtypes_test.go b/common/types/hstring/stringtypes_test.go index 2f1f865c8..75b7af13c 100644 --- a/common/types/hstring/stringtypes_test.go +++ b/common/types/hstring/stringtypes_test.go @@ -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(RenderedString("Hugo")), qt.Equals, "Hugo") - c.Assert(template.HTML(RenderedString("Hugo")), qt.Equals, template.HTML("Hugo")) + c.Assert(cast.ToString(RenderedHTML("Hugo")), qt.Equals, "Hugo") + c.Assert(template.HTML(RenderedHTML("Hugo")), qt.Equals, template.HTML("Hugo")) } -- cgit v1.2.3