| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
package page_test |
| 14 |
|
| 15 |
import ( |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib" |
| 20 |
) |
| 21 |
|
| 22 |
func TestNextPrevConfig(t *testing.T) { |
| 23 |
filesTemplate := ` |
| 24 |
-- hugo.toml -- |
| 25 |
-- content/mysection/_index.md -- |
| 26 |
-- content/mysection/p1.md -- |
| 27 |
--- |
| 28 |
title: "Page 1" |
| 29 |
weight: 10 |
| 30 |
--- |
| 31 |
-- content/mysection/p2.md -- |
| 32 |
--- |
| 33 |
title: "Page 2" |
| 34 |
weight: 20 |
| 35 |
--- |
| 36 |
-- content/mysection/p3.md -- |
| 37 |
--- |
| 38 |
title: "Page 3" |
| 39 |
weight: 30 |
| 40 |
--- |
| 41 |
-- layouts/single.html -- |
| 42 |
{{ .Title }}|Next: {{ with .Next}}{{ .Title}}{{ end }}|Prev: {{ with .Prev}}{{ .Title}}{{ end }}|NextInSection: {{ with .NextInSection}}{{ .Title}}{{ end }}|PrevInSection: {{ with .PrevInSection}}{{ .Title}}{{ end }}| |
| 43 |
|
| 44 |
` |
| 45 |
b := hugolib.Test(t, filesTemplate) |
| 46 |
|
| 47 |
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: |Prev: Page 2|NextInSection: |PrevInSection: Page 2|") |
| 48 |
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 1|Prev: Page 3|NextInSection: Page 1|PrevInSection: Page 3|") |
| 49 |
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: Page 2|Prev: |NextInSection: Page 2|PrevInSection: |") |
| 50 |
|
| 51 |
files := strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml -- |
| 52 |
[page] |
| 53 |
nextPrevSortOrder="aSc" |
| 54 |
nextPrevInSectionSortOrder="asC" |
| 55 |
`) |
| 56 |
|
| 57 |
b = hugolib.Test(t, files) |
| 58 |
|
| 59 |
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: Page 2|Prev: |NextInSection: Page 2|PrevInSection: |") |
| 60 |
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 3|Prev: Page 1|NextInSection: Page 3|PrevInSection: Page 1|") |
| 61 |
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: |Prev: Page 2|NextInSection: |PrevInSection: Page 2|") |
| 62 |
|
| 63 |
files = strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml -- |
| 64 |
[page] |
| 65 |
nextPrevSortOrder="aSc" |
| 66 |
`) |
| 67 |
|
| 68 |
b = hugolib.Test(t, files) |
| 69 |
|
| 70 |
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: Page 2|Prev: |NextInSection: |PrevInSection: Page 2|") |
| 71 |
b.AssertFileContent("public/mysection/p2/index.html", "Page 2|Next: Page 3|Prev: Page 1|NextInSection: Page 1|PrevInSection: Page 3|") |
| 72 |
b.AssertFileContent("public/mysection/p3/index.html", "Page 3|Next: |Prev: Page 2|NextInSection: Page 2|PrevInSection: |") |
| 73 |
|
| 74 |
files = strings.ReplaceAll(filesTemplate, "-- hugo.toml --", `-- hugo.toml -- |
| 75 |
[page] |
| 76 |
nextPrevInSectionSortOrder="aSc" |
| 77 |
`) |
| 78 |
|
| 79 |
b = hugolib.Test(t, files) |
| 80 |
|
| 81 |
b.AssertFileContent("public/mysection/p1/index.html", "Page 1|Next: |Prev: Page 2|NextInSection: Page 2|PrevInSection: |") |
| 82 |
} |