From: Bjørn Erik Pedersen Date: Tue, 7 Apr 2026 19:02:27 +0000 (+0200) Subject: Fix RenderShortcodes leaking context markers when indented X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=161d0d475773c5251ec00cc8848f1a174393155c;p=brevno-suite%2Fhugo 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) --- diff --git a/hugolib/page__content.go b/hugolib/page__content.go index be1f5f396..c89d6b614 100644 --- a/hugolib/page__content.go +++ b/hugolib/page__content.go @@ -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() } diff --git a/hugolib/rendershortcodes_test.go b/hugolib/rendershortcodes_test.go index 6ef7dfdef..8fa0c24eb 100644 --- a/hugolib/rendershortcodes_test.go +++ b/hugolib/rendershortcodes_test.go @@ -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", "

Some markdown.

\n") +} diff --git a/markup/goldmark/hugocontext/hugocontext.go b/markup/goldmark/hugocontext/hugocontext.go index 5f6b5eea5..1ec188647 100644 --- a/markup/goldmark/hugocontext/hugocontext.go +++ b/markup/goldmark/hugocontext/hugocontext.go @@ -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) {