hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit a9a93d082d8640684b7fd0076c64ea808ea7f762
parent b09a40333f382cc1034d2eda856230258ab6b8cc
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Wed, 28 Nov 2018 12:28:24 +0100

hugolib: Fall back to title in ByLinkTitle sort

Fixes #4953

Diffstat:
Mhugolib/page.go | 9+++++++++
Mhugolib/pageSort.go | 2+-
Mhugolib/pageSort_test.go | 32++++++++++++++++++++++++++++++++
3 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/hugolib/page.go b/hugolib/page.go
@@ -18,6 +18,7 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"math/rand"
 	"reflect"
 
 	"github.com/gohugoio/hugo/common/maps"
@@ -517,6 +518,14 @@ func (ps Pages) String() string {
 	return fmt.Sprintf("Pages(%d)", len(ps))
 }
 
+// Used in tests.
+func (ps Pages) shuffle() {
+	for i := range ps {
+		j := rand.Intn(i + 1)
+		ps[i], ps[j] = ps[j], ps[i]
+	}
+}
+
 func (ps Pages) findPagePosByFilename(filename string) int {
 	for i, x := range ps {
 		if x.Filename() == filename {
diff --git a/hugolib/pageSort.go b/hugolib/pageSort.go
@@ -155,7 +155,7 @@ func (p Pages) ByLinkTitle() Pages {
 	const key = "pageSort.ByLinkTitle"
 
 	linkTitle := func(p1, p2 *Page) bool {
-		return p1.linkTitle < p2.linkTitle
+		return p1.LinkTitle() < p2.LinkTitle()
 	}
 
 	pages, _ := spc.get(key, pageBy(linkTitle).Sort, p)
diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go
@@ -20,6 +20,7 @@ import (
 	"time"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 )
 
 func TestDefaultSort(t *testing.T) {
@@ -58,6 +59,37 @@ func TestDefaultSort(t *testing.T) {
 	assert.Equal(t, "cl", p[2].LinkTitle())
 }
 
+// https://github.com/gohugoio/hugo/issues/4953
+func TestSortByLinkTitle(t *testing.T) {
+	t.Parallel()
+	assert := require.New(t)
+	s := newTestSite(t)
+	pages := createSortTestPages(s, 6)
+
+	for i, p := range pages {
+		if i < 5 {
+			p.title = fmt.Sprintf("title%d", i)
+		}
+
+		if i > 2 {
+			p.linkTitle = fmt.Sprintf("linkTitle%d", i)
+		}
+	}
+
+	pages.shuffle()
+
+	bylt := pages.ByLinkTitle()
+
+	for i, p := range bylt {
+		msg := fmt.Sprintf("test: %d", i)
+		if i < 3 {
+			assert.Equal(fmt.Sprintf("linkTitle%d", i+3), p.LinkTitle(), msg)
+		} else {
+			assert.Equal(fmt.Sprintf("title%d", i-3), p.LinkTitle(), msg)
+		}
+	}
+}
+
 func TestSortByN(t *testing.T) {
 	t.Parallel()
 	s := newTestSite(t)