]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix shortcode detection in RenderString
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 26 Jan 2023 09:30:25 +0000 (10:30 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 26 Jan 2023 10:41:07 +0000 (11:41 +0100)
Fixes #10654

hugolib/page__per_output.go
hugolib/renderstring_test.go
parser/pageparser/pageparser.go
parser/pageparser/pageparser_test.go

index 60e3a7f59844d6ecbf2244853577c64f2ee1c0f9..97e9cc46564d129b00e487ac579f7e12cca7f6a8 100644 (file)
@@ -384,8 +384,8 @@ func (p *pageContentOutput) RenderString(args ...any) (template.HTML, error) {
 
        var rendered []byte
 
-       if strings.Contains(contentToRender, "{{") {
-               // Probably a shortcode.
+       if pageparser.HasShortcode(contentToRender) {
+               // String contains a shortcode.
                parsed, err := pageparser.ParseMain(strings.NewReader(contentToRender), pageparser.Config{})
                if err != nil {
                        return "", err
index 1be0cdffb53dafaff46c5297aa31c1d3b9ed6f79..af66156e6926fd137402efb27ae9e0072db54b79 100644 (file)
@@ -190,3 +190,39 @@ Has other: false
 `)
 
 }
+
+func TestRenderStringWithShortcodeIssue10654(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- config.toml --
+timeout = '300ms'
+-- content/p1.md --
+---
+title: "P1"
+---
+{{< toc >}}
+
+## Heading 1
+
+{{< noop >}}
+     {{ not a shortcode
+{{< /noop >}}
+}
+-- layouts/shortcodes/noop.html --
+{{ .Inner | $.Page.RenderString }}
+-- layouts/shortcodes/toc.html --
+{{ .Page.TableOfContents }}
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+       b := NewIntegrationTestBuilder(
+               IntegrationTestConfig{
+                       T:           t,
+                       TxtarString: files,
+               },
+       ).Build()
+
+       b.AssertFileContent("public/p1/index.html", `TableOfContents`)
+}
index 0a9fc61af418745948fdec722b106dec961cf2e9..7687479078ca591dc41f595476063c302a4ebd70 100644 (file)
@@ -19,6 +19,8 @@ import (
        "fmt"
        "io"
        "io/ioutil"
+       "regexp"
+       "strings"
 
        "github.com/gohugoio/hugo/parser/metadecoders"
 )
@@ -234,3 +236,14 @@ func IsProbablySourceOfItems(source []byte, items Items) bool {
 
        return true
 }
+
+var hasShortcodeRe = regexp.MustCompile(`{{[%,<][^\/]`)
+
+// HasShortcode returns true if the given string contains a shortcode.
+func HasShortcode(s string) bool {
+       // Fast path for the common case.
+       if !strings.Contains(s, "{{") {
+               return false
+       }
+       return hasShortcodeRe.MatchString(s)
+}
index a21b97970cf7266ea7c7f746701e31c9a60d7cc3..de817d1fb6ef55a3731b6f70475789d7b5870c0d 100644 (file)
@@ -101,3 +101,30 @@ func TestIsProbablyItemsSource(t *testing.T) {
        c.Assert(IsProbablySourceOfItems([]byte(`{{< foo >}}  `), items), qt.IsFalse)
        c.Assert(IsProbablySourceOfItems([]byte(``), items), qt.IsFalse)
 }
+
+func TestHasShortcode(t *testing.T) {
+       c := qt.New(t)
+
+       c.Assert(HasShortcode("{{< foo >}}"), qt.IsTrue)
+       c.Assert(HasShortcode("aSDasd  SDasd aSD\n\nasdfadf{{% foo %}}\nasdf"), qt.IsTrue)
+       c.Assert(HasShortcode("{{</* foo */>}}"), qt.IsFalse)
+       c.Assert(HasShortcode("{{%/* foo */%}}"), qt.IsFalse)
+
+}
+
+func BenchmarkHasShortcode(b *testing.B) {
+       withShortcode := strings.Repeat("this is text", 30) + "{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}" + strings.Repeat("this is text", 30)
+       withoutShortcode := strings.Repeat("this is text", 30) + "This is some inner content." + strings.Repeat("this is text", 30)
+       b.Run("Match", func(b *testing.B) {
+               for i := 0; i < b.N; i++ {
+                       HasShortcode(withShortcode)
+               }
+       })
+
+       b.Run("NoMatch", func(b *testing.B) {
+               for i := 0; i < b.N; i++ {
+                       HasShortcode(withoutShortcode)
+               }
+       })
+
+}