Skip to content

Commit 9c57af1

Browse files
bepjmooring
andcommitted
Fix regression with hyphenated codeblock templates, e.g. render-codeblock-go-html-template.html
Fixes #13864 Co-authored-by: Joe Mooring <[email protected]>
1 parent d240a70 commit 9c57af1

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

tpl/tplimpl/templatestore.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,15 +1762,18 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin
17621762
if category == CategoryMarkup {
17631763
// We store all template nodes for a given directory on the same level.
17641764
k1 = strings.TrimSuffix(k1, "/_markup")
1765-
parts := strings.Split(d.LayoutFromTemplate, "-")
1766-
if len(parts) < 2 {
1765+
v, found := strings.CutPrefix(d.LayoutFromTemplate, "render-")
1766+
if !found {
17671767
return "", "", 0, TemplateDescriptor{}, fmt.Errorf("unrecognized render hook template")
17681768
}
1769-
// Either 2 or 3 parts, e.g. render-codeblock-go.
1770-
d.Variant1 = parts[1]
1771-
if len(parts) > 2 {
1772-
d.Variant2 = parts[2]
1769+
hyphenIdx := strings.Index(v, "-")
1770+
1771+
d.Variant1 = v
1772+
if hyphenIdx > 0 {
1773+
d.Variant1 = v[:hyphenIdx]
1774+
d.Variant2 = v[hyphenIdx+1:]
17731775
}
1776+
17741777
d.LayoutFromTemplate = "" // This allows using page layout as part of the key for lookups.
17751778
}
17761779

tpl/tplimpl/templatestore_integration_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,61 @@ Link: [Foo](/foo)
393393
}
394394
}
395395

396+
func TestCodeblockIssue13864(t *testing.T) {
397+
t.Parallel()
398+
399+
files := `
400+
-- hugo.toml --
401+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
402+
-- content/_index.md --
403+
---
404+
title: home
405+
---
406+
407+
~~~
408+
LANG: none
409+
~~~
410+
411+
~~~go
412+
LANG: go
413+
~~~
414+
415+
~~~go-html-template
416+
LANG: go-html-template
417+
~~~
418+
419+
~~~xy
420+
LANG: xy
421+
~~~
422+
423+
~~~x-y
424+
LANG: x-y
425+
~~~
426+
-- layouts/home.html --
427+
{{ .Content }}
428+
-- layouts/_markup/render-codeblock.html --
429+
{{ .Inner }} LAYOUT: render-codeblock.html|
430+
-- layouts/_markup/render-codeblock-go.html --
431+
{{ .Inner }} LAYOUT: render-codeblock-go.html|
432+
-- layouts/_markup/render-codeblock-go-html-template.html --
433+
{{ .Inner }} LAYOUT: render-codeblock-go-html-template.html|
434+
-- layouts/_markup/render-codeblock-xy.html --
435+
{{ .Inner }} LAYOUT: render-codeblock-xy.html|
436+
-- layouts/_markup/render-codeblock-x-y.html.html --
437+
{{ .Inner }} LAYOUT: render-codeblock-x-y.html|
438+
`
439+
440+
b := hugolib.Test(t, files)
441+
442+
b.AssertFileContent("public/index.html",
443+
"LANG: none LAYOUT: render-codeblock.html|", // pass
444+
"LANG: go LAYOUT: render-codeblock-go.html|", // fail: uses render-codeblock-go-html-template.html
445+
"LANG: go-html-template LAYOUT: render-codeblock-go-html-template.html|", // fail: uses render-codeblock.html
446+
"LANG: xy LAYOUT: render-codeblock-xy.html|", // pass
447+
"LANG: x-y LAYOUT: render-codeblock-x-y.html|", // fail: uses render-codeblock.html
448+
)
449+
}
450+
396451
func TestRenderCodeblockSpecificity(t *testing.T) {
397452
files := `
398453
-- hugo.toml --

0 commit comments

Comments
 (0)