The patch is failing if I reuse the same test data. I will explain by showing how the GroupByParamDate works in hugo then explain how my fix works.
Originally in hugo
The test data in hugo is like this:
var pageGroupTestSources = []pageGroupTestObject{
{"/section1/testpage1.md", 3, "2012-04-06", "foo"},
{"/section1/testpage2.md", 3, "2012-01-01", "bar"},
{"/section1/testpage3.md", 2, "2012-04-06", "foo"},
{"/section2/testpage4.md", 1, "2012-03-02", "bar"},
{"/section2/testpage5.md", 1, "2012-04-06", "baz"},
}
^
|
date field
In the test data, the custom date param is actually copied from the date parameter and casted as a date:
p.params["custom_date"] = cast.ToTime(src.date)
The format passed to GroupByParamDate {{ .GroupByParamDate “custom_date” “format” "2006-01 }} is used to group and order the pages. In this case the format “yyyy-mm” will group the pages by year-month. Based on the test data it will give three groups: [2012-04, 2012-01, 2012-03]
If I pass the format “2006-01-02” it will group the pages by the full date, so if I have 5 pages with 5 different dates it will give 5 groups.
The custom_date param is casted to a date in GoLang so it must be parsed from TOML as date (since TOML supports time types)
With my fix
I discovered the problem by mistake when I tried to reuse the test data, I basically did the following:
p.params["custom_date_string"] = src.date
I basically reuse the date field, but do not cast it into a date so it’s effectively a string representing a date. I duplicated the GroupByParamDates and applied them to the custom_date_param
My fix modifies the GroupByParamDate in the following way:
- It tests if the custom_date param is a string
- if it is a string, it tries to parse as a date using the passed format
So hugo uses the format to parse the date from the string, if he can parse it he will add the page to the candidate pages to group.
When hugo reaches the sorting step, he also uses the same format to group the pages. If the format is different than the custom string date it will not parse the string as date and the tests fail
To make the tests pass, I added an other string parameter with date strings inside looking exactly like the “format” used in GroupByParamDate
I made two branches one with tests passing and the other failing:
In the end I don’t know if there is a clean way to do it. It seems the GroupByParamDate is biased toward people using TOML. In my case I’m maybe going to migrate to TOML as I discovered rq which is equivalent to jq for toml