Skip to content

Instantly share code, notes, and snippets.

@Andy-set-studio
Created December 15, 2018 14:35
Show Gist options
  • Save Andy-set-studio/61a8a289b724ff0a4502fdb7f48ab5db to your computer and use it in GitHub Desktop.
Save Andy-set-studio/61a8a289b724ff0a4502fdb7f48ab5db to your computer and use it in GitHub Desktop.
Filter posts by a passed year
title year layout
An example
2018
example.njk
<ul>
{% for item in collections.writing | yearFilter(year) %}
<li>
<!-- item -->
</li>
{% endfor %}
</ul>
/**
* Filter and return content for the passed year
*
* @param {Array} value
* @param {String|Number} year
* @returns {Array}
*/
module.exports = function yearFilter(value, year) {
let response = [];
value.map(item => {
// Pick either the date set in frontmatter or the date eleventy picks from meta info
const date = new Date(item.data.date ? item.data.date : item.date);
// Parse out the passed year and compare it against this content's full year.
// Add to the response if there's a match
if (date.getFullYear() === parseInt(year, 10)) {
response.push(item);
}
});
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment