-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
173 lines (148 loc) · 4.14 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
const yaml = require("js-yaml");
const CleanCSS = require("clean-css");
const Terser = require("terser");
const minify = require('html-minifier').minify;
const MONTHS = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
function getISODate(date) {
return new Date(date).toISOString();
}
function monthName(date) {
let monthIndex = new Date(date).getMonth();
return MONTHS[monthIndex];
}
function getISOMonth(date) {
return getISODate(date).substring(0,7);
}
function getMonthYearStringFromISO(date) {
let month = MONTHS[parseInt(date.split('-')[1]) - 1];
let year = date.split('-')[0];
let formattedDate = `${month}, ${year}`;
return formattedDate;
}
function getAllSortedPosts(collection) {
return collection
.getAll()
.filter(item => item.data.tag === 'post')
.sort((a, b) => {
if (a.url > b.url) {
return 1;
}
if (b.url > a.url) {
return -1;
}
return 0;
});
}
module.exports = function(eleventyConfig) {
/* Data extensions */
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
/* Collections */
eleventyConfig.addCollection('sortedPosts', getAllSortedPosts);
eleventyConfig.addCollection('postsGroupedByMonth', function(collection) {
let groupedPosts = getAllSortedPosts(collection)
.reduce((groupedPostsArray, item) => {
let key = getISOMonth(item.data.date);
let currentIndex = groupedPostsArray.findIndex((post) => {
return post.month === key;
});
if (currentIndex > -1) {
groupedPostsArray[currentIndex].posts.push(item);
} else {
groupedPostsArray.push({
month: key,
posts: [item]
});
}
return groupedPostsArray;
}, []);
return groupedPosts;
});
/* Filters */
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addNunjucksAsyncFilter("jsmin", async (code, callback) => {
try {
const minified = await Terser.minify(code);
return callback(null, minified.code);
} catch (err) {
console.error("Error during terser minify:", err);
return callback(err, code);
}
});
eleventyConfig.addFilter("htmlmin", function(code) {
var result = minify(code, {
collapseWhitespace: true
});
return result;
});
eleventyConfig.addFilter("postUrl", function(post) {
let postDate = getISODate(post.date).substring(0, 10);
let fileName = `${post.fileSlug}.html`;
return `/blog/${postDate}/${fileName}`;
});
eleventyConfig.addFilter("getYear", function(date) {
return new Date(date).getUTCFullYear();
});
eleventyConfig.addFilter("ISOMonth", function(date) {
return getISODate(date).substring(0,7);
});
eleventyConfig.addFilter("dateISO", function(date) {
return getISODate(date);
});
eleventyConfig.addFilter("ccyyMMdd", function(date) {
return getISODate(date).substring(0, 10);
});
eleventyConfig.addFilter("day", function(date) {
return new Date(date).getDate();
});
eleventyConfig.addFilter("month", function(date) {
return monthName(date);
});
eleventyConfig.addFilter("mon", function(date) {
return monthName(date).substring(0, 3);
});
eleventyConfig.addFilter("formattedMonthYear", function(ISOYearMonth) {
return getMonthYearStringFromISO(ISOYearMonth);
});
/* Directives */
/* Copy files in the `_root` folder to the `_site` root */
eleventyConfig.addPassthroughCopy({ "_root/*": "/" });
/* Copy CSS and JS from the base folder to the `_site` root */
eleventyConfig.addPassthroughCopy({ "css/**/*": "css" });
eleventyConfig.addPassthroughCopy({ "js/**/*": "js" });
return {
templateFormats: [
"html",
"njk",
"avif",
"jpeg",
"mp4",
"jpg",
"png",
"svg",
"webp",
"yaml",
],
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
dir: {
includes: "_includes",
layouts: "_layouts",
}
}
}