Skip to content

Commit

Permalink
Simplified filename date fallback configuration.
Browse files Browse the repository at this point in the history
Removed useFilenameDateAsFallback option and instead check to see if filenameDateFallbackPattern is set. As a result, filenameDateFallbackPattern now defaults to Nil.

Added a reference example to the docs to ease copy/paste configuration for a popular YYYY-MM-DD filename format.
  • Loading branch information
devjack committed Jun 29, 2017
1 parent 902ac96 commit 38a313f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
11 changes: 9 additions & 2 deletions 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: 2017-04-09
lastmod: 2017-06-29
date: 2013-07-01
menu:
main:
Expand Down Expand Up @@ -118,4 +118,11 @@ 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 `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 frontmatter.
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: 2 additions & 4 deletions docs/content/overview/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ 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. Only used with `useFilenameDateAsFallback`
# 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. Only used with `useFilenameDateAsFallback` and must match `filenameDateFallbackPattern`
# Time format for custom dates in filenames. Must match `filenameDateFallbackPattern`
filenameDateFallbackFormat: "2006-01-02"
footnoteAnchorPrefix: ""
footnoteReturnLinkContents: ""
Expand Down Expand Up @@ -191,8 +191,6 @@ along with their current, default values:
title: ""
# if true, use /filename.html instead of /filename/
uglyURLs: false
#if true, use dates in filenames e.g. 2017-01-31-mypostname.md
useFilenameDateAsFallback: false
# Do not make the url/path to lowercase
disablePathToLower: false
# if true, auto-detect Chinese/Japanese/Korean Languages in the content. (.Summary and .WordCount can work properly in CJKLanguage)
Expand Down
3 changes: 1 addition & 2 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func loadDefaultSettingsFor(v *viper.Viper) {
v.SetDefault("enableEmoji", false)
v.SetDefault("pygmentsCodeFencesGuessSyntax", false)
v.SetDefault("useModTimeAsFallback", false)
v.SetDefault("useFilenameDateAsFallback", false)
v.SetDefault("filenameDateFallbackPattern", "(?P<year>\\d{4})\\-(?P<month>\\d{2})\\-(?P<day>\\d{2})")
v.SetDefault("filenameDateFallbackPattern", nil)
v.SetDefault("filenameDateFallbackFormat", "2006-01-02")
v.SetDefault("defaultContentLanguage", "en")
v.SetDefault("defaultContentLanguageInSubdir", false)
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ func (p *Page) update(f interface{}) error {
p.Date = fi.ModTime()
p.Params["date"] = p.Date
}
} else if p.Date.IsZero() && p.s.Cfg.GetBool("useFilenameDateAsFallback") {
} 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)
Expand Down

0 comments on commit 38a313f

Please sign in to comment.