Skip to content

Commit

Permalink
Refactored latest posts and with a correct sort. Uses dates on posts …
Browse files Browse the repository at this point in the history
…rather than file creation dates (unreliable).
  • Loading branch information
zachleat committed Sep 30, 2018
1 parent e2028fd commit 2402ba4
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
10 changes: 7 additions & 3 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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;
});
});

Expand Down
4 changes: 2 additions & 2 deletions archive.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ layout: layouts/home.njk
tags:
- nav
navtitle: Archive
permalink: posts/index.html
permalink: /posts/
---

<h1>Blog post archive</h1>
<h1>Archive</h1>

{% set postslist = collections.posts %}
{% include "postslist.njk" %}
6 changes: 3 additions & 3 deletions index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ tags:
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" %}

<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>
1 change: 1 addition & 0 deletions posts/firstpost.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: This is my first post.
date: 2018-05-01
tags:
- post
- another-tag
Expand Down
1 change: 1 addition & 0 deletions posts/fourthpost.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: This is my fourth post.
date: 2018-09-30
tags: second-tag
layout: layouts/post.njk
---
Expand Down
1 change: 1 addition & 0 deletions posts/secondpost.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: This is my second post.
date: 2018-07-04
tags:
- post
- number-2
Expand Down
1 change: 1 addition & 0 deletions posts/thirdpost.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: This is my third post.
date: 2018-08-24
tags:
- post
- second-tag
Expand Down

0 comments on commit 2402ba4

Please sign in to comment.