Skip to content

Commit

Permalink
hugolib: Fix dates for sections with dates in front matter
Browse files Browse the repository at this point in the history
Fixes #5854
  • Loading branch information
bep committed Apr 13, 2019
1 parent f2795d4 commit 7014867
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 9 additions & 0 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ date: 2017-01-15
title: No Date
---
`)

// https://github.com/gohugoio/hugo/issues/5854
b.WithSimpleConfigFile().WithContent("with-index-date/_index.md", `---
title: Date
date: 2018-01-15
---
`)

b.CreateSites().Build(BuildCfg{})
Expand All @@ -515,6 +523,7 @@ title: No Date
assert.Equal(2017, s.getPage("/").Date().Year())
assert.Equal(2017, s.getPage("/no-index").Date().Year())
assert.True(s.getPage("/with-index-no-date").Date().IsZero())
assert.Equal(2018, s.getPage("/with-index-date").Date().Year())

}

Expand Down
19 changes: 14 additions & 5 deletions hugolib/site_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,18 @@ func (s *Site) assembleSections() pageStatePages {
if currentSection != nil {
// A new section
currentSection.setPages(children)
currentSection.m.Dates = *dates
if dates != nil {
currentSection.m.Dates = *dates
}
}

currentSection = p
children = make(page.Pages, 0)
dates = &resource.Dates{}
dates = nil
// Use section's dates from front matter if set.
if resource.IsZeroDates(currentSection) {
dates = &resource.Dates{}
}

return false

Expand All @@ -176,15 +182,18 @@ func (s *Site) assembleSections() pageStatePages {
// Regular page
p.parent = currentSection
children = append(children, p)
dates.UpdateDateAndLastmodIfAfter(p)
if dates != nil {
dates.UpdateDateAndLastmodIfAfter(p)
}

return false
})

if currentSection != nil {
currentSection.setPages(children)
currentSection.m.Dates = *dates

if dates != nil {
currentSection.m.Dates = *dates
}
}

// Build the sections hierarchy
Expand Down

0 comments on commit 7014867

Please sign in to comment.