commit 212d9e3017c32b91ffc73a6a08e73f34beb1e224
parent 4daac654d90bdc6adf92bf8b15a4aa45d7d62efd
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Wed, 1 Jun 2022 10:19:05 +0200
Fix panic with markdownify/RenderString with shortcode on Page with no content file
Fixes #9959
Diffstat:
6 files changed, 35 insertions(+), 6 deletions(-)
diff --git 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__common.go b/hugolib/page__common.go
@@ -102,6 +102,9 @@ type pageCommon struct {
// The parsed page content.
pageContent
+ // Keeps track of the shortcodes on a page.
+ shortcodeState *shortcodeHandler
+
// Set if feature enabled and this is in a Git repo.
gitInfo *gitmap.GitInfo
codeowners []string
diff --git a/hugolib/page__content.go b/hugolib/page__content.go
@@ -33,8 +33,6 @@ type pageContent struct {
cmap *pageContentMap
- shortcodeState *shortcodeHandler
-
source rawPageContent
}
diff --git 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
@@ -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
@@ -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
+`)
}