From 14336e93676248c1b47c26a52ccd17c820018b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 1 Jun 2022 10:19:05 +0200 Subject: [PATCH] Fix panic with markdownify/RenderString with shortcode on Page with no content file Fixes #9959 --- hugolib/content_map_page.go | 2 -- hugolib/page__new.go | 2 ++ hugolib/page__per_output.go | 2 -- hugolib/renderstring_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go index c6522809efe..7e6b6e67040 100644 --- a/hugolib/content_map_page.go +++ b/hugolib/content_map_page.go @@ -163,8 +163,6 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB }, } - ps.shortcodeState = newShortcodeHandler(ps, ps.s) - if err := ps.mapContent(parentBucket, metaProvider); err != nil { return nil, ps.wrapError(err) } diff --git a/hugolib/page__new.go b/hugolib/page__new.go index 897c0281b23..e52b9476b03 100644 --- a/hugolib/page__new.go +++ b/hugolib/page__new.go @@ -66,6 +66,8 @@ func newPageBase(metaProvider *pageMeta) (*pageState, error) { }, } + ps.shortcodeState = newShortcodeHandler(ps, ps.s) + siteAdapter := pageSiteAdapter{s: s, p: ps} ps.pageMenus = &pageMenus{p: ps} diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go index 2bf16dd9ee6..e69d32e27a6 100644 --- a/hugolib/page__per_output.go +++ b/hugolib/page__per_output.go @@ -25,7 +25,6 @@ import ( "errors" - "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/common/types/hstring" "github.com/gohugoio/hugo/identity" @@ -334,7 +333,6 @@ func (p *pageContentOutput) WordCount() int { } func (p *pageContentOutput) RenderString(args ...any) (template.HTML, error) { - defer herrors.Recover() if len(args) < 1 || len(args) > 2 { return "", errors.New("want 1 or 2 arguments") } diff --git a/hugolib/renderstring_test.go b/hugolib/renderstring_test.go index d2f453c3303..1be0cdffb53 100644 --- a/hugolib/renderstring_test.go +++ b/hugolib/renderstring_test.go @@ -158,5 +158,35 @@ Page Type: *hugolib.pageForShortcode`, ) }) +} + +// Issue 9959 +func TestRenderStringWithShortcodeInPageWithNoContentFile(t *testing.T) { + t.Parallel() + + files := ` +-- config.toml -- +-- layouts/shortcodes/myshort.html -- +Page Kind: {{ .Page.Kind }} +-- layouts/index.html -- +Short: {{ .RenderString "{{< myshort >}}" }} +Has myshort: {{ .HasShortcode "myshort" }} +Has other: {{ .HasShortcode "other" }} + + ` + + b := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + }, + ).Build() + + b.AssertFileContent("public/index.html", + ` +Page Kind: home +Has myshort: true +Has other: false +`) }