Strip nested page context markers from standalone RenderShortcodes

Fixes #14732

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bjørn Erik Pedersen
2026-04-07 15:59:07 +02:00
parent 58927aa14a
commit 45e4596630
4 changed files with 57 additions and 7 deletions
+14 -7
View File
@@ -2,7 +2,6 @@ package hugolib
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
@@ -39,8 +38,8 @@ import (
"github.com/gohugoio/hugo/hugofs/hglob"
"github.com/gohugoio/hugo/hugolib/sitesmatrix"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/media"
"github.com/spf13/afero"
"github.com/spf13/cast"
"golang.org/x/text/unicode/norm"
"golang.org/x/tools/txtar"
)
@@ -462,12 +461,20 @@ func (s *IntegrationTestBuilder) AssertFileContentExact(filename string, matches
func (s *IntegrationTestBuilder) AssertNoRenderShortcodesArtifacts() {
s.Helper()
for _, p := range s.H.Pages() {
content, err := p.Content(context.Background())
afero.Walk(s.fs.PublishDir, "", func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
ext := strings.TrimPrefix(filepath.Ext(path), ".")
if !s.H.Conf.GetConfigSection("mediaTypes").(media.Types).IsTextSuffix(ext) {
return nil
}
content, err := afero.ReadFile(s.fs.PublishDir, path)
s.Assert(err, qt.IsNil)
comment := qt.Commentf("Page: %s\n%s", p.Path(), content)
s.Assert(strings.Contains(cast.ToString(content), "__hugo_ctx"), qt.IsFalse, comment)
}
comment := qt.Commentf("File: %s\n%s", path, string(content))
s.Assert(strings.Contains(string(content), "__hugo_ctx"), qt.IsFalse, comment)
return nil
})
}
type IntegrationTestImageHelper struct {
+4
View File
@@ -1109,6 +1109,10 @@ func (c *cachedContentScope) RenderShortcodes(ctx context.Context) (template.HTM
return template.HTML(hugocontext.Wrap(cc, pco.po.p.pid)), nil
}
// Strip any Hugo context markers from nested RenderShortcodes calls
// that won't be processed by Goldmark.
cc = hugocontext.Strip(cc)
return helpers.BytesToHTML(cc), nil
}
+31
View File
@@ -521,3 +521,34 @@ not emphasized
"<p>a</p>\n<p><em>emphasized</em></p>\n<p>not emphasized</p>\n<p>b</p>\n",
)
}
// Issue 14732.
func TestRenderShortcodesStandalone(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['section','rss','sitemap','taxonomy','term']
-- layouts/all.html --
RenderShortcodes: {{ .RenderShortcodes }}|
-- layouts/_shortcodes/headings.html --
## Heading 1
### Heading 2
{{ $p := site.GetPage "includeme" }}
RenderShortcodes2: {{ $p.RenderShortcodes }}|
-- content/p1.md --
---
title: "p1"
---
{{% headings "headings" %}}
-- content/includeme.md --
---
title: "includeme"
---
## Heading 2
`
b := Test(t, files)
b.AssertNoRenderShortcodesArtifacts()
}
@@ -104,6 +104,14 @@ var (
hugoCtxRe = regexp.MustCompile(`{{__hugo_ctx( pid=\d+)?/?}}\n?`)
)
// Strip strips any Hugo context markers from b.
func Strip(b []byte) []byte {
if !bytes.Contains(b, hugoCtxPrefix) {
return b
}
return hugoCtxRe.ReplaceAll(b, nil)
}
var _ parser.InlineParser = (*hugoContextParser)(nil)
type hugoContextParser struct{}