mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Add config options page.nextPrevSortOrder/nextPrevInSectionSortOrder
See #12776
This commit is contained in:
parent
53c0ddfcb4
commit
01008ba512
6 changed files with 132 additions and 1 deletions
|
@ -182,6 +182,9 @@ type Config struct {
|
||||||
// Pagination configuration.
|
// Pagination configuration.
|
||||||
Pagination config.Pagination `mapstructure:"-"`
|
Pagination config.Pagination `mapstructure:"-"`
|
||||||
|
|
||||||
|
// Page configuration.
|
||||||
|
Page config.PageConfig `mapstructure:"-"`
|
||||||
|
|
||||||
// Privacy configuration.
|
// Privacy configuration.
|
||||||
Privacy privacy.Config `mapstructure:"-"`
|
Privacy privacy.Config `mapstructure:"-"`
|
||||||
|
|
||||||
|
|
|
@ -327,6 +327,25 @@ var allDecoderSetups = map[string]decodeWeight{
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"page": {
|
||||||
|
key: "page",
|
||||||
|
decode: func(d decodeWeight, p decodeConfig) error {
|
||||||
|
p.c.Page = config.PageConfig{
|
||||||
|
NextPrevSortOrder: "desc",
|
||||||
|
NextPrevInSectionSortOrder: "desc",
|
||||||
|
}
|
||||||
|
if p.p.IsSet(d.key) {
|
||||||
|
if err := mapstructure.WeakDecode(p.p.Get(d.key), &p.c.Page); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
getCompiler: func(c *Config) configCompiler {
|
||||||
|
return &c.Page
|
||||||
|
},
|
||||||
|
},
|
||||||
"pagination": {
|
"pagination": {
|
||||||
key: "pagination",
|
key: "pagination",
|
||||||
decode: func(d decodeWeight, p decodeConfig) error {
|
decode: func(d decodeWeight, p decodeConfig) error {
|
||||||
|
|
|
@ -422,3 +422,18 @@ type Pagination struct {
|
||||||
// Whether to disable generation of alias for the first pagination page.
|
// Whether to disable generation of alias for the first pagination page.
|
||||||
DisableAliases bool
|
DisableAliases bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PageConfig configures the behavior of pages.
|
||||||
|
type PageConfig struct {
|
||||||
|
// Sort order for Page.Next and Page.Prev. Default "desc" (the default page sort order in Hugo).
|
||||||
|
NextPrevSortOrder string
|
||||||
|
|
||||||
|
// Sort order for Page.NextInSection and Page.PrevInSection. Default "desc".
|
||||||
|
NextPrevInSectionSortOrder string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *PageConfig) CompileConfig(loggers.Logger) error {
|
||||||
|
c.NextPrevInSectionSortOrder = strings.ToLower(c.NextPrevInSectionSortOrder)
|
||||||
|
c.NextPrevSortOrder = strings.ToLower(c.NextPrevSortOrder)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1600,6 +1600,9 @@ config:
|
||||||
term:
|
term:
|
||||||
- html
|
- html
|
||||||
- rss
|
- rss
|
||||||
|
page:
|
||||||
|
nextPrevInSectionSortOrder: desc
|
||||||
|
nextPrevSortOrder: desc
|
||||||
paginate: 0
|
paginate: 0
|
||||||
paginatePath: ""
|
paginatePath: ""
|
||||||
pagination:
|
pagination:
|
||||||
|
@ -1784,6 +1787,8 @@ config_helpers:
|
||||||
_merge: shallow
|
_merge: shallow
|
||||||
outputs:
|
outputs:
|
||||||
_merge: none
|
_merge: none
|
||||||
|
page:
|
||||||
|
_merge: none
|
||||||
pagination:
|
pagination:
|
||||||
_merge: none
|
_merge: none
|
||||||
params:
|
params:
|
||||||
|
|
|
@ -117,6 +117,9 @@ func (s *Site) prepareInits() {
|
||||||
|
|
||||||
s.init.prevNext = init.Branch(func(context.Context) (any, error) {
|
s.init.prevNext = init.Branch(func(context.Context) (any, error) {
|
||||||
regularPages := s.RegularPages()
|
regularPages := s.RegularPages()
|
||||||
|
if s.conf.Page.NextPrevSortOrder == "asc" {
|
||||||
|
regularPages = regularPages.Reverse()
|
||||||
|
}
|
||||||
for i, p := range regularPages {
|
for i, p := range regularPages {
|
||||||
np, ok := p.(nextPrevProvider)
|
np, ok := p.(nextPrevProvider)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -181,7 +184,11 @@ func (s *Site) prepareInits() {
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, section := range sections {
|
for _, section := range sections {
|
||||||
setNextPrev(section.RegularPages())
|
ps := section.RegularPages()
|
||||||
|
if s.conf.Page.NextPrevInSectionSortOrder == "asc" {
|
||||||
|
ps = ps.Reverse()
|
||||||
|
}
|
||||||
|
setNextPrev(ps)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
82
resources/page/pages_prev_next_integration_test.go
Normal file
82
resources/page/pages_prev_next_integration_test.go
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
// Copyright 2024 The Hugo Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
package page_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/hugolib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNextPrevConfig(t *testing.T) {
|
||||||
|
filesTemplate := `
|
||||||
|
-- hugo.toml --
|
||||||
|
-- content/mysection/_index.md --
|
||||||
|
-- content/mysection/p1.md --
|
||||||
|
---
|
||||||
|
title: "Page 1"
|
||||||
|
weight: 10
|
||||||
|
---
|
||||||
|
-- content/mysection/p2.md --
|
||||||
|
---
|
||||||
|
title: "Page 2"
|
||||||
|
weight: 20
|
||||||
|
---
|
||||||
|
-- content/mysection/p3.md --
|
||||||
|
---
|
||||||
|
title: "Page 3"
|
||||||
|
weight: 30
|
||||||
|
---
|
||||||
|
-- layouts/_default/single.html --
|
||||||
|
{{ .Title }}|Next: {{ with .Next}}{{ .Title}}{{ end }}|Prev: {{ with .Prev}}{{ .Title}}{{ end }}|NextInSection: {{ with .NextInSection}}{{ .Title}}{{ end }}|PrevInSection: {{ with .PrevInSection}}{{ .Title}}{{ end }}|
|
||||||
|
|
||||||
|
`
|
||||||
|
b := hugolib.Test(t, filesTemplate)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: |Prev: Page 2|NextInSection: |PrevInSection: Page 2|")
|
||||||
|
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 1|Prev: Page 3|NextInSection: Page 1|PrevInSection: Page 3|")
|
||||||
|
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: Page 2|Prev: |NextInSection: Page 2|PrevInSection: |")
|
||||||
|
|
||||||
|
files := strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml --
|
||||||
|
[page]
|
||||||
|
nextPrevSortOrder="aSc"
|
||||||
|
nextPrevInSectionSortOrder="asC"
|
||||||
|
`)
|
||||||
|
|
||||||
|
b = hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: Page 2|Prev: |NextInSection: Page 2|PrevInSection: |")
|
||||||
|
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 3|Prev: Page 1|NextInSection: Page 3|PrevInSection: Page 1|")
|
||||||
|
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: |Prev: Page 2|NextInSection: |PrevInSection: Page 2|")
|
||||||
|
|
||||||
|
files = strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml --
|
||||||
|
[page]
|
||||||
|
nextPrevSortOrder="aSc"
|
||||||
|
`)
|
||||||
|
|
||||||
|
b = hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: Page 2|Prev: |NextInSection: |PrevInSection: Page 2|")
|
||||||
|
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 3|Prev: Page 1|NextInSection: Page 1|PrevInSection: Page 3|")
|
||||||
|
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: |Prev: Page 2|NextInSection: Page 2|PrevInSection: |")
|
||||||
|
|
||||||
|
files = strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml --
|
||||||
|
[page]
|
||||||
|
nextPrevInSectionSortOrder="aSc"
|
||||||
|
`)
|
||||||
|
|
||||||
|
b = hugolib.Test(t, files)
|
||||||
|
|
||||||
|
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: |Prev: Page 2|NextInSection: Page 2|PrevInSection: |")
|
||||||
|
}
|
Loading…
Reference in a new issue