Repository: gohugoio/hugo (language: go)

Issue:
Codeblock render hook gets used when it shouldn't
As of v0.146.x, codeblocks without language specifiers get processed by the language-specific project-defined codeblock render hooks, but it shouldn't.

For example, if I create a codeblock render hook for the language `what`, and process the following markdown:

~~~markdown

```what
some content
```

```
note that there is no lang spec on this element
```
~~~

... as of Hugo 0.146.x, the second codeblock gets processed as if it had the `what` language specifier. Here's a diff of the output, generated from the attached minimal test project:

> <img width="609" alt="Image" src="https://github.com/user-attachments/assets/703130b7-93bc-478c-bc83-2fb0dd694674" />


📎 Minimal test project: [hugo-code-block-test.zip](https://github.com/user-attachments/files/19895267/hugo-code-block-test.zip)


Discussion / hints:

Fix patch:
diff --git a/tpl/tplimpl/templatedescriptor.go b/tpl/tplimpl/templatedescriptor.go
index f65ad394310..ea47afc88d3 100644
--- a/tpl/tplimpl/templatedescriptor.go
+++ b/tpl/tplimpl/templatedescriptor.go
@@ -130,17 +130,12 @@ func (this TemplateDescriptor) doCompare(category Category, isEmbedded bool, def
 
 	// One example of variant1 and 2 is for render codeblocks:
 	// variant1=codeblock, variant2=go (language).
-	if other.Variant1 != "" && other.Variant1 != this.Variant1 {
-		return w
-	}
-
-	if isEmbedded {
-		if other.Variant2 != "" && other.Variant2 != this.Variant2 {
+	if other.Variant1 != "" {
+		if other.Variant1 != this.Variant1 {
 			return w
 		}
-	} else {
-		// If both are set and different, no match.
-		if other.Variant2 != "" && this.Variant2 != "" && other.Variant2 != this.Variant2 {
+
+		if other.Variant2 != "" && other.Variant2 != this.Variant2 {
 			return w
 		}
 	}


Test patch:
diff --git a/tpl/tplimpl/templatestore_integration_test.go b/tpl/tplimpl/templatestore_integration_test.go
index bd00f82b7b2..638341581d9 100644
--- a/tpl/tplimpl/templatestore_integration_test.go
+++ b/tpl/tplimpl/templatestore_integration_test.go
@@ -1038,6 +1038,35 @@ _markup/render-codeblock-goat.html
 	b.AssertFileContent("public/index.html", "_markup/render-codeblock.html_markup/render-codeblock-goat.html")
 }
 
+func TestLookupCodeblockIssue13651(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+-- layouts/all.html --
+{{ .Content }}|
+-- layouts/_markup/render-codeblock-foo.html --
+render-codeblock-foo.html
+-- content/_index.md --
+---
+---
+
+§§§
+printf "Hello, world!"
+§§§
+
+§§§ foo
+printf "Hello, world again!"
+§§§
+`
+
+	b := hugolib.Test(t, files)
+
+	content := b.FileContent("public/index.html")
+	fooCount := strings.Count(content, "render-codeblock-foo.html")
+	b.Assert(fooCount, qt.Equals, 1)
+}
+
 // Issue #13515
 func TestPrintPathWarningOnDotRemoval(t *testing.T) {
 	t.Parallel()