mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-25 22:00:58 +02:00
Make string sorting (e.g. ByTitle, ByLinkTitle and ByParam) language aware
Fixes #2180
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
"sort"
|
||||
|
||||
"github.com/gohugoio/hugo/compare"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
|
||||
"github.com/gohugoio/hugo/resources/page"
|
||||
)
|
||||
@@ -40,6 +41,15 @@ type Taxonomy map[string]page.WeightedPages
|
||||
// Important because you can't order a map.
|
||||
type OrderedTaxonomy []OrderedTaxonomyEntry
|
||||
|
||||
// getOneOPage returns one page in the taxonomy,
|
||||
// nil if there is none.
|
||||
func (t OrderedTaxonomy) getOneOPage() page.Page {
|
||||
if len(t) == 0 {
|
||||
return nil
|
||||
}
|
||||
return t[0].Pages()[0]
|
||||
}
|
||||
|
||||
// OrderedTaxonomyEntry is similar to an element of a Taxonomy, but with the key embedded (as name)
|
||||
// e.g: {Name: Technology, page.WeightedPages: TaxonomyPages}
|
||||
type OrderedTaxonomyEntry struct {
|
||||
@@ -72,11 +82,18 @@ func (i Taxonomy) TaxonomyArray() OrderedTaxonomy {
|
||||
|
||||
// Alphabetical returns an ordered taxonomy sorted by key name.
|
||||
func (i Taxonomy) Alphabetical() OrderedTaxonomy {
|
||||
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
||||
return compare.LessStrings(i1.Name, i2.Name)
|
||||
}
|
||||
|
||||
ia := i.TaxonomyArray()
|
||||
p := ia.getOneOPage()
|
||||
if p == nil {
|
||||
return ia
|
||||
}
|
||||
currentSite := p.Site().Current()
|
||||
coll := langs.GetCollator(currentSite.Language())
|
||||
coll.Lock()
|
||||
defer coll.Unlock()
|
||||
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
|
||||
return coll.CompareStrings(i1.Name, i2.Name) < 0
|
||||
}
|
||||
oiBy(name).Sort(ia)
|
||||
return ia
|
||||
}
|
||||
|
Reference in New Issue
Block a user