mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Don't fail when calling Paginate with an empty pages.PagesGroup
Fixes #10802
This commit is contained in:
parent
0f01bd4637
commit
34a86e13f6
3 changed files with 35 additions and 9 deletions
|
@ -136,3 +136,29 @@ weight: %d
|
||||||
b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
|
b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
|
||||||
b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
|
b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 10802
|
||||||
|
func TestPaginatorEmptyPageGroups(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
files := `
|
||||||
|
-- config.toml --
|
||||||
|
baseURL = "https://example.com/"
|
||||||
|
-- content/p1.md --
|
||||||
|
-- content/p2.md --
|
||||||
|
-- layouts/index.html --
|
||||||
|
{{ $empty := site.RegularPages | complement site.RegularPages }}
|
||||||
|
Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
|
||||||
|
{{ $pgs := $empty.GroupByPublishDate "January 2006" }}
|
||||||
|
{{ $pag := .Paginate $pgs }}
|
||||||
|
Len Pag: {{ len $pag.Pages }}
|
||||||
|
`
|
||||||
|
b := NewIntegrationTestBuilder(
|
||||||
|
IntegrationTestConfig{
|
||||||
|
T: t,
|
||||||
|
TxtarString: files,
|
||||||
|
},
|
||||||
|
).Build()
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
|
||||||
|
}
|
||||||
|
|
|
@ -430,14 +430,14 @@ func (psg PagesGroup) ProbablyEq(other any) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToPagesGroup tries to convert seq into a PagesGroup.
|
// ToPagesGroup tries to convert seq into a PagesGroup.
|
||||||
func ToPagesGroup(seq any) (PagesGroup, error) {
|
func ToPagesGroup(seq any) (PagesGroup, bool, error) {
|
||||||
switch v := seq.(type) {
|
switch v := seq.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
return nil, nil
|
return nil, true, nil
|
||||||
case PagesGroup:
|
case PagesGroup:
|
||||||
return v, nil
|
return v, true, nil
|
||||||
case []PageGroup:
|
case []PageGroup:
|
||||||
return PagesGroup(v), nil
|
return PagesGroup(v), true, nil
|
||||||
case []any:
|
case []any:
|
||||||
l := len(v)
|
l := len(v)
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
|
@ -450,12 +450,12 @@ func ToPagesGroup(seq any) (PagesGroup, error) {
|
||||||
if pg, ok := ipg.(PageGroup); ok {
|
if pg, ok := ipg.(PageGroup); ok {
|
||||||
pagesGroup[i] = pg
|
pagesGroup[i] = pg
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
|
return nil, false, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pagesGroup, nil
|
return pagesGroup, true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -277,11 +277,11 @@ func Paginate(td TargetPathDescriptor, seq any, pagerSize int) (*Paginator, erro
|
||||||
|
|
||||||
var paginator *Paginator
|
var paginator *Paginator
|
||||||
|
|
||||||
groups, err := ToPagesGroup(seq)
|
groups, ok, err := ToPagesGroup(seq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if groups != nil {
|
if ok {
|
||||||
paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
|
paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
|
||||||
} else {
|
} else {
|
||||||
pages, err := ToPages(seq)
|
pages, err := ToPages(seq)
|
||||||
|
|
Loading…
Reference in a new issue