]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Fix codeblock hook resolve issue
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 11 Apr 2025 16:37:25 +0000 (18:37 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 11 Apr 2025 16:59:42 +0000 (18:59 +0200)
Fixes #13593

tpl/tplimpl/templatedescriptor.go
tpl/tplimpl/templatedescriptor_test.go
tpl/tplimpl/templatestore.go
tpl/tplimpl/templatestore_integration_test.go

index fc757126f866b135ba09c5e6e46984f624edbb88..f8cf02870903b6e2ed56715de5c98371bb146588 100644 (file)
@@ -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 (
index 76201287d46fe278f4dcaba85e4f55f807f3401a..6fb8c219515d8871c9ac5c0d35b35d7cabe20e13 100644 (file)
@@ -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)
                }
        }
 }
index 8fe89586d02d5a080baed03fb1e2482371fbbc1d..6ef03053178037850490059be68bad4d334454e1 100644 (file)
@@ -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)
index 74abe58d0fdcf5a27e5fd87a69d82febe182f26b..618c173fc763a3500d29eec1de0ede08512cc4f3 100644 (file)
@@ -826,6 +826,31 @@ func TestPartialHTML(t *testing.T) {
        b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"/css/style.css\">")
 }
 
+// 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()