From 2f8c82d4415b0d71d6585dc733bdc351b223c70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 13 Oct 2017 10:21:28 +0200 Subject: [PATCH] Make sure Date and PublishDate is always set to a value if one is available Fixes #3854 --- hugolib/page.go | 8 ++++++++ hugolib/page_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/hugolib/page.go b/hugolib/page.go index 306d3373496..331a217d916 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -1147,6 +1147,14 @@ func (p *Page) update(f interface{}) error { } p.Params["draft"] = p.Draft + if p.Date.IsZero() { + p.Date = p.PublishDate + } + + if p.PublishDate.IsZero() { + p.PublishDate = p.Date + } + if p.Date.IsZero() && p.s.Cfg.GetBool("useModTimeAsFallback") { fi, err := p.s.Fs.Source.Stat(filepath.Join(p.s.PathSpec.AbsPathify(p.s.Cfg.GetString("contentDir")), p.File.Path())) if err == nil { diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 7723c02c443..4e369e24114 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -714,6 +714,42 @@ func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) { } } +// Issue #3854 +func TestPageWithDateFields(t *testing.T) { + assert := require.New(t) + pageWithDate := `--- +title: P%d +weight: %d +%s: 2017-10-13 +--- +Simple Page With Some Date` + + hasBothDates := func(p *Page) bool { + return p.Date.Year() == 2017 && p.PublishDate.Year() == 2017 + } + + datePage := func(field string, weight int) string { + return fmt.Sprintf(pageWithDate, weight, weight, field) + } + + t.Parallel() + assertFunc := func(t *testing.T, ext string, pages Pages) { + assert.True(len(pages) > 0) + for _, p := range pages { + assert.True(hasBothDates(p)) + } + + } + + fields := []string{"date", "publishdate", "pubdate", "published"} + pageContents := make([]string, len(fields)) + for i, field := range fields { + pageContents[i] = datePage(field, i+1) + } + + testAllMarkdownEnginesForPages(t, assertFunc, nil, pageContents...) +} + // Issue #2601 func TestPageRawContent(t *testing.T) { t.Parallel()