hugolib: Fix dates for sections with dates in front matter
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 13 Apr 2019 16:58:06 +0000 (18:58 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 13 Apr 2019 18:01:43 +0000 (20:01 +0200)
Fixes #5854

hugolib/page_test.go
hugolib/site_sections.go

index a3b86ef2aea4eac069490c626063119522b00129..91ccb0d3e55ceee58f891829ece603385cf798dc 100644 (file)
@@ -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())
 
 }
 
index 9090c3eded43c7c71285d57e95dd48e1af789cc9..8fce43471fc3835575108097ec445517c7f3ff77 100644 (file)
@@ -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