Skip to content

Commit 0e68643

Browse files
committed
Implemented sorting pages by date updated rather than date created!
Created a custom filter! It's awesome! Wheeeeee!
1 parent 869a3ec commit 0e68643

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

.eleventy.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

_includes/layouts/listing.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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>

0 commit comments

Comments
 (0)