File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,27 @@ module.exports = function(eleventyConfig) {
2828 return moment ( value ) . utc ( ) . format ( arg ) ;
2929 } ) ;
3030
31+ // This function takes an array of pages and sorts them by date updated.
32+ // If they don't have an "updated" date, it sorts them by date created instead.
33+ eleventyConfig . addFilter ( "sortByUpdated" , function ( value ) {
34+
35+ value . forEach ( function ( item ) {
36+ // If the page doesn't have a date updated, we'll sort it by it's date created.
37+ if ( typeof item . data . updated == 'undefined' ) {
38+ item . data . sortdate = item . date ;
39+ } else { // Otherwise, sort by date updated
40+ item . data . sortdate = item . data . updated ;
41+ } ;
42+ } ) ;
43+
44+ // Do the sort
45+ value . sort ( function ( a , b ) {
46+ return b . data . sortdate - a . data . sortdate ;
47+ } ) ;
48+
49+ return value ;
50+ } ) ;
51+
3152 eleventyConfig . addNunjucksFilter ( "split" , function ( value , arg ) {
3253 return value . split ( arg ) ;
3354 } ) ;
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ layout: layouts/base.njk
1111{% block content %}
1212{{ content | safe }}
1313<ul >
14- {% for page in collections [cat ] | reverse %}
14+ {% for page in collections [cat ] | sortByUpdated %}
1515 <li ><a href =" {{ page.url }}" >{{ page .data .title }} </a > <span class =" publish-date" >(published {{ page .date | utcDate (' YYYY-MM-DD' ) }} {% if page .data .updated %} , updated {{ page .data .updated | utcDate (' YYYY-MM-DD' ) }} {% endif %} )</span ></li >
1616{% endfor %}
1717</ul >
You can’t perform that action at this time.
0 commit comments