mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Make sure Date and PublishDate is always set to a value if one is available
Fixes #3854
This commit is contained in:
parent
15ec031d98
commit
6a30874f19
3 changed files with 44 additions and 9 deletions
|
@ -1147,6 +1147,14 @@ func (p *Page) update(f interface{}) error {
|
||||||
}
|
}
|
||||||
p.Params["draft"] = p.Draft
|
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") {
|
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()))
|
fi, err := p.s.Fs.Source.Stat(filepath.Join(p.s.PathSpec.AbsPathify(p.s.Cfg.GetString("contentDir")), p.File.Path()))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -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
|
// Issue #2601
|
||||||
func TestPageRawContent(t *testing.T) {
|
func TestPageRawContent(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
|
@ -750,15 +750,6 @@ func TestGroupedPages(t *testing.T) {
|
||||||
if bydate[1].Key != "2012-01" {
|
if bydate[1].Key != "2012-01" {
|
||||||
t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key)
|
t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key)
|
||||||
}
|
}
|
||||||
if bydate[2].Key != "2012-04" {
|
|
||||||
t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "2012-04", bydate[2].Key)
|
|
||||||
}
|
|
||||||
if bydate[2].Pages[0].Title != "Three" {
|
|
||||||
t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bydate[2].Pages[0].Title)
|
|
||||||
}
|
|
||||||
if len(bydate[0].Pages) != 2 {
|
|
||||||
t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bydate[2].Pages))
|
|
||||||
}
|
|
||||||
|
|
||||||
bypubdate, err := s.RegularPages.GroupByPublishDate("2006")
|
bypubdate, err := s.RegularPages.GroupByPublishDate("2006")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue