Created
February 24, 2022 12:02
-
-
Save BryanSchuetz/5f0edb20961bca128befcd88e2ab299d to your computer and use it in GitHub Desktop.
eleventy filter config
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
const _get = require("lodash.get"); | |
//It seems like, when using these filters on items in a collection with attributes in frontmatter—the keys are mostly on the data object, e.g. `| where('data.key', 'value')`. When using on global data, look for item attributes directly `| where('key', 'value')` | |
//sort by order attribute | |
function sortByOrder(values) { | |
let vals = [...values]; // this *seems* to prevent collection mutation... | |
return vals.sort((a, b) => Math.sign(a.data.order - b.data.order)); | |
} | |
//sort collections by title attribute | |
function sortByTitle(values) { | |
return values.slice().sort((a, b) => a.data.title.localeCompare(b.data.title)) | |
} | |
module.exports = function(eleventyConfig) { | |
//Filter helpers | |
eleventyConfig.addFilter("sortByOrder", sortByOrder); | |
eleventyConfig.addFilter('sortByTitle', sortByTitle); | |
//Re-creating Jekyll where filter | |
eleventyConfig.addFilter("where", (arr, key, compare) => { | |
compare = [].concat(compare) // force to arr | |
let filtered = arr.filter(item => { | |
return compare.includes(_get(item, key)); | |
}) | |
return filtered | |
}) | |
eleventyConfig.addFilter("removeWhere", (arr, key, compare) => { | |
compare = [].concat(compare) // force to arr | |
let filtered = arr.filter(item => { | |
return !compare.includes(_get(item, key)); | |
}) | |
return filtered | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment