ref: 82abca32fa8791e526edd820c32c0b8f9d8e0e78
parent: fc045e12a953aac88b942c25b958c5c0554b252b
	author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
	date: Fri Jun 19 05:37:37 EDT 2020
	
Add GroupByLastmod Fixes #7408
--- a/docs/content/en/templates/lists.md
+++ b/docs/content/en/templates/lists.md
@@ -424,7 +424,7 @@
### By Publish Date
 {{< code file="layouts/partials/by-page-publish-date.html" >}}-<!-- Groups content by month according to the "publishdate" field in front matter -->
+<!-- Groups content by month according to the "publishDate" field in front matter -->
 {{ range .Pages.GroupByPublishDate "2006-01" }} <h3>{{ .Key }}</h3><ul>
@@ -432,6 +432,41 @@
<li>
     <a href="{{ .Permalink }}">{{ .Title }}</a>     <div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div>+ </li>
+    {{ end }}+</ul>
+{{ end }}+{{< /code >}}+
+
+### By Lastmod
+
+{{< code file="layouts/partials/by-page-lastmod.html" >}}+<!-- Groups content by month according to the "lastMod" field in front matter -->
+{{ range .Pages.GroupByLastmod "2006-01" }}+<h3>{{ .Key }}</h3>+<ul>
+    {{ range .Pages }}+ <li>
+    <a href="{{ .Permalink }}">{{ .Title }}</a>+    <div class="meta">{{ .Lastmod.Format "Mon, Jan 2, 2006" }}</div>+ </li>
+    {{ end }}+</ul>
+{{ end }}+{{< /code >}}+
+### By Expiry Date
+
+{{< code file="layouts/partials/by-page-expiry-date.html" >}}+<!-- Groups content by month according to the "expiryDate" field in front matter -->
+{{ range .Pages.GroupByExpiryDate "2006-01" }}+<h3>{{ .Key }}</h3>+<ul>
+    {{ range .Pages }}+ <li>
+    <a href="{{ .Permalink }}">{{ .Title }}</a>+    <div class="meta">{{ .ExpiryDate.Format "Mon, Jan 2, 2006" }}</div></li>
     {{ end }}</ul>
--- a/resources/page/pagegroup.go
+++ b/resources/page/pagegroup.go
@@ -284,6 +284,20 @@
return p.groupByDateField(sorter, formatter, order...)
}
+// GroupByLastmod groups by the given page's Lastmod value in
+// the given format and with the given order.
+// Valid values for order is asc, desc, rev and reverse.
+// For valid format strings, see https://golang.org/pkg/time/#Time.Format
+func (p Pages) GroupByLastmod(format string, order ...string) (PagesGroup, error) {+	sorter := func(p Pages) Pages {+ return p.ByLastmod()
+ }
+	formatter := func(p Page) string {+ return p.Lastmod().Format(format)
+ }
+ return p.groupByDateField(sorter, formatter, order...)
+}
+
// GroupByParamDate groups by a date set as a param on the page in
// the given format and with the given order.
// Valid values for order is asc, desc, rev and reverse.
--- a/resources/page/pagegroup_test.go
+++ b/resources/page/pagegroup_test.go
@@ -49,6 +49,7 @@
p.date = cast.ToTime(src.date)
p.pubDate = cast.ToTime(src.date)
p.expiryDate = cast.ToTime(src.date)
+ p.lastMod = cast.ToTime(src.date).AddDate(3, 0, 0)
p.params["custom_param"] = src.param
p.params["custom_date"] = cast.ToTime(src.date)
pages = append(pages, p)
@@ -375,6 +376,42 @@
}
 	if !reflect.DeepEqual(groups, expect) { 		t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)+ }
+}
+
+func TestGroupByLastmod(t *testing.T) {+ t.Parallel()
+ pages := preparePageGroupTestPages(t)
+	expect := PagesGroup{+		{Key: "2015-04", Pages: Pages{pages[4], pages[2], pages[0]}},+		{Key: "2015-03", Pages: Pages{pages[3]}},+		{Key: "2015-01", Pages: Pages{pages[1]}},+ }
+
+	groups, err := pages.GroupByLastmod("2006-01")+	if err != nil {+		t.Fatalf("Unable to make PagesGroup array: %s", err)+ }
+	if !reflect.DeepEqual(groups, expect) {+		t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)+ }
+}
+
+func TestGroupByLastmodInReverseOrder(t *testing.T) {+ t.Parallel()
+ pages := preparePageGroupTestPages(t)
+	expect := PagesGroup{+		{Key: "2015-01", Pages: Pages{pages[1]}},+		{Key: "2015-03", Pages: Pages{pages[3]}},+		{Key: "2015-04", Pages: Pages{pages[0], pages[2], pages[4]}},+ }
+
+	groups, err := pages.GroupByLastmod("2006-01", "asc")+	if err != nil {+		t.Fatalf("Unable to make PagesGroup array: %s", err)+ }
+	if !reflect.DeepEqual(groups, expect) {+		t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups)}
}
--
⑨