tpl: Fix overlapping layout sections

Fixes #13672
This commit is contained in:
Bjørn Erik Pedersen
2025-04-30 19:49:42 +02:00
parent a1cb15e1cf
commit be93d5218b
2 changed files with 32 additions and 0 deletions

View File

@@ -323,6 +323,9 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor
} }
ti.baseVariants.WalkPath(k1, func(k2 string, v map[TemplateDescriptor]*TemplWithBaseApplied) (bool, error) { ti.baseVariants.WalkPath(k1, func(k2 string, v map[TemplateDescriptor]*TemplWithBaseApplied) (bool, error) {
if !s.inPath(k1, k2) {
return false, nil
}
slashCountK2 := strings.Count(k2, "/") slashCountK2 := strings.Count(k2, "/")
distance := slashCountK1 - slashCountK2 distance := slashCountK1 - slashCountK2
@@ -615,6 +618,9 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
defer s.putBest(best) defer s.putBest(best)
s.treeShortcodes.WalkPath(k1, func(k2 string, m map[string]map[TemplateDescriptor]*TemplInfo) (bool, error) { s.treeShortcodes.WalkPath(k1, func(k2 string, m map[string]map[TemplateDescriptor]*TemplInfo) (bool, error) {
if !s.inPath(k1, k2) {
return false, nil
}
slashCountK2 := strings.Count(k2, "/") slashCountK2 := strings.Count(k2, "/")
distance := slashCountK1 - slashCountK2 distance := slashCountK1 - slashCountK2
@@ -780,8 +786,18 @@ func (s *TemplateStore) findBestMatchGet(key string, category Category, consider
} }
} }
func (s *TemplateStore) inPath(k1, k2 string) bool {
if k1 != k2 && !strings.HasPrefix(k1, k2+"/") {
return false
}
return true
}
func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashCountK1 int, best *bestMatch) { func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashCountK1 int, best *bestMatch) {
s.treeMain.WalkPath(k1, func(k2 string, v map[nodeKey]*TemplInfo) (bool, error) { s.treeMain.WalkPath(k1, func(k2 string, v map[nodeKey]*TemplInfo) (bool, error) {
if !s.inPath(k1, k2) {
return false, nil
}
slashCountK2 := strings.Count(k2, "/") slashCountK2 := strings.Count(k2, "/")
distance := slashCountK1 - slashCountK2 distance := slashCountK1 - slashCountK2

View File

@@ -1396,3 +1396,19 @@ layouts/taxononmy.html.html
b.AssertFileExists("public/p1/index.html", false) b.AssertFileExists("public/p1/index.html", false)
} }
} }
func TestTemplateLoopBlogVsBlogrollIssue13672(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- layouts/blog/all.html --
blog/all.html
-- layouts/blogroll/all.html --
blogroll/all.html
-- content/blogroll/p1.md --
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/blogroll/p1/index.html", "blogroll/all.html")
}