-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleventy.config.collection.js
55 lines (50 loc) · 1.34 KB
/
eleventy.config.collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const path = require("path");
module.exports = function (eleventyConfig) {
eleventyConfig.addFilter("field", (array, field) => {
const result = array.map((i) => {
return i.data[field];
});
return result;
});
eleventyConfig.addFilter("filter", (array, field, value) => {
return array.filter((i) => {
return i.data[field] === value;
});
});
eleventyConfig.addFilter("unique", (array) => {
return [...new Set(array)];
});
eleventyConfig.addFilter("sortByDirectoryPrefix", (array) => {
const getNumberPrefix = (inputPath) => {
const dirName = path.basename(path.dirname(inputPath));
const prefix = dirName.split("-")[0];
const [beforeDecimal, afterDecimal] = prefix.split(".").map(Number);
return [beforeDecimal, afterDecimal || 0];
};
return array.sort((a, b) => {
const [aBefore, aAfter] = getNumberPrefix(a.inputPath);
const [bBefore, bAfter] = getNumberPrefix(b.inputPath);
if (aBefore === bBefore) {
return aAfter - bAfter;
}
return aBefore - bBefore;
});
});
eleventyConfig.addFilter("sort", (array, key, ascending = true) => {
if (ascending) {
return array.sort((a, b) => {
if (a.data[key] > b.data[key]) {
return 1;
}
return -1;
});
} else {
return array.sort((a, b) => {
if (a.data[key] > b.data[key]) {
return -1;
}
return 1;
});
}
});
};