shithub: hugo

Download patch

ref: 701486728e21bc0c6c78c2a8edb988abdf6116c7
parent: f2795d4d2cef30170af43327f3ff7114923833b1
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Apr 13 14:58:06 EDT 2019

hugolib: Fix dates for sections with dates in front matter

Fixes #5854

--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -507,6 +507,14 @@
 
 `)
 
+	// 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{})
 
 	assert.Equal(1, len(b.H.Sites))
@@ -515,6 +523,7 @@
 	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())
 
 }
 
--- a/hugolib/site_sections.go
+++ b/hugolib/site_sections.go
@@ -162,12 +162,18 @@
 			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,7 +182,9 @@
 		// Regular page
 		p.parent = currentSection
 		children = append(children, p)
-		dates.UpdateDateAndLastmodIfAfter(p)
+		if dates != nil {
+			dates.UpdateDateAndLastmodIfAfter(p)
+		}
 
 		return false
 	})
@@ -183,8 +191,9 @@
 
 	if currentSection != nil {
 		currentSection.setPages(children)
-		currentSection.m.Dates = *dates
-
+		if dates != nil {
+			currentSection.m.Dates = *dates
+		}
 	}
 
 	// Build the sections hierarchy