From d1c394442be0858b12fb1dbb42a98237e95c6d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 11 Apr 2025 18:37:25 +0200 Subject: [PATCH] tpl: Fix codeblock hook resolve issue Fixes #13593 --- tpl/tplimpl/templatedescriptor.go | 19 +++++++++----- tpl/tplimpl/templatedescriptor_test.go | 8 +++--- tpl/tplimpl/templatestore.go | 10 ++++---- tpl/tplimpl/templatestore_integration_test.go | 25 +++++++++++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/tpl/tplimpl/templatedescriptor.go b/tpl/tplimpl/templatedescriptor.go index fc757126f..f8cf02870 100644 --- a/tpl/tplimpl/templatedescriptor.go +++ b/tpl/tplimpl/templatedescriptor.go @@ -60,12 +60,13 @@ type descriptorHandler struct { // Note that this in this setup is usually a descriptor constructed from a page, // so we want to find the best match for that page. -func (s descriptorHandler) compareDescriptors(category Category, this, other TemplateDescriptor) weight { +func (s descriptorHandler) compareDescriptors(category Category, isEmbedded bool, this, other TemplateDescriptor) weight { if this.LayoutMustMatch && this.Layout != other.Layout { return weightNoMatch } - w := this.doCompare(category, other) + w := this.doCompare(category, isEmbedded, other) + if w.w1 <= 0 { if category == CategoryMarkup && (this.Variant1 == other.Variant1) && (this.Variant2 == other.Variant2 || this.Variant2 != "" && other.Variant2 == "") { // See issue 13242. @@ -82,7 +83,7 @@ func (s descriptorHandler) compareDescriptors(category Category, this, other Tem } //lint:ignore ST1006 this vs other makes it easier to reason about. -func (this TemplateDescriptor) doCompare(category Category, other TemplateDescriptor) weight { +func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, other TemplateDescriptor) weight { w := weightNoMatch // HTML in plain text is OK, but not the other way around. @@ -135,9 +136,15 @@ func (this TemplateDescriptor) doCompare(category Category, other TemplateDescri return w } - // If both are set and different, no match. - if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 { - return w + if isEmbedded { + if other.Variant2 != "" && other.Variant2 != this.Variant2 { + return w + } + } else { + // If both are set and different, no match. + if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 { + return w + } } const ( diff --git a/tpl/tplimpl/templatedescriptor_test.go b/tpl/tplimpl/templatedescriptor_test.go index 76201287d..6fb8c2195 100644 --- a/tpl/tplimpl/templatedescriptor_test.go +++ b/tpl/tplimpl/templatedescriptor_test.go @@ -20,14 +20,14 @@ func TestTemplateDescriptorCompare(t *testing.T) { less := func(category Category, this, other1, other2 TemplateDescriptor) { c.Helper() - result1 := dh.compareDescriptors(category, this, other1) - result2 := dh.compareDescriptors(category, this, other2) + result1 := dh.compareDescriptors(category, false, this, other1) + result2 := dh.compareDescriptors(category, false, this, other2) c.Assert(result1.w1 < result2.w1, qt.IsTrue, qt.Commentf("%d < %d", result1, result2)) } check := func(category Category, this, other TemplateDescriptor, less bool) { c.Helper() - result := dh.compareDescriptors(category, this, other) + result := dh.compareDescriptors(category, false, this, other) if less { c.Assert(result.w1 < 0, qt.IsTrue, qt.Commentf("%d", result)) } else { @@ -98,7 +98,7 @@ func BenchmarkCompareDescriptors(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { for _, pair := range pairs { - _ = dh.compareDescriptors(CategoryLayout, pair.d1, pair.d2) + _ = dh.compareDescriptors(CategoryLayout, false, pair.d1, pair.d2) } } } diff --git a/tpl/tplimpl/templatestore.go b/tpl/tplimpl/templatestore.go index 8fe89586d..6ef030531 100644 --- a/tpl/tplimpl/templatestore.go +++ b/tpl/tplimpl/templatestore.go @@ -311,7 +311,7 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor distance := slashCountK1 - slashCountK2 for d2, vv := range v { - weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2) + weight := s.dh.compareDescriptors(CategoryBaseof, false, d1, d2) weight.distance = distance if best.isBetter(weight, vv.Template) { best.updateValues(weight, k2, d2, vv.Template) @@ -447,7 +447,7 @@ func (s *TemplateStore) FindAllBaseTemplateCandidates(overlayKey string, desc Te continue } - if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, descBaseof, vv.D).w1 > 0 { + if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, false, descBaseof, vv.D).w1 > 0 { result = append(result, keyTemplateInfo{Key: k, Info: vv}) } } @@ -584,7 +584,7 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo { continue } - weight := s.dh.compareDescriptors(q.Category, q.Desc, k) + weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k) weight.distance = distance if best.isBetter(weight, vv) { best.updateValues(weight, k2, k, vv) @@ -737,7 +737,7 @@ func (s *TemplateStore) findBestMatchGet(key string, category Category, consider continue } - weight := s.dh.compareDescriptors(category, desc, k.d) + weight := s.dh.compareDescriptors(category, vv.subCategory == SubCategoryEmbedded, desc, k.d) if best.isBetter(weight, vv) { best.updateValues(weight, key, k.d, vv) } @@ -758,7 +758,7 @@ func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashC continue } - weight := s.dh.compareDescriptors(q.Category, q.Desc, k.d) + weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k.d) weight.distance = distance isBetter := best.isBetter(weight, vv) diff --git a/tpl/tplimpl/templatestore_integration_test.go b/tpl/tplimpl/templatestore_integration_test.go index 74abe58d0..618c173fc 100644 --- a/tpl/tplimpl/templatestore_integration_test.go +++ b/tpl/tplimpl/templatestore_integration_test.go @@ -826,6 +826,31 @@ func TestPartialHTML(t *testing.T) { b.AssertFileContent("public/index.html", "") } +// Issue #13593. +func TestNoGoat(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +-- content/_index.md -- +--- +title: "Home" +--- + +§§§ +printf "Hello, world!" +§§§ + +-- layouts/all.html -- +{{ .Content }} + +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.html", "Hello, world!") +} + // Issue #13515 func TestPrintPathWarningOnDotRemoval(t *testing.T) { t.Parallel()