]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Fix it so embedded render-codeblock-goat is used even if custom render-codeblock...
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 12 Apr 2025 10:02:33 +0000 (12:02 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 12 Apr 2025 11:03:45 +0000 (13:03 +0200)
Fixes #13595

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

index f8cf02870903b6e2ed56715de5c98371bb146588..f93993092fd5984165fc27d238c8259b59ff576e 100644 (file)
@@ -208,12 +208,9 @@ func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, oth
                w.w1 += weightVariant1
        }
 
-       if other.Variant2 != "" && other.Variant2 == this.Variant2 {
+       if other.Variant1 != "" && other.Variant2 == this.Variant2 {
                w.w1 += weightVariant2
        }
-       if other.Variant2 != "" && this.Variant2 == "" {
-               w.w1--
-       }
 
        return w
 }
index 6ef03053178037850490059be68bad4d334454e1..e761e14d108f52cc1010a69c3dccc70f77fddbf8 100644 (file)
@@ -1832,7 +1832,10 @@ func (best *bestMatch) isBetter(w weight, ti *TemplInfo) bool {
                return false
        }
 
-       if best.w.w1 > 0 {
+       // Note that for render hook templates, we need to make
+       // the embedded render hook template wih if they're a better match,
+       // e.g. render-codeblock-goat.html.
+       if best.templ.category != CategoryMarkup && best.w.w1 > 0 {
                currentBestIsEmbedded := best.templ.subCategory == SubCategoryEmbedded
                if currentBestIsEmbedded {
                        if ti.subCategory != SubCategoryEmbedded {
index 618c173fc763a3500d29eec1de0ede08512cc4f3..db7cb5084c8d026d09842d9646ba8422dfacf84f 100644 (file)
@@ -827,7 +827,7 @@ func TestPartialHTML(t *testing.T) {
 }
 
 // Issue #13593.
-func TestNoGoat(t *testing.T) {
+func TestGoatAndNoGoat(t *testing.T) {
        t.Parallel()
 
        files := `
@@ -837,10 +837,20 @@ func TestNoGoat(t *testing.T) {
 title: "Home"
 ---
 
+
 §§§
 printf "Hello, world!"
 §§§
 
+
+§§§ goat
+.---.     .-.       .-.       .-.     .---.
+| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
+'---'     '-'       '+'       '+'     '---'
+§§§
+
+
+
 -- layouts/all.html --
 {{ .Content }}
 
@@ -848,7 +858,90 @@ printf "Hello, world!"
 
        b := hugolib.Test(t, files)
 
-       b.AssertFileContent("public/index.html", "Hello, world!")
+       // Basic code block.
+       b.AssertFileContent("public/index.html", "<code>printf &#34;Hello, world!&#34;\n</code>")
+
+       // Goat code block.
+       b.AssertFileContent("public/index.html", "Menlo,Lucida")
+}
+
+// Issue #13595.
+func TestGoatAndNoGoatCustomTemplate(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+-- content/_index.md --
+---
+title: "Home"
+---
+
+§§§
+printf "Hello, world!"
+§§§
+
+§§§ goat
+.---.     .-.       .-.       .-.     .---.
+| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
+'---'     '-'       '+'       '+'     '---'
+§§§
+
+
+
+-- layouts/_markup/render-codeblock.html --
+_markup/render-codeblock.html
+-- layouts/all.html --
+{{ .Content }}
+
+`
+
+       b := hugolib.Test(t, files)
+
+       // Basic code block.
+       b.AssertFileContent("public/index.html", "_markup/render-codeblock.html")
+
+       // Goat code block.
+       b.AssertFileContent("public/index.html", "Menlo,Lucida")
+}
+
+func TestGoatcustom(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+-- content/_index.md --
+---
+title: "Home"
+---
+
+§§§
+printf "Hello, world!"
+§§§
+
+§§§ goat
+.---.     .-.       .-.       .-.     .---.
+| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B |
+'---'     '-'       '+'       '+'     '---'
+§§§
+
+
+
+-- layouts/_markup/render-codeblock.html --
+_markup/render-codeblock.html
+-- layouts/_markup/render-codeblock-goat.html --
+_markup/render-codeblock-goat.html
+-- layouts/all.html --
+{{ .Content }}
+
+`
+
+       b := hugolib.Test(t, files)
+
+       // Basic code block.
+       b.AssertFileContent("public/index.html", "_markup/render-codeblock.html")
+
+       // Custom Goat code block.
+       b.AssertFileContent("public/index.html", "_markup/render-codeblock.html_markup/render-codeblock-goat.html")
 }
 
 // Issue #13515