From bd76718dfc991b935d9d797cc8c088aefd5c1499 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Wed, 16 Aug 2017 16:08:49 -0400 Subject: [PATCH] Set post.title --- collection/strategies.go | 7 ++++--- example/_posts/2017-06-10-birthday.md | 2 +- utils/filepath.go | 19 +++++++++++++++---- utils/filepath_test.go | 5 +++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/collection/strategies.go b/collection/strategies.go index f7d1e94..d86fcd5 100644 --- a/collection/strategies.go +++ b/collection/strategies.go @@ -31,18 +31,19 @@ func (s defaultStrategy) future(filename string) bool { return fa type postsStrategy struct{} func (s postsStrategy) addDate(filename string, fm map[string]interface{}) { - if t, found := utils.FilenameDate(filename); found { + if t, title, found := utils.FilenameDate(filename); found { fm["date"] = t + fm["title"] = title } } func (s postsStrategy) collectible(filename string) bool { - _, ok := utils.FilenameDate(filename) + _, _, ok := utils.FilenameDate(filename) return ok } func (s postsStrategy) future(filename string) bool { - t, ok := utils.FilenameDate(filename) + t, _, ok := utils.FilenameDate(filename) return ok && t.After(time.Now()) } diff --git a/example/_posts/2017-06-10-birthday.md b/example/_posts/2017-06-10-birthday.md index e58f84a..9ec9bc2 100755 --- a/example/_posts/2017-06-10-birthday.md +++ b/example/_posts/2017-06-10-birthday.md @@ -4,4 +4,4 @@ {{ page.title }} ================ -I was born on this day. +I was *born* on _this_ day. diff --git a/utils/filepath.go b/utils/filepath.go index 4b9b7af..44c688c 100644 --- a/utils/filepath.go +++ b/utils/filepath.go @@ -9,10 +9,21 @@ import ( // FilenameDate returns the date for a filename that uses Jekyll post convention. // It also returns a bool indicating whether a date was found. -func FilenameDate(s string) (time.Time, bool) { - layout := "2006-01-02-" - t, err := time.Parse(layout, filepath.Base(s + layout)[:len(layout)]) - return t, err == nil +func FilenameDate(s string) (t time.Time, title string, found bool) { + var ( + base = TrimExt(filepath.Base(s)) + layout = "2006-01-02-" + ) + if len(base) < len(layout) { + found = false + return + } + t, err := time.Parse(layout, base[:len(layout)]) + if err != nil { + return + } + title, found = base[len(layout):], true + return } // MatchList implement Jekyll include: and exclude: configurations. diff --git a/utils/filepath_test.go b/utils/filepath_test.go index 0f810e3..dd8e974 100644 --- a/utils/filepath_test.go +++ b/utils/filepath_test.go @@ -21,11 +21,12 @@ func TestMustAbs(t *testing.T) { } func TestFilenameDate(t *testing.T) { - d, found := FilenameDate("2017-07-02-post.html") + d, title, found := FilenameDate("2017-07-02-post.html") require.True(t, found) + require.Equal(t, "post", title) require.Equal(t, timeMustParse("2017-07-02T00:00:00Z"), d) - _, found = FilenameDate("not-post.html") + _, _, found = FilenameDate("not-post.html") require.False(t, found) }