From 701486728e21bc0c6c78c2a8edb988abdf6116c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 13 Apr 2019 18:58:06 +0200 Subject: [PATCH] hugolib: Fix dates for sections with dates in front matter Fixes #5854 --- hugolib/page_test.go | 9 +++++++++ hugolib/site_sections.go | 19 ++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hugolib/page_test.go b/hugolib/page_test.go index a3b86ef2a..91ccb0d3e 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -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{}) @@ -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()) } diff --git a/hugolib/site_sections.go b/hugolib/site_sections.go index 9090c3ede..8fce43471 100644 --- a/hugolib/site_sections.go +++ b/hugolib/site_sections.go @@ -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 @@ -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