1
0
Fork 0

Leave sort order alone for highlight identification.

experiences
Mari 5 years ago
parent d13478dcd0
commit c0c1dfcff6
  1. 22
      .eleventy.js

@ -44,16 +44,16 @@ module.exports = function(eleventyConfig) {
// negated IDs are lowlights and others are highlights // negated IDs are lowlights and others are highlights
// * an object, in which case its keys are used as an array (as above)m // * an object, in which case its keys are used as an array (as above)m
// regardless of their values // regardless of their values
// highlightsFirst is an optional boolean; if true, highlights are listed
// first, then lowlights, though order is preserved within those groups. If
// false or not specified, order is the same as the input.
// Returns an array of objects, each of which has a one-to-one correspondence // Returns an array of objects, each of which has a one-to-one correspondence
// with an input item, using that object as its prototype and adding a // with an input item, using that object as its prototype and adding a
// boolean "highlight" property. Order is preserved within highlights and // boolean "highlight" property.
// lowlights, but highlights are first in the list. function identifyHighlights(itemList, filter, highlightsFirst) {
function identifyHighlights(itemList, filter) {
if (!Array.isArray(itemList)) { if (!Array.isArray(itemList)) {
return itemList; return itemList;
} }
const highlights = [];
const lowlights = [];
if (!Array.isArray(filter)) { if (!Array.isArray(filter)) {
if (filter === true || filter === "all") { if (filter === true || filter === "all") {
filter = ["all"] filter = ["all"]
@ -63,16 +63,20 @@ module.exports = function(eleventyConfig) {
filter = []; filter = [];
} }
} }
itemList.forEach(function(item) { const result = itemList.map(function(item) {
if ((filter.includes("all") if ((filter.includes("all")
&& !filter.includes("-" + item.id)) && !filter.includes("-" + item.id))
|| filter.includes(item.id)) { || filter.includes(item.id)) {
highlights.push(inheritAndAdd(item, {highlight: true})); return inheritAndAdd(item, {highlight: true});
} else { } else {
lowlights.push(inheritAndAdd(item, {highlight: false})); return inheritAndAdd(item, {highlight: false});
} }
}); });
return highlights.concat(lowlights); if (highlightsFirst) {
return result.filter(function(item) { return item.highlight }).concat(result.filter(function(item) { return !item.highlight }));
} else {
return result;
}
} }
eleventyConfig.addFilter("identifyHighlights", identifyHighlights); eleventyConfig.addFilter("identifyHighlights", identifyHighlights);

Loading…
Cancel
Save