Skip to content

Commit c871062

Browse files
committed
tpl: Fix it so embedded render-codeblock-goat is used even if custom render-codeblock exists
Fixes #13595
1 parent 53221f8 commit c871062

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

Diff for: tpl/tplimpl/templatedescriptor.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,9 @@ func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, oth
208208
w.w1 += weightVariant1
209209
}
210210

211-
if other.Variant2 != "" && other.Variant2 == this.Variant2 {
211+
if other.Variant1 != "" && other.Variant2 == this.Variant2 {
212212
w.w1 += weightVariant2
213213
}
214-
if other.Variant2 != "" && this.Variant2 == "" {
215-
w.w1--
216-
}
217214

218215
return w
219216
}

Diff for: tpl/tplimpl/templatestore.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,10 @@ func (best *bestMatch) isBetter(w weight, ti *TemplInfo) bool {
18321832
return false
18331833
}
18341834

1835-
if best.w.w1 > 0 {
1835+
// Note that for render hook templates, we need to make
1836+
// the embedded render hook template wih if they're a better match,
1837+
// e.g. render-codeblock-goat.html.
1838+
if best.templ.category != CategoryMarkup && best.w.w1 > 0 {
18361839
currentBestIsEmbedded := best.templ.subCategory == SubCategoryEmbedded
18371840
if currentBestIsEmbedded {
18381841
if ti.subCategory != SubCategoryEmbedded {

Diff for: tpl/tplimpl/templatestore_integration_test.go

+95-2
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ func TestPartialHTML(t *testing.T) {
827827
}
828828

829829
// Issue #13593.
830-
func TestNoGoat(t *testing.T) {
830+
func TestGoatAndNoGoat(t *testing.T) {
831831
t.Parallel()
832832

833833
files := `
@@ -837,18 +837,111 @@ func TestNoGoat(t *testing.T) {
837837
title: "Home"
838838
---
839839
840+
840841
§§§
841842
printf "Hello, world!"
842843
§§§
843844
845+
846+
§§§ goat
847+
.---. .-. .-. .-. .---.
848+
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
849+
'---' '-' '+' '+' '---'
850+
§§§
851+
852+
853+
844854
-- layouts/all.html --
845855
{{ .Content }}
846856
847857
`
848858

849859
b := hugolib.Test(t, files)
850860

851-
b.AssertFileContent("public/index.html", "Hello, world!")
861+
// Basic code block.
862+
b.AssertFileContent("public/index.html", "<code>printf &#34;Hello, world!&#34;\n</code>")
863+
864+
// Goat code block.
865+
b.AssertFileContent("public/index.html", "Menlo,Lucida")
866+
}
867+
868+
// Issue #13595.
869+
func TestGoatAndNoGoatCustomTemplate(t *testing.T) {
870+
t.Parallel()
871+
872+
files := `
873+
-- hugo.toml --
874+
-- content/_index.md --
875+
---
876+
title: "Home"
877+
---
878+
879+
§§§
880+
printf "Hello, world!"
881+
§§§
882+
883+
§§§ goat
884+
.---. .-. .-. .-. .---.
885+
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
886+
'---' '-' '+' '+' '---'
887+
§§§
888+
889+
890+
891+
-- layouts/_markup/render-codeblock.html --
892+
_markup/render-codeblock.html
893+
-- layouts/all.html --
894+
{{ .Content }}
895+
896+
`
897+
898+
b := hugolib.Test(t, files)
899+
900+
// Basic code block.
901+
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html")
902+
903+
// Goat code block.
904+
b.AssertFileContent("public/index.html", "Menlo,Lucida")
905+
}
906+
907+
func TestGoatcustom(t *testing.T) {
908+
t.Parallel()
909+
910+
files := `
911+
-- hugo.toml --
912+
-- content/_index.md --
913+
---
914+
title: "Home"
915+
---
916+
917+
§§§
918+
printf "Hello, world!"
919+
§§§
920+
921+
§§§ goat
922+
.---. .-. .-. .-. .---.
923+
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
924+
'---' '-' '+' '+' '---'
925+
§§§
926+
927+
928+
929+
-- layouts/_markup/render-codeblock.html --
930+
_markup/render-codeblock.html
931+
-- layouts/_markup/render-codeblock-goat.html --
932+
_markup/render-codeblock-goat.html
933+
-- layouts/all.html --
934+
{{ .Content }}
935+
936+
`
937+
938+
b := hugolib.Test(t, files)
939+
940+
// Basic code block.
941+
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html")
942+
943+
// Custom Goat code block.
944+
b.AssertFileContent("public/index.html", "_markup/render-codeblock.html_markup/render-codeblock-goat.html")
852945
}
853946

854947
// Issue #13515

0 commit comments

Comments
 (0)