diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-04-10 20:30:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-04-12 13:24:16 +0200 |
commit | 627eed1d620910f494056330733db6c6187b8fe9 (patch) | |
tree | a136a398e8f85b10d3de7b1781825453886435ed /hugolib | |
parent | 82ba634ed90186c756189a79b637559b28dd363e (diff) |
Make string sorting (e.g. ByTitle, ByLinkTitle and ByParam) language aware
Fixes #2180
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/site.go | 5 | ||||
-rw-r--r-- | hugolib/taxonomy.go | 23 |
2 files changed, 25 insertions, 3 deletions
diff --git a/hugolib/site.go b/hugolib/site.go index d7b5cb64e..bbabf91a3 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -739,7 +739,12 @@ func (s *SiteInfo) Sites() page.Sites { } // Current returns the currently rendered Site. +// If that isn't set yet, which is the situation before we start rendering, +// if will return the Site itself. func (s *SiteInfo) Current() page.Site { + if s.s.h.currentSite == nil { + return s + } return s.s.h.currentSite.Info } diff --git a/hugolib/taxonomy.go b/hugolib/taxonomy.go index e3f033109..6b3c2b961 100644 --- a/hugolib/taxonomy.go +++ b/hugolib/taxonomy.go @@ -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 { + 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 compare.LessStrings(i1.Name, i2.Name) + return coll.CompareStrings(i1.Name, i2.Name) < 0 } - - ia := i.TaxonomyArray() oiBy(name).Sort(ia) return ia } |