Skip to content

Commit

Permalink
Fix .RawContent for empty content pages (gohugoio#11407)
Browse files Browse the repository at this point in the history
  • Loading branch information
bep authored Aug 30, 2023
1 parent a7b93e6 commit 3a8aad6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
5 changes: 2 additions & 3 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func (p *pageState) RawContent() string {
if start == -1 {
start = 0
}

return string(p.source.parsed.Input()[start:])
}

Expand Down Expand Up @@ -727,9 +728,7 @@ Loop:
frontMatterSet = true

next := iter.Peek()
if !next.IsDone() {
p.source.posMainContent = next.Pos()
}
p.source.posMainContent = next.Pos()

if !p.s.shouldBuild(p) {
// Nothing more to do.
Expand Down
36 changes: 21 additions & 15 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,27 +642,33 @@ Simple Page With Some Date`
testAllMarkdownEnginesForPages(t, assertFunc, nil, pageContents...)
}

// Issue #2601
func TestPageRawContent(t *testing.T) {
t.Parallel()
c := qt.New(t)
cfg, fs := newTestCfg()
configs, err := loadTestConfigFromProvider(cfg)
c.Assert(err, qt.IsNil)

writeSource(t, fs, filepath.Join("content", "raw.md"), `---
title: Raw
files := `
-- hugo.toml --
-- content/basic.md --
---
**Raw**`)

writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `{{ .RawContent }}`)
title: "basic"
---
**basic**
-- content/empty.md --
---
title: "empty"
---
-- layouts/_default/single.html --
|{{ .RawContent }}|
`

s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

c.Assert(len(s.RegularPages()), qt.Equals, 1)
p := s.RegularPages()[0]
b.AssertFileContent("public/basic/index.html", "|**basic**|")
b.AssertFileContent("public/empty/index.html", "! title")

c.Assert("**Raw**", qt.Equals, p.RawContent())
}

func TestPageWithShortCodeInSummary(t *testing.T) {
Expand Down

0 comments on commit 3a8aad6

Please sign in to comment.