Skip to content

Instantly share code, notes, and snippets.

@jensgro
Forked from sombriks/.eleventy.js
Created March 21, 2023 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jensgro/3ef351c4049cde6dc1dc45312725f02a to your computer and use it in GitHub Desktop.
Save jensgro/3ef351c4049cde6dc1dc45312725f02a to your computer and use it in GitHub Desktop.
custom eleventy filter to count posts by year #eleventy #ssg
//...
eleventyConfig
.addFilter('yearTags', posts => {
const yearsList = posts.map(p => p.data.date.getFullYear())
const yearsCount = {}
yearsList.forEach(y => {
if (!yearsCount[y]) yearsCount[y] = 1
else yearsCount[y]++
});
return Object.keys(yearsCount)
.map(yc => ({ name: yc, count: yearsCount[yc] }))
});
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment