Skip to content

Commit

Permalink
Fix panic with markdownify/RenderString with shortcode on Page with n…
Browse files Browse the repository at this point in the history
…o content file

Fixes gohugoio#9959
  • Loading branch information
bep committed Jun 1, 2022
1 parent 4daac65 commit 23c9bde
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
4 changes: 3 additions & 1 deletion hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
},
}

ps.shortcodeState = newShortcodeHandler(ps, ps.s)
if ps.shortcodeState == nil {
panic("NO2")
}

if err := ps.mapContent(parentBucket, metaProvider); err != nil {
return nil, ps.wrapError(err)
Expand Down
3 changes: 3 additions & 0 deletions hugolib/page__common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions hugolib/page__content.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type pageContent struct {

cmap *pageContentMap

shortcodeState *shortcodeHandler

source rawPageContent
}

Expand Down
2 changes: 2 additions & 0 deletions hugolib/page__new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 0 additions & 2 deletions hugolib/page__per_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
30 changes: 30 additions & 0 deletions hugolib/renderstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`)

}

0 comments on commit 23c9bde

Please sign in to comment.