Support Go time format strings in permalinks

In the vein of an ancient TODO about supporting custom date formatting with
strftime, this allows `:`-prefixed Go time format strings in permalink segments.
This allows users to customize date-based permalinks any way they need to.

For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render
as `/19/11/9`.

See:

07978e4a49 (diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112)
https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
https://github.com/gohugoio/hugo/pull/6488
This commit is contained in:
Luke Francl
2019-11-09 21:58:38 -08:00
committed by Bjørn Erik Pedersen
parent cafecca440
commit 70a1aa345b
3 changed files with 43 additions and 11 deletions

View File

@@ -32,16 +32,21 @@ var testdataPermalinks = []struct {
{":title", true, "spf13-vim-3.0-release-and-new-website"},
{"/:year-:month-:title", true, "/2012-04-spf13-vim-3.0-release-and-new-website"},
{"/:year/:yearday/:month/:monthname/:day/:weekday/:weekdayname/", true, "/2012/97/04/April/06/5/Friday/"}, // Dates
{"/:section/", true, "/blue/"}, // Section
{"/:title/", true, "/spf13-vim-3.0-release-and-new-website/"}, // Title
{"/:slug/", true, "/the-slug/"}, // Slug
{"/:filename/", true, "/test-page/"}, // Filename
{"/:section/", true, "/blue/"}, // Section
{"/:title/", true, "/spf13-vim-3.0-release-and-new-website/"}, // Title
{"/:slug/", true, "/the-slug/"}, // Slug
{"/:filename/", true, "/test-page/"}, // Filename
{"/:06-:1-:2-:Monday", true, "/12-4-6-Friday"}, // Dates with Go formatting
{"/:2006_01_02_15_04_05.000", true, "/2012_04_06_03_01_59.000"}, // Complicated custom date format
// TODO(moorereason): need test scaffolding for this.
//{"/:sections/", false, "/blue/"}, // Sections
// Failures
{"/blog/:fred", false, ""},
{"/:year//:title", false, ""},
{"/:TITLE", false, ""}, // case is not normalized
{"/:2017", false, ""}, // invalid date format
{"/:2006-01-02", false, ""}, // valid date format but invalid attribute name
}
func TestPermalinkExpansion(t *testing.T) {
@@ -51,7 +56,7 @@ func TestPermalinkExpansion(t *testing.T) {
page := newTestPageWithFile("/test-page/index.md")
page.title = "Spf13 Vim 3.0 Release and new website"
d, _ := time.Parse("2006-01-02", "2012-04-06")
d, _ := time.Parse("2006-01-02 15:04:05", "2012-04-06 03:01:59")
page.date = d
page.section = "blue"
page.slug = "The Slug"