Skip to content

Commit 6a30874

Browse files
committed
Make sure Date and PublishDate is always set to a value if one is available
Fixes #3854
1 parent 15ec031 commit 6a30874

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

‎hugolib/page.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,14 @@ func (p *Page) update(f interface{}) error {
11471147
}
11481148
p.Params["draft"] = p.Draft
11491149

1150+
if p.Date.IsZero() {
1151+
p.Date = p.PublishDate
1152+
}
1153+
1154+
if p.PublishDate.IsZero() {
1155+
p.PublishDate = p.Date
1156+
}
1157+
11501158
if p.Date.IsZero() && p.s.Cfg.GetBool("useModTimeAsFallback") {
11511159
fi, err := p.s.Fs.Source.Stat(filepath.Join(p.s.PathSpec.AbsPathify(p.s.Cfg.GetString("contentDir")), p.File.Path()))
11521160
if err == nil {

‎hugolib/page_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,42 @@ func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) {
714714
}
715715
}
716716

717+
// Issue #3854
718+
func TestPageWithDateFields(t *testing.T) {
719+
assert := require.New(t)
720+
pageWithDate := `---
721+
title: P%d
722+
weight: %d
723+
%s: 2017-10-13
724+
---
725+
Simple Page With Some Date`
726+
727+
hasBothDates := func(p *Page) bool {
728+
return p.Date.Year() == 2017 && p.PublishDate.Year() == 2017
729+
}
730+
731+
datePage := func(field string, weight int) string {
732+
return fmt.Sprintf(pageWithDate, weight, weight, field)
733+
}
734+
735+
t.Parallel()
736+
assertFunc := func(t *testing.T, ext string, pages Pages) {
737+
assert.True(len(pages) > 0)
738+
for _, p := range pages {
739+
assert.True(hasBothDates(p))
740+
}
741+
742+
}
743+
744+
fields := []string{"date", "publishdate", "pubdate", "published"}
745+
pageContents := make([]string, len(fields))
746+
for i, field := range fields {
747+
pageContents[i] = datePage(field, i+1)
748+
}
749+
750+
testAllMarkdownEnginesForPages(t, assertFunc, nil, pageContents...)
751+
}
752+
717753
// Issue #2601
718754
func TestPageRawContent(t *testing.T) {
719755
t.Parallel()

‎hugolib/site_test.go‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,6 @@ func TestGroupedPages(t *testing.T) {
750750
if bydate[1].Key != "2012-01" {
751751
t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key)
752752
}
753-
if bydate[2].Key != "2012-04" {
754-
t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "2012-04", bydate[2].Key)
755-
}
756-
if bydate[2].Pages[0].Title != "Three" {
757-
t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bydate[2].Pages[0].Title)
758-
}
759-
if len(bydate[0].Pages) != 2 {
760-
t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bydate[2].Pages))
761-
}
762753

763754
bypubdate, err := s.RegularPages.GroupByPublishDate("2006")
764755
if err != nil {

0 commit comments

Comments
 (0)