mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 23:38:53 +00:00
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:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user