mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
82abca32fa
commit
6ff435aa3f
2 changed files with 47 additions and 8 deletions
|
@ -21,6 +21,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cast"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/common/collections"
|
"github.com/gohugoio/hugo/common/collections"
|
||||||
"github.com/gohugoio/hugo/compare"
|
"github.com/gohugoio/hugo/compare"
|
||||||
|
|
||||||
|
@ -225,6 +227,10 @@ func (p Pages) groupByDateField(sorter func(p Pages) Pages, formatter func(p Pag
|
||||||
sp = sp.Reverse()
|
sp = sp.Reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sp == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
date := formatter(sp[0].(Page))
|
date := formatter(sp[0].(Page))
|
||||||
var r []PageGroup
|
var r []PageGroup
|
||||||
r = append(r, PageGroup{Key: date, Pages: make(Pages, 0)})
|
r = append(r, PageGroup{Key: date, Pages: make(Pages, 0)})
|
||||||
|
@ -303,23 +309,36 @@ func (p Pages) GroupByLastmod(format string, order ...string) (PagesGroup, error
|
||||||
// Valid values for order is asc, desc, rev and reverse.
|
// Valid values for order is asc, desc, rev and reverse.
|
||||||
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
// For valid format strings, see https://golang.org/pkg/time/#Time.Format
|
||||||
func (p Pages) GroupByParamDate(key string, format string, order ...string) (PagesGroup, error) {
|
func (p Pages) GroupByParamDate(key string, format string, order ...string) (PagesGroup, error) {
|
||||||
sorter := func(p Pages) Pages {
|
// Cache the dates.
|
||||||
|
dates := make(map[Page]time.Time)
|
||||||
|
|
||||||
|
sorter := func(pages Pages) Pages {
|
||||||
var r Pages
|
var r Pages
|
||||||
for _, e := range p {
|
|
||||||
param := resource.GetParamToLower(e, key)
|
for _, p := range pages {
|
||||||
if _, ok := param.(time.Time); ok {
|
param := resource.GetParamToLower(p, key)
|
||||||
r = append(r, e)
|
var t time.Time
|
||||||
|
|
||||||
|
if param != nil {
|
||||||
|
var ok bool
|
||||||
|
if t, ok = param.(time.Time); !ok {
|
||||||
|
// Probably a string. Try to convert it to time.Time.
|
||||||
|
t = cast.ToTime(param)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dates[p] = t
|
||||||
|
r = append(r, p)
|
||||||
|
}
|
||||||
|
|
||||||
pdate := func(p1, p2 Page) bool {
|
pdate := func(p1, p2 Page) bool {
|
||||||
p1p, p2p := p1.(Page), p2.(Page)
|
return dates[p1].Unix() < dates[p2].Unix()
|
||||||
return resource.GetParamToLower(p1p, key).(time.Time).Unix() < resource.GetParamToLower(p2p, key).(time.Time).Unix()
|
|
||||||
}
|
}
|
||||||
pageBy(pdate).Sort(r)
|
pageBy(pdate).Sort(r)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
formatter := func(p Page) string {
|
formatter := func(p Page) string {
|
||||||
return resource.GetParamToLower(p, key).(time.Time).Format(format)
|
return dates[p].Format(format)
|
||||||
}
|
}
|
||||||
return p.groupByDateField(sorter, formatter, order...)
|
return p.groupByDateField(sorter, formatter, order...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ func preparePageGroupTestPages(t *testing.T) Pages {
|
||||||
p.lastMod = cast.ToTime(src.date).AddDate(3, 0, 0)
|
p.lastMod = cast.ToTime(src.date).AddDate(3, 0, 0)
|
||||||
p.params["custom_param"] = src.param
|
p.params["custom_param"] = src.param
|
||||||
p.params["custom_date"] = cast.ToTime(src.date)
|
p.params["custom_date"] = cast.ToTime(src.date)
|
||||||
|
p.params["custom_string_date"] = src.date
|
||||||
pages = append(pages, p)
|
pages = append(pages, p)
|
||||||
}
|
}
|
||||||
return pages
|
return pages
|
||||||
|
@ -379,6 +380,25 @@ func TestGroupByParamDate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/gohugoio/hugo/issues/3983
|
||||||
|
func TestGroupByParamDateWithStringParams(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_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) {
|
func TestGroupByLastmod(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
pages := preparePageGroupTestPages(t)
|
pages := preparePageGroupTestPages(t)
|
||||||
|
|
Loading…
Reference in a new issue