Fix RenderShortcodes leaking context markers when indented

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>
This commit is contained in:
Bjørn Erik Pedersen
2026-04-07 21:02:27 +02:00
parent 45e4596630
commit 161d0d4757
3 changed files with 41 additions and 0 deletions
+2
View 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()
}
+29
View 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")
}
@@ -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) {