Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 161d0d4

Browse files
bepclaude
andcommitted
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) <[email protected]>
1 parent 45e4596 commit 161d0d4

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

hugolib/page__content.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfCont
695695
return nil, err
696696
}
697697

698+
ct.contentToRender = hugocontext.DedentMarkers(ct.contentToRender)
699+
698700
if hasVariants {
699701
p.incrPageOutputTemplateVariation()
700702
}

hugolib/rendershortcodes_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,32 @@ title: "includeme"
552552

553553
b.AssertNoRenderShortcodesArtifacts()
554554
}
555+
556+
// Issue 12457.
557+
func TestRenderShortcodesCodeBlock(t *testing.T) {
558+
t.Parallel()
559+
560+
files := `
561+
-- hugo.toml --
562+
disableKinds = ['section','rss','sitemap','taxonomy','term']
563+
-- layouts/_shortcodes/foo.html --
564+
{{ $p := site.GetPage "includeme" }}
565+
{{ $p.RenderShortcodes }}
566+
-- layouts/home.html --
567+
{{ .Content }}
568+
-- content/_index.md --
569+
---
570+
title: home
571+
---
572+
{{% foo %}}
573+
-- content/includeme.md --
574+
---
575+
title: "includeme"
576+
---
577+
Some markdown.
578+
`
579+
580+
b := Test(t, files)
581+
b.AssertNoRenderShortcodesArtifacts()
582+
b.AssertFileContentEquals("public/index.html", "<p>Some markdown.</p>\n")
583+
}

markup/goldmark/hugocontext/hugocontext.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,18 @@ var (
102102
hugoCtxEndDelim = []byte("}}")
103103
hugoCtxClosingDelim = []byte("/}}")
104104
hugoCtxRe = regexp.MustCompile(`{{__hugo_ctx( pid=\d+)?/?}}\n?`)
105+
hugoCtxIndentedRe = regexp.MustCompile(`(?m)^[ \t]+({{__hugo_ctx[^\n]*}})`)
105106
)
106107

108+
// DedentMarkers removes leading whitespace from Hugo context marker lines
109+
// to prevent them from being treated as indented code blocks by Goldmark.
110+
func DedentMarkers(b []byte) []byte {
111+
if !bytes.Contains(b, hugoCtxPrefix) {
112+
return b
113+
}
114+
return hugoCtxIndentedRe.ReplaceAll(b, []byte("$1"))
115+
}
116+
107117
// Strip strips any Hugo context markers from b.
108118
func Strip(b []byte) []byte {
109119
if !bytes.Contains(b, hugoCtxPrefix) {

0 commit comments

Comments
 (0)