Skip to content

Commit 826641d

Browse files
committed
feat(theme): use pagination's built-in href for next and previous URL; implement dynamic data point with getter
1 parent 95d9dc7 commit 826641d

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

.eleventy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = function(config) {
2626
config.addLayoutAlias('home', 'layouts/home.njk');
2727
config.addLayoutAlias('post', 'layouts/post.njk');
2828
config.addLayoutAlias('page', 'layouts/page.njk');
29+
config.addLayoutAlias('archive', 'layouts/archive.njk');
2930

3031
// Transforms
3132
config.addTransform('htmlmin', htmlMinTransform);

_data/global.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
const thisYear = new Date().getFullYear();
2-
const years = [];
3-
// my blog started in 2008
4-
for (let y = 2008; y <= thisYear; y++) {
5-
years.push(y);
6-
}
7-
1+
/* eslint-disable no-bitwise */
82
module.exports = {
9-
years,
3+
get years() {
4+
const thisYear = new Date().getFullYear();
5+
const years = [];
6+
// my blog started in 2008
7+
for (let y = 2008; y <= thisYear; y++) {
8+
years.push(y);
9+
}
10+
11+
return years;
12+
},
1013
random() {
1114
const segment = () => {
1215
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);

blog-by-year.njk

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@ permalink: /blog/{{ year }}/
1414

1515
{# Post list content #}
1616
{% set postListHeadingLevel = '2' %}
17-
{% set postListHeading = 'Posts' %}
1817
{% set postListItems = helpers.filterPostsByYear(collections.posts, year) %}
18+
{% set postListHeading = postListItems.length +' posts' %}
1919

2020
{# Pagination #}
2121
{% if year > global.years | first %}
2222
{% set paginationPrevText = 'Posts in ' + (year - 1) %}
23-
{% set paginationPrevUrl %}/blog/{{year - 1}}/{% endset %}
23+
{% set paginationPrevUrl = pagination.href.previous %}
2424
{% endif %}
2525
{% if year < global.years | last %}
2626
{% set paginationNextText = 'Posts in ' + (year + 1) %}
27-
{% set paginationNextUrl %}/blog/{{year + 1}}/{% endset %}
27+
{% set paginationNextUrl = pagination.href.next %}
2828
{% endif %}
2929

3030

3131
{% block content %}
3232
<main id="main-content" tabindex="-1">
3333
{% include "partials/components/intro.njk" %}
34+
{% if postListItems.length == 0 %}
35+
<div class="inner-wrapper"><p>No posts found.</p></div>
36+
{% endif %}
3437
{% include "partials/components/post-list.njk" %}
3538
{% include "partials/components/pagination.njk" %}
3639
</main>

blog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
title: 'Blog'
3-
layout: 'layouts/archive.njk'
3+
layout: 'archive'
44
---

0 commit comments

Comments
 (0)