title | year | layout |
---|---|---|
An example |
2018 |
example.njk |
Created
December 15, 2018 14:35
-
-
Save Andy-set-studio/61a8a289b724ff0a4502fdb7f48ab5db to your computer and use it in GitHub Desktop.
Filter posts by a passed year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
{% for item in collections.writing | yearFilter(year) %} | |
<li> | |
<!-- item --> | |
</li> | |
{% endfor %} | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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