allow nested params when using Pages.GroupByParam and Pages.GroupByParamDate - hugo - [fork] hugo port for 9front
(HTM) git clone git@git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
(DIR) commit 51f09b17fdbaae705f043d8e675a8bcdc3ad12c6
(DIR) parent be643580dd3a0d25be92cb2759f21b628ad80821
(HTM) Author: n1xx1 <me@simonemiraglia.it>
Date: Thu, 1 Aug 2024 13:12:32 +0200
allow nested params when using Pages.GroupByParam and Pages.GroupByParamDate
Diffstat:
M resources/page/pagegroup_test.go | 60 +++++++++++++++++++++++++++++++
M resources/resource/resource_helper… | 5 +++--
2 files changed, 63 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/resources/page/pagegroup_test.go b/resources/page/pagegroup_test.go
@@ -55,6 +55,11 @@ func preparePageGroupTestPages(t *testing.T) Pages {
p.params["custom_param"] = src.param
p.params["custom_date"] = cast.ToTime(src.date)
p.params["custom_string_date"] = src.date
+ p.params["custom_object"] = map[string]any{
+ "param": src.param,
+ "date": cast.ToTime(src.date),
+ "string_date": src.date,
+ }
pages = append(pages, p)
}
return pages
@@ -252,6 +257,25 @@ func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
}
}
+func TestGroupByParamNested(t *testing.T) {
+ t.Parallel()
+ pages := preparePageGroupTestPages(t)
+
+ expect := PagesGroup{
+ {Key: "bar", Pages: Pages{pages[1], pages[3]}},
+ {Key: "baz", Pages: Pages{pages[4]}},
+ {Key: "foo", Pages: Pages{pages[0], pages[2]}},
+ }
+
+ groups, err := pages.GroupByParam("custom_object.param")
+ if err != nil {
+ t.Fatalf("Unable to make PagesGroup array: %s", err)
+ }
+ if !reflect.DeepEqual(groups, expect) {
+ t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
+ }
+}
+
func TestGroupByDate(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
@@ -372,6 +396,24 @@ func TestGroupByParamDate(t *testing.T) {
}
}
+func TestGroupByParamDateNested(t *testing.T) {
+ t.Parallel()
+ pages := preparePageGroupTestPages(t)
+ expect := PagesGroup{
+ {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
+ {Key: "2012-03", Pages: Pages{pages[3]}},
+ {Key: "2012-01", Pages: Pages{pages[1]}},
+ }
+
+ groups, err := pages.GroupByParamDate("custom_object.date", "2006-01")
+ if err != nil {
+ t.Fatalf("Unable to make PagesGroup array: %s", err)
+ }
+ if !reflect.DeepEqual(groups, expect) {
+ t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
+ }
+}
+
// https://github.com/gohugoio/hugo/issues/3983
func TestGroupByParamDateWithStringParams(t *testing.T) {
t.Parallel()
@@ -391,6 +433,24 @@ func TestGroupByParamDateWithStringParams(t *testing.T) {
}
}
+func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
+ t.Parallel()
+ pages := preparePageGroupTestPages(t)
+ expect := PagesGroup{
+ {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
+ {Key: "2012-03", Pages: Pages{pages[3]}},
+ {Key: "2012-01", Pages: Pages{pages[1]}},
+ }
+
+ groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01")
+ if err != nil {
+ t.Fatalf("Unable to make PagesGroup array: %s", err)
+ }
+ if !reflect.DeepEqual(groups, expect) {
+ t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
+ }
+}
+
func TestGroupByLastmod(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
(DIR) diff --git a/resources/resource/resource_helpers.go b/resources/resource/resource_helpers.go
@@ -14,6 +14,7 @@
package resource
import (
+ "github.com/gohugoio/hugo/common/maps"
"strings"
"time"
@@ -36,9 +37,9 @@ func GetParamToLower(r Resource, key string) any {
}
func getParam(r Resource, key string, stringToLower bool) any {
- v := r.Params()[strings.ToLower(key)]
+ v, err := maps.GetNestedParam(key, ".", r.Params())
- if v == nil {
+ if v == nil || err != nil {
return nil
}