]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Add config options page.nextPrevSortOrder/nextPrevInSectionSortOrder
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 17 Aug 2024 13:16:09 +0000 (15:16 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 17 Aug 2024 14:47:50 +0000 (16:47 +0200)
See #12776

config/allconfig/allconfig.go
config/allconfig/alldecoders.go
config/commonConfig.go
docs/data/docs.yaml
hugolib/site.go
resources/page/pages_prev_next_integration_test.go [new file with mode: 0644]

index afa72fb11ac2c9850a6c94d13a526f021f789dec..802000f974008b2afd7b3a5bd21dc30211e5d3a8 100644 (file)
@@ -182,6 +182,9 @@ type Config struct {
        // Pagination configuration.
        Pagination config.Pagination `mapstructure:"-"`
 
+       // Page configuration.
+       Page config.PageConfig `mapstructure:"-"`
+
        // Privacy configuration.
        Privacy privacy.Config `mapstructure:"-"`
 
index c09aa0ff0a2f49d1b0e4d52910066b1549d952f1..45cdd0de6a7476a2f957931c762991c2f5cfd800 100644 (file)
@@ -327,6 +327,25 @@ var allDecoderSetups = map[string]decodeWeight{
                        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": {
                key: "pagination",
                decode: func(d decodeWeight, p decodeConfig) error {
index a615c51a76ec0c8eec29e8eeb7807737084a4834..9dea4a2fcea2c0a779fe8102038bdcca70922dac 100644 (file)
@@ -422,3 +422,18 @@ type Pagination struct {
        // Whether to disable generation of alias for the first pagination page.
        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
+}
index e399426b6f5115020e9c05fe4140d644d9f6f8f6..7ac76ee3919ca1e795eb382a47463362a6d214b9 100644 (file)
@@ -1600,6 +1600,9 @@ config:
     term:
     - html
     - rss
+  page:
+    nextPrevInSectionSortOrder: desc
+    nextPrevSortOrder: desc
   paginate: 0
   paginatePath: ""
   pagination:
@@ -1784,6 +1787,8 @@ config_helpers:
       _merge: shallow
     outputs:
       _merge: none
+    page:
+      _merge: none
     pagination:
       _merge: none
     params:
index bbf110d1d8cb5acd4f4e4177ff3f530c62aa3b76..a93bbdbe63b8cae2fac0e3c3cc8302714e02ffad 100644 (file)
@@ -117,6 +117,9 @@ func (s *Site) prepareInits() {
 
        s.init.prevNext = init.Branch(func(context.Context) (any, error) {
                regularPages := s.RegularPages()
+               if s.conf.Page.NextPrevSortOrder == "asc" {
+                       regularPages = regularPages.Reverse()
+               }
                for i, p := range regularPages {
                        np, ok := p.(nextPrevProvider)
                        if !ok {
@@ -181,7 +184,11 @@ func (s *Site) prepareInits() {
                )
 
                for _, section := range sections {
-                       setNextPrev(section.RegularPages())
+                       ps := section.RegularPages()
+                       if s.conf.Page.NextPrevInSectionSortOrder == "asc" {
+                               ps = ps.Reverse()
+                       }
+                       setNextPrev(ps)
                }
 
                return nil, nil
diff --git a/resources/page/pages_prev_next_integration_test.go b/resources/page/pages_prev_next_integration_test.go
new file mode 100644 (file)
index 0000000..d61d23c
--- /dev/null
@@ -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: |")
+}