forked from jamstack/jamstack.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
203 lines (166 loc) · 5.58 KB
/
.eleventy.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const lodashGet = require("lodash/get");
const yaml = require("js-yaml");
module.exports = function(eleventyConfig) {
// Support yaml data files
eleventyConfig.addDataExtension("yaml", contents => yaml.safeLoad(contents))
eleventyConfig.addWatchTarget("src/site/survey/**/*.js");
// pass images directly through to the output
eleventyConfig.addPassthroughCopy("src/site/img");
eleventyConfig.addPassthroughCopy({
"src/js": "js",
"node_modules/@zachleat/filter-container/*.js": "js",
"src/css": "css",
});
// Date helper
const { DateTime } = require('luxon');
eleventyConfig.addFilter('formatDate', (dateObj, formatStr) => {
// convert any date strings to read dates
if(typeof(dateObj) == "string") {
dateObj = new Date(dateObj);
}
const format = formatStr ? formatStr : 'LLLL d, y';
return DateTime.fromJSDate(dateObj, {
zone: 'utc'
}).toFormat(format);
});
// A handy markdown shortcode for blocks of markdown
// coming from our data sources
const markdownIt = require('markdown-it');
const md = new markdownIt({
html: true
});
eleventyConfig.addPairedShortcode('markdown', (content) => {
return md.render(content);
});
eleventyConfig.addFilter('convertFromEpoc', (time) => {
let date = new Date(0);
return date.setUTCSeconds(time);
});
eleventyConfig.addCollection("resources", function(collectionApi) {
return collectionApi.getFilteredByGlob("src/site/resources/*.md");
});
// Filter a collection based on items flagged as "featured"
eleventyConfig.addFilter('featured', (items) => {
return items.filter(item => item.data.featured);
});
// Filter a collection based on the value of a data attribute
eleventyConfig.addFilter('dataAttr', (items, attr, value) => {
return items.filter(item => item.data[attr] == value);
});
// Filter a collection based on the value of a data attribute
eleventyConfig.addFilter('whereData', (items, expression) => {
let key = expression.split('=')[0];
let val = expression.split('=')[1];
return items.filter(item => item.data[key] == val);
});
// filter a data array based on the value of a property
eleventyConfig.addFilter('select', (array, clause) => {
if(clause.indexOf("=") > -1) {
const property = clause.split("=")[0];
const value = clause.split("=")[1];
return array.filter(item => lodashGet(item, property).includes(value));
} else {
return array.map(item => lodashGet(item, clause));
}
});
eleventyConfig.addFilter('flatten', (array) => {
let results = [];
for(let result of array) {
if(result) {
if(Array.isArray(result)) {
results = [...results, ...result];
} else {
results.push(result);
}
}
}
return results;
});
eleventyConfig.addFilter('unique', (array) => {
let caseInsensitive = {};
for(let val of array) {
if(typeof val === "string") {
caseInsensitive[val.toLowerCase()] = val;
}
}
return Object.values(caseInsensitive);
});
// Get a random selection of items from an array
eleventyConfig.addFilter('luckydip', (array, count) => {
if (count > array.length) {
count = array.length;
}
const shuffled = array.sort( () => 0.5 - Math.random() );
return shuffled.slice(0, count);
});
// Convert an associative array into an indexable, iterable array
eleventyConfig.addFilter('iterable', (obj) => {
var iterableArray = new Array();
for (var item in obj){
iterableArray.push( obj[item] );
}
return iterableArray;
});
// format a url for display
eleventyConfig.addFilter('domain', (str) => {
var url = new URL(str);
return url.hostname;
});
// convert json to yaml
eleventyConfig.addFilter('dumpasyaml', obj => {
return yaml.safeDump(obj)
});
// sort an array of objects by one object key value
// can also get fancy and do a lodash.get selector string too
// see https://lodash.com/docs/4.17.15#get
eleventyConfig.addFilter('sortKey', (arr, selector) => {
return arr.sort((a, b) => {
let aKey = lodashGet(a, selector).toLowerCase();
let bKey = lodashGet(b, selector).toLowerCase();
if(aKey < bKey) {
return -1;
} else if(aKey > bKey) {
return 1;
}
return 0;
});
});
eleventyConfig.addFilter('sortTools', (arr, githubData) => {
return arr.sort((a, b) => {
let aKey = githubData[a.data.repo] ? (githubData[a.data.repo].stars || 0) : 0;
let bKey = githubData[b.data.repo] ? (githubData[b.data.repo].stars || 0) : 0;
if(aKey < bKey) {
return 1;
} else if(aKey > bKey) {
return -1;
}
return 0;
});
});
// Format a path to avoid any Cloudinary URL API miss-steps.
eleventyConfig.addFilter("cloudinaryifyPath", (str) => {
if(str) {
// add generic url encoding
str = encodeURI(str);
// we also need to double escape some characters which might appear in text
// but are meaningful in cloudinary URLs
str = str.replace(/,/g, '%252C');
str = str.replace(/\//g, '%252F');
}
return str;
});
// favicons files
eleventyConfig.addPassthroughCopy("src/site/browserconfig.xml");
eleventyConfig.addPassthroughCopy("src/site/site.webmanifest");
eleventyConfig.addPassthroughCopy("src/site/survey/2021/community-survey-2021-methodology.pdf");
return {
dir: {
input: "src/site",
inludes: "_includes",
output: "dist"
},
passthroughFileCopy: true,
markdownTemplateEngine: false,
htmlTemplateEngine: "njk"
};
};