diff --git a/docs/content/getting-started/configuration.md b/docs/content/getting-started/configuration.md index 6034b6eb29e..dc7cabe2e65 100644 --- a/docs/content/getting-started/configuration.md +++ b/docs/content/getting-started/configuration.md @@ -245,10 +245,6 @@ disablePathToLower = false enableEmoji = false # Show a placeholder instead of the default value or an empty string if a translation is missing enableMissingTranslationPlaceholders = false -# Regex to use for dates in filenames. -filenameDateFallbackPattern = "(?P\\d{4})\\-(?P\\d{2})\\-(?P\\d{2})" -# Time format for custom dates in filenames. Only used with `useFilenameDateAsFallback` and must match `filenameDateFallbackPattern` -filenameDateFallbackFormat = "2006-01-02" footnoteAnchorPrefix = "" footnoteReturnLinkContents = "" # google analytics tracking id diff --git a/docs/content/variables/page.md b/docs/content/variables/page.md index c2e07d4f5f0..117aafb4d4c 100644 --- a/docs/content/variables/page.md +++ b/docs/content/variables/page.md @@ -37,7 +37,7 @@ See [`.Scratch`](/functions/scratch/) for page-scoped, writable variables. `.Date` : the date associated with the page; `.Date` pulls from the `date` field in a content's front matter. -If you're migrating content to Hugo, you may have content with dates in the filename. For example `2017-01-31-myblog.md`. You can optionally enable the `useFilenameDateAsFallback` configuration option. This will attempt to parse the datestamp in the filename and use it as a fallback to providing a date variable in the front matter. +If you're migrating content to Hugo, you may have content with dates in the filename. For example `2017-01-31-myblog.md`. You can optionally enable the `useFilenameDateAsFallback` configuration option. This will attempt to parse the datestamp in the filename and use it as a fallback to providing a date variable in the front matter. It will also set `.Slug` to contain the path without the date prefix.' See also `.ExpiryDate`, `.PublishDate`, and `.Lastmod`. `.Description` diff --git a/hugolib/config.go b/hugolib/config.go index 9cc47da0f7f..f139a0b2d12 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -127,8 +127,7 @@ func loadDefaultSettingsFor(v *viper.Viper) { v.SetDefault("enableEmoji", false) v.SetDefault("pygmentsCodeFencesGuessSyntax", false) v.SetDefault("useModTimeAsFallback", false) - v.SetDefault("filenameDateFallbackPattern", nil) - v.SetDefault("filenameDateFallbackFormat", "2006-01-02") + v.SetDefault("useFilenameDateAsFallback", false) v.SetDefault("defaultContentLanguage", "en") v.SetDefault("defaultContentLanguageInSubdir", false) v.SetDefault("enableMissingTranslationPlaceholders", false) diff --git a/hugolib/page.go b/hugolib/page.go index fb9cf309c31..652d20beb36 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -1079,14 +1079,32 @@ func (p *Page) update(f interface{}) error { } } - if p.Date.IsZero() && p.s.Cfg.GetString("filenameDateFallbackPattern") != "" { - dateExp := regexp.MustCompile(p.s.Cfg.GetString("filenameDateFallbackPattern")) - dateString := dateExp.FindString(p.File.Path()) - filenameDate, err := time.Parse(p.s.Cfg.GetString("filenameDateFallbackFormat"), dateString) - if err == nil { - p.s.Log.DEBUG.Printf("using filename date as fallback for page %s", p.File.Path()) - p.Date = filenameDate - p.Params["date"] = p.Date + if p.Date.IsZero() && p.s.Cfg.GetString("useFilenameDateAsFallback") != "" { + dateExp := regexp.MustCompile("(?P\\d{4})\\-(?P\\d{2})\\-(?P\\d{2})-(?P.*)\\..*") + match := dateExp.FindStringSubmatch(p.File.Path()) + + if match != nil { + year := match[1] + month := match[2] + day := match[3] + slug := match[4] + + filenameDate, err := time.Parse("2006-01-02", year+"-"+month+"-"+day) + + if err == nil { + p.Date = filenameDate + p.Params["date"] = p.Date + if p.Slug == "" { + p.Slug = slug + p.s.Log.DEBUG.Printf("Using filename date (%s) and slug (%s) as fallback for page %s", p.Date, p.Slug, p.File.Path()) + + } else { + p.s.Log.DEBUG.Printf("Using filename date (%s) as fallback for page %s", p.Date, p.File.Path()) + + } + } else { + p.s.Log.WARN.Printf("File has what looks like a date, but the date is invalid for page %s.", p.File.Path()) + } } } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 553238a71be..1b7a5aa1383 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -516,6 +516,7 @@ func checkPageDate(t *testing.T, page *Page, time time.Time) { if page.Date != time { t.Fatalf("Page date is: %s. Expected: %s", page.Date, time) } + } func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) { @@ -855,12 +856,15 @@ func TestPageWithDate(t *testing.T) { d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z") checkPageDate(t, p, d) + } -func TestPageWithDateInFilename(t *testing.T) { +func TestDateAsFallbackWithDateInFilename(t *testing.T) { t.Parallel() cfg, fs := newTestCfg() + cfg.Set("useFilenameDateAsFallback", true) + writeSource(t, fs, filepath.Join("content", "2017-01-31-simple.md"), simplePageNoDate) s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) @@ -868,9 +872,51 @@ func TestPageWithDateInFilename(t *testing.T) { require.Len(t, s.RegularPages, 1) p := s.RegularPages[0] - d, _ := time.Parse(time.RFC3339, "2017-01-31") + d, err := time.Parse("2006-01-02", "2017-01-31") + + assert.Equal(t, err, nil) checkPageDate(t, p, d) + + assert.Equal(t, "simple", p.Slug, "slug for page should be set as there is both date and title in filename") +} + +func TestDateAsFallbackWithOutDateInFilename(t *testing.T) { + t.Parallel() + cfg, fs := newTestCfg() + + cfg.Set("useFilenameDateAsFallback", true) + + writeSource(t, fs, filepath.Join("content", "simple.md"), simplePageNoDate) + + s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) + + require.Len(t, s.RegularPages, 1) + + p := s.RegularPages[0] + + assert.True(t, p.Date.IsZero(), "page date should be empty as no date in filename") + assert.Equal(t, "", p.Slug, "page slug should not be set as no date in filename") + +} + +func TestDateAsFallbackWithInvalidDateInFilename(t *testing.T) { + t.Parallel() + cfg, fs := newTestCfg() + + cfg.Set("useFilenameDateAsFallback", true) + + writeSource(t, fs, filepath.Join("content", "2017-31-31-simple.md"), simplePageNoDate) + + s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) + + require.Len(t, s.RegularPages, 1) + + p := s.RegularPages[0] + + assert.True(t, p.Date.IsZero(), "page date should be empty as no valid date in filename") + assert.Equal(t, "", p.Slug, "page slug should not be set as no valid date in filename") + } func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {