-
Notifications
You must be signed in to change notification settings - Fork 7
/
.eleventy.js
244 lines (217 loc) · 6.28 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const { DateTime } = require("luxon");
const CleanCSS = require("clean-css");
const UglifyJS = require("uglify-es");
const sanitizeHTML = require('sanitize-html')
const htmlmin = require("html-minifier");
const widont = require("widont");
const md = require("markdown-it")({
html: true,
linkify: true,
typographer: true,
});
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const inclusiveLangPlugin = require("@11ty/eleventy-plugin-inclusive-language");
module.exports = function(eleventyConfig) {
// Date formatting (human readable)
eleventyConfig.addFilter("readableDate", date => {
return DateTime.fromJSDate(date).toFormat("dd LLL yyyy");
});
// Date formatting (machine readable)
eleventyConfig.addFilter("machineDate", date => {
return DateTime.fromJSDate(date).toISO();
});
// Markdownify
eleventyConfig.addFilter("markdownify", text => {
return md.renderInline( text );
});
// unSluggify
eleventyConfig.addFilter("unslug", text => {
text = text.charAt(0).toUpperCase() + text.slice(1);
return text.replace(/-/g, ' ');
});
// Name List
eleventyConfig.addShortcode("NameList", names => {
let strings = [],
string = "",
count = names.length;
while ( count-- )
{
let url = names[count].url || `https://twitter.com/${names[count].twitter}/`;
strings.unshift( `<a href="${url}">${names[count].name}</a>` );
}
count = strings.length;
if ( count > 2 )
{
strings[count-1] = "and " + strings[count-1];
string = strings.join(", ");
}
else if ( count == 2 )
{
string = `${strings[0]} and ${strings[1]}`;
}
else
{
string = strings[0];
}
return `${string}`;
});
// Fix proper nouns
eleventyConfig.addFilter("fixNames", text => {
let test = text.toLowerCase(),
acronyms = [ "html", "css", "svg" ],
camel_case = [ "JavaScript" ],
i, proper_name;
if ( acronyms.indexOf( test ) > -1 )
{
return text.toUpperCase();
}
else
{
for ( i in camel_case )
{
proper_name = camel_case[i];
if ( proper_name.toLowerCase() == test )
{
return proper_name;
}
}
}
return text;
});
// Widont
eleventyConfig.addFilter("widont", function(text) {
return `${widont( text )}`;
});
// Minify CSS
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
// Minify JS
eleventyConfig.addFilter("jsmin", function(code) {
let minified = UglifyJS.minify(code);
if (minified.error) {
console.log("UglifyJS error: ", minified.error);
return code;
}
return minified.code;
});
// Minify HTML output
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if (outputPath.indexOf(".html") > -1) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true
});
minified = minified.replace('\u00a0', '<b class="shy">\u00a0</b>');
return minified;
}
return content;
});
// Minify JS output
eleventyConfig.addTransform("jsmin", function(content, outputPath) {
if (outputPath.indexOf(".js") > -1) {
let minified = UglifyJS.minify(content);
return minified;
}
return content;
});
// limit filter
eleventyConfig.addNunjucksFilter("limit", function(array, limit) {
return array.slice(0, limit);
});
eleventyConfig.addCollection("devs", collection => {
// get unsorted items
return collection.getAll().filter( item => {
return item.inputPath.indexOf("devs/") > -1;
});
});
eleventyConfig.addFilter("extractID", url => {
url = url.split("/");
return url[2];
});
eleventyConfig.addCollection("tags", function(collection) {
// get unsorted items
var tags = [];
collection.getAll()
.map( item => {
if ( item.inputPath.indexOf("wants/") > -1 )
{
item.data.tags.map( tag => {
if ( tags.indexOf( tag ) < 0 )
{
tags.push( tag );
}
});
}
});
return tags.sort();
});
eleventyConfig.addFilter("toString", function(collection, separator, props) {
var ret = [],
i = collection.length;
while ( i-- )
{
let str = [],
j = props.length;
while ( j-- )
{
let text = collection[i].data[props[j]];
if ( props[j].indexOf("date") > -1 )
{
text = new Date( text );
text = DateTime.fromJSDate(text).toFormat("dd LLL yyyy");
}
str.unshift( text );
}
ret.unshift( str.join( separator ) );
}
return ret;
});
eleventyConfig.addFilter("getDirectory", function(url) {
url = url.split('/');
return url[1];
});
// Don't process folders with static assets e.g. images
eleventyConfig.addPassthroughCopy("sw.js");
eleventyConfig.addPassthroughCopy("static/img");
eleventyConfig.addPassthroughCopy("static/js");
eleventyConfig.addPassthroughCopy("manifest.json");
eleventyConfig.addPassthroughCopy("admin");
// eleventyConfig.addPassthroughCopy("_includes/assets/");
/* Markdown Plugins */
let markdownIt = require("markdown-it");
let markdownItAnchor = require("markdown-it-anchor");
let options = {
html: true,
breaks: true,
linkify: true
};
let opts = {
permalink: false
};
eleventyConfig.setLibrary("md", markdownIt(options)
.use(markdownItAnchor, opts)
);
eleventyConfig.addPlugin(syntaxHighlight);
//eleventyConfig.addPlugin(inclusiveLangPlugin);
return {
templateFormats: ["md", "njk", "html", "liquid"],
// If your site lives in a different subdirectory, change this.
// Leading or trailing slashes are all normalized away, so don’t worry about it.
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
// This is only used for URLs (it does not affect your file structure)
pathPrefix: "/",
markdownTemplateEngine: "liquid",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
passthroughFileCopy: true,
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
};