Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dates to be parsed from filenames #3310

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/content/content/front-matter.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
aliases:
- /doc/front-matter/
lastmod: 2015-12-23
lastmod: 2017-06-29
date: 2013-07-01
menu:
main:
Expand Down Expand Up @@ -117,3 +117,12 @@ It's possible to set some options for Markdown rendering in the page's front mat

See [Configuration]({{< ref "overview/configuration.md#configure-blackfriday-rendering" >}}) for more.

## Fallback date variable from filenames
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 `filenameDateFallbackPattern` and `filenameDateFallbackFormat` configuration options. These will allow you to fallback on datestamps provided in the filename in place of a date value in the front matter.

As an example, for posts following a YYY-MM-DD-posttitle.md naming convention, you can use:

```
filenameDateFallbackPattern: "(?P<year>\\d{4})\\-(?P<month>\\d{2})\\-(?P<day>\\d{2})"
filenameDateFallbackFormat: "2006-01-02"
```
6 changes: 5 additions & 1 deletion docs/content/overview/configuration.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
aliases:
- /doc/configuration/
lastmod: 2016-09-17
lastmod: 2017-04-09
date: 2013-07-01
linktitle: Configuration
menu:
Expand Down Expand Up @@ -139,6 +139,10 @@ along with their current, default values:
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<year>\\d{4})\\-(?P<month>\\d{2})\\-(?P<day>\\d{2})"
# Time format for custom dates in filenames. Must match `filenameDateFallbackPattern`
filenameDateFallbackFormat: "2006-01-02"
footnoteAnchorPrefix: ""
footnoteReturnLinkContents: ""
# google analytics tracking id
Expand Down
2 changes: 2 additions & 0 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ 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("defaultContentLanguage", "en")
v.SetDefault("defaultContentLanguageInSubdir", false)
v.SetDefault("enableMissingTranslationPlaceholders", false)
Expand Down
8 changes: 8 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ func (p *Page) update(f interface{}) error {
p.Date = fi.ModTime()
p.Params["date"] = p.Date
}
} else 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.Date = filenameDate
p.Params["date"] = p.Date
}
}

if p.Lastmod.IsZero() {
Expand Down
17 changes: 17 additions & 0 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Content of the file goes Here
Content of the file goes Here
`
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
simplePageNoDate = "---\ntitle: Path Date\n---\n Date param from url"
simplePageJSONMultiple = `
{
"title": "foobar",
Expand Down Expand Up @@ -847,6 +848,22 @@ func TestPageWithDate(t *testing.T) {
checkPageDate(t, p, d)
}

func TestPageWithDateInFilename(t *testing.T) {
t.Parallel()
cfg, fs := newTestCfg()

writeSource(t, fs, filepath.Join("content", "2017-01-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]
d, _ := time.Parse(time.RFC3339, "2017-01-31")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this always fails as RFC3339 requires timezone info.


checkPageDate(t, p, d)
}

func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {
t.Parallel()
assertFunc := func(t *testing.T, ext string, pages Pages) {
Expand Down