Skip to content

Commit c498d0f

Browse files
committed
Use the page path and not the backing filename as the last resort in the default sort
This should: 1. Fix some (rare) tiebreaker issues when sorting pages from multiple content adapters. 2. Improve the sorting for pages without a backing file.
1 parent 521911a commit c498d0f

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

common/paths/pathparser_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func TestParse(t *testing.T) {
165165
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"})
166166
c.Assert(p.Base(), qt.Equals, "/a/b.a.b.txt")
167167
c.Assert(p.BaseNoLeadingSlash(), qt.Equals, "a/b.a.b.txt")
168+
c.Assert(p.Path(), qt.Equals, "/a/b.a.b.no.txt")
168169
c.Assert(p.PathNoLang(), qt.Equals, "/a/b.a.b.txt")
169170
c.Assert(p.Ext(), qt.Equals, "txt")
170171
c.Assert(p.PathNoIdentifier(), qt.Equals, "/a/b.a.b")
@@ -220,6 +221,7 @@ func TestParse(t *testing.T) {
220221
c.Assert(p.NameNoExt(), qt.Equals, "index.no")
221222
c.Assert(p.NameNoIdentifier(), qt.Equals, "index")
222223
c.Assert(p.NameNoLang(), qt.Equals, "index.md")
224+
c.Assert(p.Path(), qt.Equals, "/a/b/index.no.md")
223225
c.Assert(p.PathNoLang(), qt.Equals, "/a/b/index.md")
224226
c.Assert(p.Section(), qt.Equals, "a")
225227
},

hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ Single: {{ .Title }}|{{ .Content }}|
382382
}
383383
}
384384

385+
func TestPagesFromGoTmplDefaultPageSort(t *testing.T) {
386+
t.Parallel()
387+
files := `
388+
-- hugo.toml --
389+
defaultContentLanguage = "en"
390+
-- layouts/index.html --
391+
{{ range site.RegularPages }}{{ .RelPermalink }}|{{ end}}
392+
-- content/_content.gotmpl --
393+
{{ $.AddPage (dict "kind" "page" "path" "docs/_p22" "title" "A" ) }}
394+
{{ $.AddPage (dict "kind" "page" "path" "docs/p12" "title" "A" ) }}
395+
{{ $.AddPage (dict "kind" "page" "path" "docs/_p12" "title" "A" ) }}
396+
-- content/docs/_content.gotmpl --
397+
{{ $.AddPage (dict "kind" "page" "path" "_p21" "title" "A" ) }}
398+
{{ $.AddPage (dict "kind" "page" "path" "p11" "title" "A" ) }}
399+
{{ $.AddPage (dict "kind" "page" "path" "_p11" "title" "A" ) }}
400+
`
401+
402+
b := hugolib.Test(t, files)
403+
404+
b.AssertFileContent("public/index.html", "/docs/_p11/|/docs/_p12/|/docs/_p21/|/docs/_p22/|/docs/p11/|/docs/p12/|")
405+
}
406+
385407
func TestPagesFromGoTmplEnableAllLanguages(t *testing.T) {
386408
t.Parallel()
387409

resources/page/pages_sort.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ var (
9090
if w01 != w02 && w01 != -1 && w02 != -1 {
9191
return w01 < w02
9292
}
93+
9394
if p1.Weight() == p2.Weight() {
9495
if p1.Date().Unix() == p2.Date().Unix() {
9596
c := collatorStringCompare(func(p Page) string { return p.LinkTitle() }, p1, p2)
9697
if c == 0 {
97-
if p1.File() == nil || p2.File() == nil {
98-
return p1.File() == nil
99-
}
100-
return compare.LessStrings(p1.File().Filename(), p2.File().Filename())
98+
// This is the full normalized path, which will contain extension and any language code preserved,
99+
// which is what we want for sorting.
100+
return compare.LessStrings(p1.PathInfo().Path(), p2.PathInfo().Path())
101101
}
102102
return c < 0
103103
}

0 commit comments

Comments
 (0)