Skip to content

Commit d1c3944

Browse files
committed
tpl: Fix codeblock hook resolve issue
Fixes #13593
1 parent 1074e01 commit d1c3944

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

tpl/tplimpl/templatedescriptor.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ type descriptorHandler struct {
6060

6161
// Note that this in this setup is usually a descriptor constructed from a page,
6262
// so we want to find the best match for that page.
63-
func (s descriptorHandler) compareDescriptors(category Category, this, other TemplateDescriptor) weight {
63+
func (s descriptorHandler) compareDescriptors(category Category, isEmbedded bool, this, other TemplateDescriptor) weight {
6464
if this.LayoutMustMatch && this.Layout != other.Layout {
6565
return weightNoMatch
6666
}
6767

68-
w := this.doCompare(category, other)
68+
w := this.doCompare(category, isEmbedded, other)
69+
6970
if w.w1 <= 0 {
7071
if category == CategoryMarkup && (this.Variant1 == other.Variant1) && (this.Variant2 == other.Variant2 || this.Variant2 != "" && other.Variant2 == "") {
7172
// See issue 13242.
@@ -82,7 +83,7 @@ func (s descriptorHandler) compareDescriptors(category Category, this, other Tem
8283
}
8384

8485
//lint:ignore ST1006 this vs other makes it easier to reason about.
85-
func (this TemplateDescriptor) doCompare(category Category, other TemplateDescriptor) weight {
86+
func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, other TemplateDescriptor) weight {
8687
w := weightNoMatch
8788

8889
// HTML in plain text is OK, but not the other way around.
@@ -135,9 +136,15 @@ func (this TemplateDescriptor) doCompare(category Category, other TemplateDescri
135136
return w
136137
}
137138

138-
// If both are set and different, no match.
139-
if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 {
140-
return w
139+
if isEmbedded {
140+
if other.Variant2 != "" && other.Variant2 != this.Variant2 {
141+
return w
142+
}
143+
} else {
144+
// If both are set and different, no match.
145+
if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 {
146+
return w
147+
}
141148
}
142149

143150
const (

tpl/tplimpl/templatedescriptor_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func TestTemplateDescriptorCompare(t *testing.T) {
2020

2121
less := func(category Category, this, other1, other2 TemplateDescriptor) {
2222
c.Helper()
23-
result1 := dh.compareDescriptors(category, this, other1)
24-
result2 := dh.compareDescriptors(category, this, other2)
23+
result1 := dh.compareDescriptors(category, false, this, other1)
24+
result2 := dh.compareDescriptors(category, false, this, other2)
2525
c.Assert(result1.w1 < result2.w1, qt.IsTrue, qt.Commentf("%d < %d", result1, result2))
2626
}
2727

2828
check := func(category Category, this, other TemplateDescriptor, less bool) {
2929
c.Helper()
30-
result := dh.compareDescriptors(category, this, other)
30+
result := dh.compareDescriptors(category, false, this, other)
3131
if less {
3232
c.Assert(result.w1 < 0, qt.IsTrue, qt.Commentf("%d", result))
3333
} else {
@@ -98,7 +98,7 @@ func BenchmarkCompareDescriptors(b *testing.B) {
9898
b.ResetTimer()
9999
for i := 0; i < b.N; i++ {
100100
for _, pair := range pairs {
101-
_ = dh.compareDescriptors(CategoryLayout, pair.d1, pair.d2)
101+
_ = dh.compareDescriptors(CategoryLayout, false, pair.d1, pair.d2)
102102
}
103103
}
104104
}

tpl/tplimpl/templatestore.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor
311311
distance := slashCountK1 - slashCountK2
312312

313313
for d2, vv := range v {
314-
weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2)
314+
weight := s.dh.compareDescriptors(CategoryBaseof, false, d1, d2)
315315
weight.distance = distance
316316
if best.isBetter(weight, vv.Template) {
317317
best.updateValues(weight, k2, d2, vv.Template)
@@ -447,7 +447,7 @@ func (s *TemplateStore) FindAllBaseTemplateCandidates(overlayKey string, desc Te
447447
continue
448448
}
449449

450-
if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, descBaseof, vv.D).w1 > 0 {
450+
if vv.D.isKindInLayout(desc.Layout) && s.dh.compareDescriptors(CategoryBaseof, false, descBaseof, vv.D).w1 > 0 {
451451
result = append(result, keyTemplateInfo{Key: k, Info: vv})
452452
}
453453
}
@@ -584,7 +584,7 @@ func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
584584
continue
585585
}
586586

587-
weight := s.dh.compareDescriptors(q.Category, q.Desc, k)
587+
weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k)
588588
weight.distance = distance
589589
if best.isBetter(weight, vv) {
590590
best.updateValues(weight, k2, k, vv)
@@ -737,7 +737,7 @@ func (s *TemplateStore) findBestMatchGet(key string, category Category, consider
737737
continue
738738
}
739739

740-
weight := s.dh.compareDescriptors(category, desc, k.d)
740+
weight := s.dh.compareDescriptors(category, vv.subCategory == SubCategoryEmbedded, desc, k.d)
741741
if best.isBetter(weight, vv) {
742742
best.updateValues(weight, key, k.d, vv)
743743
}
@@ -758,7 +758,7 @@ func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashC
758758
continue
759759
}
760760

761-
weight := s.dh.compareDescriptors(q.Category, q.Desc, k.d)
761+
weight := s.dh.compareDescriptors(q.Category, vv.subCategory == SubCategoryEmbedded, q.Desc, k.d)
762762

763763
weight.distance = distance
764764
isBetter := best.isBetter(weight, vv)

tpl/tplimpl/templatestore_integration_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,31 @@ func TestPartialHTML(t *testing.T) {
826826
b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"/css/style.css\">")
827827
}
828828

829+
// Issue #13593.
830+
func TestNoGoat(t *testing.T) {
831+
t.Parallel()
832+
833+
files := `
834+
-- hugo.toml --
835+
-- content/_index.md --
836+
---
837+
title: "Home"
838+
---
839+
840+
§§§
841+
printf "Hello, world!"
842+
§§§
843+
844+
-- layouts/all.html --
845+
{{ .Content }}
846+
847+
`
848+
849+
b := hugolib.Test(t, files)
850+
851+
b.AssertFileContent("public/index.html", "Hello, world!")
852+
}
853+
829854
// Issue #13515
830855
func TestPrintPathWarningOnDotRemoval(t *testing.T) {
831856
t.Parallel()

0 commit comments

Comments
 (0)