]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix regression with hyphenated codeblock templates, e.g. render-codeblock-go-html...
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 27 Jul 2025 10:02:21 +0000 (12:02 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 27 Jul 2025 12:39:39 +0000 (14:39 +0200)
Fixes #13864

Co-authored-by: Joe Mooring <joe.mooring@veriphor.com>
tpl/tplimpl/templatestore.go
tpl/tplimpl/templatestore_integration_test.go

index 008b743c7e3a0fa6088a4246071e7be3805a4cb6..caf53c0055b0665175a2c81e0060040b80921b3d 100644 (file)
@@ -1762,15 +1762,18 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin
        if category == CategoryMarkup {
                // We store all template nodes for a given directory on the same level.
                k1 = strings.TrimSuffix(k1, "/_markup")
-               parts := strings.Split(d.LayoutFromTemplate, "-")
-               if len(parts) < 2 {
+               v, found := strings.CutPrefix(d.LayoutFromTemplate, "render-")
+               if !found {
                        return "", "", 0, TemplateDescriptor{}, fmt.Errorf("unrecognized render hook template")
                }
-               // Either 2 or 3 parts, e.g. render-codeblock-go.
-               d.Variant1 = parts[1]
-               if len(parts) > 2 {
-                       d.Variant2 = parts[2]
+               hyphenIdx := strings.Index(v, "-")
+
+               d.Variant1 = v
+               if hyphenIdx > 0 {
+                       d.Variant1 = v[:hyphenIdx]
+                       d.Variant2 = v[hyphenIdx+1:]
                }
+
                d.LayoutFromTemplate = "" // This allows using page layout as part of the key for lookups.
        }
 
index 9da95935013a528311b8c9a961a87ef9e42defc3..13c4d5af9e380d7e446b9630e0d10c8d957317a9 100644 (file)
@@ -393,6 +393,61 @@ Link: [Foo](/foo)
        }
 }
 
+func TestCodeblockIssue13864(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+-- content/_index.md --
+---
+title: home
+---
+
+~~~
+LANG: none
+~~~
+
+~~~go
+LANG: go
+~~~
+
+~~~go-html-template
+LANG: go-html-template
+~~~
+
+~~~xy
+LANG: xy
+~~~
+
+~~~x-y
+LANG: x-y
+~~~
+-- layouts/home.html --
+{{ .Content }}
+-- layouts/_markup/render-codeblock.html --
+{{ .Inner }} LAYOUT: render-codeblock.html|
+-- layouts/_markup/render-codeblock-go.html --
+{{ .Inner }} LAYOUT: render-codeblock-go.html|
+-- layouts/_markup/render-codeblock-go-html-template.html --
+{{ .Inner }} LAYOUT: render-codeblock-go-html-template.html|
+-- layouts/_markup/render-codeblock-xy.html --
+{{ .Inner }} LAYOUT: render-codeblock-xy.html|
+-- layouts/_markup/render-codeblock-x-y.html.html --
+{{ .Inner }} LAYOUT: render-codeblock-x-y.html|
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/index.html",
+               "LANG: none LAYOUT: render-codeblock.html|",                              // pass
+               "LANG: go LAYOUT: render-codeblock-go.html|",                             // fail: uses render-codeblock-go-html-template.html
+               "LANG: go-html-template LAYOUT: render-codeblock-go-html-template.html|", // fail: uses render-codeblock.html
+               "LANG: xy LAYOUT: render-codeblock-xy.html|",                             // pass
+               "LANG: x-y LAYOUT: render-codeblock-x-y.html|",                           // fail: uses render-codeblock.html
+       )
+}
+
 func TestRenderCodeblockSpecificity(t *testing.T) {
        files := `
 -- hugo.toml --