]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix RenderShortcodes leaking context markers when indented
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 7 Apr 2026 19:02:27 +0000 (21:02 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 7 Apr 2026 20:05:42 +0000 (22:05 +0200)
Strip leading whitespace from Hugo context marker lines before
Goldmark parsing to prevent them from being treated as indented
code blocks.

Fixes #12457

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
hugolib/page__content.go
hugolib/rendershortcodes_test.go
markup/goldmark/hugocontext/hugocontext.go

index be1f5f396711f425e13ad274762ae99b1a44bbc4..c89d6b614408bc5052a0a3e7e86923c1f9521b59 100644 (file)
@@ -695,6 +695,8 @@ func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfCont
                        return nil, err
                }
 
+               ct.contentToRender = hugocontext.DedentMarkers(ct.contentToRender)
+
                if hasVariants {
                        p.incrPageOutputTemplateVariation()
                }
index 6ef7dfdeff9f86c406a42e7b2923d9126e4090e0..8fa0c24eb71f71ab304b9603b7c78929bb5177aa 100644 (file)
@@ -552,3 +552,32 @@ title: "includeme"
 
        b.AssertNoRenderShortcodesArtifacts()
 }
+
+// Issue 12457.
+func TestRenderShortcodesCodeBlock(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['section','rss','sitemap','taxonomy','term']
+-- layouts/_shortcodes/foo.html --
+{{ $p := site.GetPage "includeme" }}
+    {{ $p.RenderShortcodes }}
+-- layouts/home.html --
+{{ .Content }}
+-- content/_index.md --
+---
+title: home
+---
+{{% foo %}}
+-- content/includeme.md --
+---
+title: "includeme"
+---
+Some markdown.
+    `
+
+       b := Test(t, files)
+       b.AssertNoRenderShortcodesArtifacts()
+       b.AssertFileContentEquals("public/index.html", "<p>Some markdown.</p>\n")
+}
index 5f6b5eea52135922b4a2802631ddc7c2e67c8292..1ec188647edd7e3885e752afec57f7982a365a23 100644 (file)
@@ -102,8 +102,18 @@ var (
        hugoCtxEndDelim     = []byte("}}")
        hugoCtxClosingDelim = []byte("/}}")
        hugoCtxRe           = regexp.MustCompile(`{{__hugo_ctx( pid=\d+)?/?}}\n?`)
+       hugoCtxIndentedRe   = regexp.MustCompile(`(?m)^[ \t]+({{__hugo_ctx[^\n]*}})`)
 )
 
+// DedentMarkers removes leading whitespace from Hugo context marker lines
+// to prevent them from being treated as indented code blocks by Goldmark.
+func DedentMarkers(b []byte) []byte {
+       if !bytes.Contains(b, hugoCtxPrefix) {
+               return b
+       }
+       return hugoCtxIndentedRe.ReplaceAll(b, []byte("$1"))
+}
+
 // Strip strips any Hugo context markers from b.
 func Strip(b []byte) []byte {
        if !bytes.Contains(b, hugoCtxPrefix) {