-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
210 lines (168 loc) · 6.12 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
204
205
206
207
208
209
210
const yaml = require("js-yaml");
const { DateTime } = require("luxon");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const htmlmin = require("html-minifier");
const util = require('util');
const svgContents = require("eleventy-plugin-svg-contents");
const Image = require("@11ty/eleventy-img");
function imageShortcode({src, alt, cls, styleName}) {
if(alt === undefined) {
// You bet we throw an error on missing alt (alt="" works okay)
throw new Error(`Missing \`alt\` on myImage from: ${src}`);
}
if (styleName === undefined) {
throw new Error(`Missing \`styleName\` on myImage from: ${src}`);
}
let styles = {
medium_half: {
sizes: "(max-width: 767px) 100vw, 600px"
}
}
let options = {
widths: [640, 768, 1024],
formats: ['webp', 'jpeg'],
urlPath: "/static/img/",
outputDir: "./_site/static/img/"
};
Image(src, options);
let sizes = styles[styleName].sizes;
let imageAttributes = {
class: cls,
alt,
sizes,
loading: "lazy",
decoding: "async",
};
// get metadata even the images are not fully generated
metadata = Image.statsSync(src, options);
return Image.generateHTML(metadata, imageAttributes);
}
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(svgContents);
// Disable automatic use of your .gitignore
eleventyConfig.setUseGitIgnore(false);
// Merge data instead of overriding
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addFilter('console', function(value) {
const str = util.inspect(value);
return `<pre class="block" style="max-height: 500px; overflow-y: scroll;">${unescape(str)}</pre>;`
});
// human readable date
eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat(
"dd LLL yyyy"
);
});
eleventyConfig.addFilter("sortByName", (items) => {
let data = [...items];
return data.sort((a, b) => { b.data.name.localeCompare(a.data.name) });
})
eleventyConfig.addFilter("sortResultsDirectory", (results) => {
// Using sort for multiple columns
// https://dev.to/markbdsouza/js-sort-an-array-of-objects-on-multiple-columns-keys-2bj1
// avoids mutating results collection
let data = [...results];
// sorts by grade, then subject, then area.
return data.sort((a, b) => (a.data.grade.localeCompare(b.data.grade) || a.data.subject.localeCompare(b.data.subject) || a.data.area.localeCompare(b.data.area)) );
});
eleventyConfig.addShortcode("image", imageShortcode);
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
eleventyConfig.addShortcode("buildTime", () => `${new Date().toISOString()}`);
eleventyConfig.addShortcode("getItemName", (collection, id) => {
let items = collection.filter( (item) => {
if (item.data.id == id ) {
return item;
}
});
return items[0].data.name;
});
eleventyConfig.addShortcode("getSubjectField", (collection, id, fieldName) => {
let items = collection.filter( (item) => {
if (item.data.id == id ) {
return item;
}
});
return items[0].data[fieldName];
});
eleventyConfig.addShortcode("subjectTree", (results, grades, subjects, areas) => {
const tree = {};
// Make a list of subjects which are used in any results.
results.forEach(result => {
// if the tree doesn't yet have this result's subject, add it
if (!(tree.hasOwnProperty(result.data.grade))) {
tree[result.data.grade] = {
"name": getNameById(grades, result.data.grade),
"subjects": {}
};
}
// if this subject id branch doesn't have this grade yet, add it
if (!(tree[result.data.grade]['subjects'].hasOwnProperty(result.data.subject))) {
tree[result.data.grade]['subjects'][result.data.subject] = {
"name": getNameById(subjects, result.data.subject),
"areas": {}
};
}
// if this subject and grade branch doesn't have this area yet, add it
if (!(tree[result.data.grade]['subjects'][result.data.subject]['areas'].hasOwnProperty(result.data.area))) {
tree[result.data.grade]['subjects'][result.data.subject]['areas'][result.data.area] = {
"name": getNameById(areas, result.data.area),
"url": result.url
};
}
});
return JSON.stringify(tree);
});
// Return grades which are referenced on results which also include a given subject.
const getNameById = (data, id) => {
let name;
data.every(item => {
if (item.data.id == id) {
name = item.data.name;
return false;
}
return true;
});
return name;
}
// Syntax Highlighting for Code blocks
eleventyConfig.addPlugin(syntaxHighlight);
// To Support .yaml Extension in _data
// You may remove this if you can use JSON
eleventyConfig.addDataExtension("yaml", (contents) =>
yaml.safeLoad(contents)
);
// Copy Static Files to /_Site
eleventyConfig.addPassthroughCopy({
"./src/robots.txt": "./robots.txt",
"./src/admin/config.yml": "./admin/config.yml",
"./node_modules/prismjs/themes/prism-tomorrow.css": "./static/css/prism-tomorrow.css",
"./src/static/fonts/icons/fonts/*": "./static/fonts/icons/fonts/",
"./src/static/fonts/icons/style.css": "./static/fonts/icons/style.css",
"./src/static/files/*": "./static/files/",
});
// Copy Image Folder to /_site
eleventyConfig.addPassthroughCopy("./src/static/img");
// Copy favicon to route of /_site
eleventyConfig.addPassthroughCopy("./src/static/favicon");
// Minify HTML
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
// Eleventy 1.0+: use this.inputPath and this.outputPath instead
if (outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
// Let Eleventy transform HTML files as nunjucks
// So that we can use .html instead of .njk
return {
dir: {
input: "src",
},
htmlTemplateEngine: "njk",
};
};