From 8d898ad6672e0ccb62c5a29b6fccab24d980f104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 27 May 2019 22:57:57 +0200 Subject: tpl/collections: Unwrap any interface value in sort and where Hugo `0.55.0` introduced some new interface types for `Page` etc. This worked great in general, but there were cases where this would fail in `where` and `sort`. One such example would be sorting by `MenuItem.Page.Date` where `Page` on `MenuItem` was a small subset of the bigger `page.Page` interface. This commit fixes that by unwrapping such interface values. Fixes #5989 --- hugolib/menu_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'hugolib/menu_test.go') diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index f1db3cb3a..4a2b17603 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -222,3 +222,54 @@ menu: "main" b.AssertFileContent("public/index.html", "AMP and HTML|/blog/html-amp/|AMP only|/amp/blog/amp/|HTML only|/blog/html/|Home Sweet Home|/|") b.AssertFileContent("public/amp/index.html", "AMP and HTML|/amp/blog/html-amp/|AMP only|/amp/blog/amp/|HTML only|/blog/html/|Home Sweet Home|/amp/|") } + +// https://github.com/gohugoio/hugo/issues/5989 +func TestMenuPageSortByDate(t *testing.T) { + + b := newTestSitesBuilder(t).WithSimpleConfigFile() + + b.WithContent("blog/a.md", ` +--- +Title: A +date: 2019-01-01 +menu: + main: + identifier: "a" + weight: 1 +--- + +`) + + b.WithContent("blog/b.md", ` +--- +Title: B +date: 2018-01-02 +menu: + main: + parent: "a" + weight: 100 +--- + +`) + + b.WithContent("blog/c.md", ` +--- +Title: C +date: 2019-01-03 +menu: + main: + parent: "a" + weight: 10 +--- + +`) + + b.WithTemplatesAdded("index.html", `{{ range .Site.Menus.main }}{{ .Title }}|Children: +{{- $children := sort .Children ".Page.Date" "desc" }}{{ range $children }}{{ .Title }}|{{ end }}{{ end }} + +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", "A|Children:C|B|") +} -- cgit v1.2.3