]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/transform: Make Plainify and ToMath return template.HTML
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 11 Aug 2024 10:48:30 +0000 (12:48 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 11 Aug 2024 13:16:16 +0000 (15:16 +0200)
None of these are useful as plain strings in the templates, which forces the users to do `transform.Plainify "foo" | safeHTML`.

If people have trust issues with the output of these functions, they need to just stop using them.

Closes #8732

docs/content/en/functions/transform/Plainify.md
tpl/transform/transform.go
tpl/transform/transform_integration_test.go
tpl/transform/transform_test.go

index 040145170b894529fbc225c043206347f5004ec3..681d41f72e2e2d424ab1ecaa3cc1f06004f2399d 100644 (file)
@@ -6,7 +6,7 @@ keywords: []
 action:
   aliases: [plainify]
   related: []
-  returnType: string
+  returnType: template.HTML
   signatures: [transform.Plainify INPUT]
 aliases: [/functions/plainify]
 ---
index db7703b7fb5c68e2b7af2d98e4a2f4dfde129a5f..10a91671d54fdd688ca7143b22dede4c45572a0a 100644 (file)
@@ -54,7 +54,7 @@ func New(deps *deps.Deps) *Namespace {
                        "/tmpl/transform/unmarshal",
                        dynacache.OptionsPartition{Weight: 30, ClearWhen: dynacache.ClearOnChange},
                ),
-               cacheMath: dynacache.GetOrCreatePartition[string, string](
+               cacheMath: dynacache.GetOrCreatePartition[string, template.HTML](
                        deps.MemCache,
                        "/tmpl/transform/math",
                        dynacache.OptionsPartition{Weight: 30, ClearWhen: dynacache.ClearNever},
@@ -65,7 +65,7 @@ func New(deps *deps.Deps) *Namespace {
 // Namespace provides template functions for the "transform" namespace.
 type Namespace struct {
        cacheUnmarshal *dynacache.Partition[string, *resources.StaleValue[any]]
-       cacheMath      *dynacache.Partition[string, string]
+       cacheMath      *dynacache.Partition[string, template.HTML]
 
        id   atomic.Uint32
        deps *deps.Deps
@@ -188,18 +188,18 @@ func (ns *Namespace) Markdownify(ctx context.Context, s any) (template.HTML, err
 }
 
 // Plainify returns a copy of s with all HTML tags removed.
-func (ns *Namespace) Plainify(s any) (string, error) {
+func (ns *Namespace) Plainify(s any) (template.HTML, error) {
        ss, err := cast.ToStringE(s)
        if err != nil {
                return "", err
        }
 
-       return tpl.StripHTML(ss), nil
+       return template.HTML(tpl.StripHTML(ss)), nil
 }
 
 // ToMath converts a LaTeX string to math in the given format, default MathML.
 // This uses KaTeX to render the math, see https://katex.org/.
-func (ns *Namespace) ToMath(ctx context.Context, args ...any) (string, error) {
+func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, error) {
        if len(args) < 1 {
                return "", errors.New("must provide at least one argument")
        }
@@ -226,7 +226,7 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (string, error) {
        key := "tomath/" + s[:2] + "/" + s[2:]
        fileCache := ns.deps.ResourceSpec.FileCaches.MiscCache()
 
-       return ns.cacheMath.GetOrCreate(key, func(string) (string, error) {
+       return ns.cacheMath.GetOrCreate(key, func(string) (template.HTML, error) {
                _, r, err := fileCache.GetOrCreate(key, func() (io.ReadCloser, error) {
                        message := warpc.Message[warpc.KatexInput]{
                                Header: warpc.Header{
@@ -250,7 +250,9 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (string, error) {
                        return "", err
                }
 
-               return hugio.ReadString(r)
+               s, err := hugio.ReadString(r)
+
+               return template.HTML(s), err
        })
 }
 
index 529f18a5f17b1f0fcce239c9987a4132c867a903..b030417a234fdea54e3b3c42d424a8c7729ea84c 100644 (file)
@@ -141,8 +141,7 @@ func TestToMath(t *testing.T) {
 -- hugo.toml --
 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
 -- layouts/index.html --
-{{ $result := transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
-{{ printf "%v" $result | safeHTML }}
+{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
   `
        b := hugolib.Test(t, files)
 
index d645ca8e2ebae82e7db33bb97de6be78831035a3..1f6c99ec4f7966a9a9f73a29fcfa13d1f95630ba 100644 (file)
@@ -244,6 +244,6 @@ func TestPlainify(t *testing.T) {
                }
 
                b.Assert(err, qt.IsNil)
-               b.Assert(result, qt.Equals, test.expect)
+               b.Assert(result, qt.Equals, template.HTML(test.expect.(string)))
        }
 }