From 2402ba4887aeabe497cde796d6260c52bcfb2b7f Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Sun, 30 Sep 2018 00:09:09 -0500 Subject: [PATCH] Refactored latest posts and with a correct sort. Uses dates on posts rather than file creation dates (unreliable). --- .eleventy.js | 10 +++++++--- archive.njk | 4 ++-- index.njk | 6 +++--- posts/firstpost.md | 1 + posts/fourthpost.md | 1 + posts/secondpost.md | 1 + posts/thirdpost.md | 1 + 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 5996d4e..bfda7a6 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -9,11 +9,15 @@ module.exports = function(eleventyConfig) { eleventyConfig.addLayoutAlias("post", "layouts/post.njk"); 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. eleventyConfig.addFilter("head", (array, n) => { + if( n < 0 ) { + return array.slice(n); + } + return array.slice(0, n); }); @@ -24,8 +28,8 @@ module.exports = function(eleventyConfig) { // only content in the `posts/` directory eleventyConfig.addCollection("posts", function(collection) { - return collection.getAllSorted().filter(function(item) { - return item.inputPath.startsWith('./posts'); + return collection.getFilteredByGlob("./posts/*").sort(function(a, b) { + return a.date - b.date; }); }); diff --git a/archive.njk b/archive.njk index 235fd75..61c48d9 100644 --- a/archive.njk +++ b/archive.njk @@ -3,10 +3,10 @@ layout: layouts/home.njk tags: - nav navtitle: Archive -permalink: posts/index.html +permalink: /posts/ --- -

Blog post archive

+

Archive

{% set postslist = collections.posts %} {% include "postslist.njk" %} diff --git a/index.njk b/index.njk index db6baea..4903742 100644 --- a/index.njk +++ b/index.njk @@ -5,9 +5,9 @@ tags: navtitle: Home --- -

Latest 2 blog posts

+

Latest 3 Posts

-{% set postslist = collections.posts | head(2) %} +{% set postslist = collections.posts | head(-3) %} {% include "postslist.njk" %} -

More posts can be found in the blog archive.

+

More posts can be found in the archive.

diff --git a/posts/firstpost.md b/posts/firstpost.md index 8bd3e44..666a29a 100644 --- a/posts/firstpost.md +++ b/posts/firstpost.md @@ -1,5 +1,6 @@ --- title: This is my first post. +date: 2018-05-01 tags: - post - another-tag diff --git a/posts/fourthpost.md b/posts/fourthpost.md index 4ac6319..9107bcf 100644 --- a/posts/fourthpost.md +++ b/posts/fourthpost.md @@ -1,5 +1,6 @@ --- title: This is my fourth post. +date: 2018-09-30 tags: second-tag layout: layouts/post.njk --- diff --git a/posts/secondpost.md b/posts/secondpost.md index c7afa5f..bee6fcc 100644 --- a/posts/secondpost.md +++ b/posts/secondpost.md @@ -1,5 +1,6 @@ --- title: This is my second post. +date: 2018-07-04 tags: - post - number-2 diff --git a/posts/thirdpost.md b/posts/thirdpost.md index 04202aa..6688b55 100644 --- a/posts/thirdpost.md +++ b/posts/thirdpost.md @@ -1,5 +1,6 @@ --- title: This is my third post. +date: 2018-08-24 tags: - post - second-tag