Refactored latest posts and with a correct sort. Uses dates on posts rather than file creation dates (unreliable).

This commit is contained in:
Zach Leatherman 2018-09-30 00:09:09 -05:00
parent e2028fd551
commit 2402ba4887
7 changed files with 16 additions and 8 deletions

View File

@ -9,11 +9,15 @@ module.exports = function(eleventyConfig) {
eleventyConfig.addLayoutAlias("post", "layouts/post.njk"); eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
eleventyConfig.addFilter("readableDate", dateObj => { eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy"); return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat("dd LLL yyyy");
}); });
// Get the first `n` elements of a collection. // Get the first `n` elements of a collection.
eleventyConfig.addFilter("head", (array, n) => { eleventyConfig.addFilter("head", (array, n) => {
if( n < 0 ) {
return array.slice(n);
}
return array.slice(0, n); return array.slice(0, n);
}); });
@ -24,8 +28,8 @@ module.exports = function(eleventyConfig) {
// only content in the `posts/` directory // only content in the `posts/` directory
eleventyConfig.addCollection("posts", function(collection) { eleventyConfig.addCollection("posts", function(collection) {
return collection.getAllSorted().filter(function(item) { return collection.getFilteredByGlob("./posts/*").sort(function(a, b) {
return item.inputPath.startsWith('./posts'); return a.date - b.date;
}); });
}); });

View File

@ -3,10 +3,10 @@ layout: layouts/home.njk
tags: tags:
- nav - nav
navtitle: Archive navtitle: Archive
permalink: posts/index.html permalink: /posts/
--- ---
<h1>Blog post archive</h1> <h1>Archive</h1>
{% set postslist = collections.posts %} {% set postslist = collections.posts %}
{% include "postslist.njk" %} {% include "postslist.njk" %}

View File

@ -5,9 +5,9 @@ tags:
navtitle: Home navtitle: Home
--- ---
<h1>Latest 2 blog posts</h1> <h1>Latest 3 Posts</h1>
{% set postslist = collections.posts | head(2) %} {% set postslist = collections.posts | head(-3) %}
{% include "postslist.njk" %} {% include "postslist.njk" %}
<p>More posts can be found in <a href="/posts/">the blog archive</a>.</p> <p>More posts can be found in <a href="/posts/">the archive</a>.</p>

View File

@ -1,5 +1,6 @@
--- ---
title: This is my first post. title: This is my first post.
date: 2018-05-01
tags: tags:
- post - post
- another-tag - another-tag

View File

@ -1,5 +1,6 @@
--- ---
title: This is my fourth post. title: This is my fourth post.
date: 2018-09-30
tags: second-tag tags: second-tag
layout: layouts/post.njk layout: layouts/post.njk
--- ---

View File

@ -1,5 +1,6 @@
--- ---
title: This is my second post. title: This is my second post.
date: 2018-07-04
tags: tags:
- post - post
- number-2 - number-2

View File

@ -1,5 +1,6 @@
--- ---
title: This is my third post. title: This is my third post.
date: 2018-08-24
tags: tags:
- post - post
- second-tag - second-tag