diff --git a/hugolib/page_frontmatter.go b/hugolib/page_frontmatter.go index c5f50f7ef5e..532d33047db 100644 --- a/hugolib/page_frontmatter.go +++ b/hugolib/page_frontmatter.go @@ -21,6 +21,8 @@ import ( "strings" "time" + "github.com/mitchellh/mapstructure" + "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/config" @@ -198,6 +200,64 @@ func (f frontmatterHandler) newChainedFrontMatterFieldHandler(handlers ...frontM } } +type frontmatterConfig struct { + date []string + lastMod []string + publishDate []string + expiryDate []string +} + +const ( + fmDate = "date" + fmPubDate = "publishdate" + fmLastMod = "lastmod" + fmExpiryDate = "expirydate" +) + +func newDefaultFrontmatterConfig() frontmatterConfig { + return frontmatterConfig{ + date: []string{fmDate, fmPubDate, fmLastMod}, + lastMod: []string{fmLastMod, fmDate}, + publishDate: []string{fmPubDate, fmDate}, + expiryDate: []string{fmExpiryDate}, + } +} + +func newFrontmatterConfig(cfg config.Provider) (frontmatterConfig, error) { + c := newDefaultFrontmatterConfig() + + if cfg.IsSet("frontmatter") { + fm := cfg.GetStringMap("frontmatter") + if fm != nil { + for k, v := range fm { + loki := strings.ToLower(k) + switch loki { + case fmDate: + c.date = toLowerSlice(v) + case fmPubDate: + c.publishDate = toLowerSlice(v) + case fmLastMod: + c.lastMod = toLowerSlice(v) + case fmExpiryDate: + c.expiryDate = toLowerSlice(v) + } + } + } + err := mapstructure.WeakDecode(fm, &c) + return c, err + } + return c, nil +} + +func toLowerSlice(in interface{}) []string { + out := cast.ToStringSlice(in) + for i := 0; i < len(out); i++ { + out[i] = strings.ToLower(out[i]) + } + + return out +} + func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatterHandler, error) { if logger == nil { @@ -208,6 +268,16 @@ func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatte handlers := &frontmatterFieldHandlers{logger: logger} + /* + + [frontmatter] + date = ["date", "publishDate", "lastMod"] + lastMod = ["lastMod", "date"] + publishDate = ["publishDate", "date"] + expiryDate = ["expiryDate"] + + */ + dateHandlers := []frontMatterFieldHandler{handlers.defaultDateHandler} defaultDate := cfg.Get("frontmatter.defaultdate") diff --git a/hugolib/page_frontmatter_test.go b/hugolib/page_frontmatter_test.go index 317e1f26a7f..38f486e6c07 100644 --- a/hugolib/page_frontmatter_test.go +++ b/hugolib/page_frontmatter_test.go @@ -61,27 +61,53 @@ func TestDateAndSlugFromBaseFilename(t *testing.T) { } } -func TestFrontMatterDates(t *testing.T) { - t.Parallel() +func TestFrontMatterNewConfig(t *testing.T) { + assert := require.New(t) - defaultDateSettings := []string{"none", "file"} + cfg := viper.New() - for _, defaultDateSetting := range defaultDateSettings { - t.Run(fmt.Sprintf("defaultDate=%s", defaultDateSetting), func(t *testing.T) { - doTestFrontMatterDates(t, defaultDateSetting) - }) - } -} + cfg.Set("frontmatter", map[string]interface{}{ + "date": []string{"publishDate", "LastMod"}, + "Lastmod": []string{"publishDate"}, + "expiryDate": []string{"lastMod"}, + "publishDate": []string{"date"}, + }) -func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) { - assert := require.New(t) + fc, err := newFrontmatterConfig(cfg) + assert.NoError(err) + assert.Equal([]string{"publishdate", "lastmod"}, fc.date) + assert.Equal([]string{"publishdate"}, fc.lastMod) + assert.Equal([]string{"lastmod"}, fc.expiryDate) + assert.Equal([]string{"date"}, fc.publishDate) + + // Default + cfg = viper.New() + fc, err = newFrontmatterConfig(cfg) + assert.NoError(err) + assert.Equal(3, len(fc.date)) + assert.Equal(2, len(fc.lastMod)) + assert.Equal(2, len(fc.publishDate)) + assert.Equal(1, len(fc.expiryDate)) +} + +func TestFrontMatterDatesConfigVariations(t *testing.T) { cfg := viper.New() cfg.Set("frontmatter", map[string]interface{}{ - "defaultDate": []string{defaultDateSetting}, + "defaultDate": []string{"date"}, }) + fmt.Println(">>", cfg) +} + +func TestFrontMatterDates(t *testing.T) { + t.Parallel() + + assert := require.New(t) + + cfg := viper.New() + handler, err := newFrontmatterHandler(newWarningLogger(), cfg) assert.NoError(err)