Add GroupByLastmod
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 19 Jun 2020 07:37:37 +0000 (09:37 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 19 Jun 2020 09:58:58 +0000 (11:58 +0200)
Fixes #7408

docs/content/en/templates/lists.md
resources/page/pagegroup.go
resources/page/pagegroup_test.go

index c2140b472967de8131acaaf0d6cb062aa5eb0745..be9df664cf0304a958d34294f90b7e87ca6c0e03 100644 (file)
@@ -424,7 +424,7 @@ In the above example, you may want `{{.Title}}` to point the `title` field you h
 ### 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>
@@ -438,6 +438,41 @@ In the above example, you may want `{{.Title}}` to point the `title` field you h
 {{ 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>
+{{ end }}
+{{< /code >}}
+
 ### By Page Parameter
 
 {{< code file="layouts/partials/by-page-param.html" >}}
index fbb6e7e5304822b589eba42b031c83fb7b39d218..2d37a5d4d122016f16b990f0ffcb2e766586bea1 100644 (file)
@@ -284,6 +284,20 @@ func (p Pages) GroupByExpiryDate(format string, order ...string) (PagesGroup, er
        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.
index 26a25c381afc351d85eced6cac70a0b9874a714e..fe6dbf94bdfe5328ba24f2b0d054cd411526ec84 100644 (file)
@@ -49,6 +49,7 @@ func preparePageGroupTestPages(t *testing.T) Pages {
                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)
@@ -378,6 +379,42 @@ func TestGroupByParamDate(t *testing.T) {
        }
 }
 
+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)
+       }
+}
+
 func TestGroupByParamDateInReverseOrder(t *testing.T) {
        t.Parallel()
        pages := preparePageGroupTestPages(t)