/
.eleventy.js
145 lines (131 loc) · 3.87 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
const htmlmin = require('html-minifier');
const { DateTime } = require('luxon');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItLazyImg = require('markdown-it-image-lazy-loading');
const markdownitLinkAttributes = require('markdown-it-link-attributes');
const markdownitAttrs = require('markdown-it-attrs');
const { minify } = require('terser');
// const bart = {
// doeIets: (input) => {
// return input.toUpperCase();
// }
// };
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
// eleventyConfig.addPlugin(lazyImagesPlugin, {
// transformImgPath: (imgPath) => {
// if (imgPath.startsWith('http://') || imgPath.startsWith('https://')) {
// // Handle remote file
// return imgPath;
// } else {
// return `./src/${imgPath}`;
// }
// },
// });
// eleventyConfig.setEjsOptions({
// rmWhitespace: true,
// context: {
// // bart
// },
// });
//
// eleventyConfig.addFilter('json', (obj) => {
// return JSON.stringify(obj);
// });
eleventyConfig.addFilter('date', (dateObj, type) => {
let format;
const dateTime = DateTime.fromJSDate(dateObj, { zone: 'utc' });
switch (type) {
case 'iso':
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toISODate();
case 'date':
default:
return dateTime.toFormat('LLLL d, yyyy');
}
});
eleventyConfig.setBrowserSyncConfig({
files: './_site/assets/styles/main.css',
ghostMode: false,
});
// Get the first `n` elements of a collection. Or last by supplying a negative `n`
eleventyConfig.addFilter('head', (array, n) => {
return n < 0 ? array.slice(n) : array.slice(0, n);
});
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
if (outputPath.endsWith('.html')) {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
conservativeCollapse: false,
});
}
return content;
});
eleventyConfig.setDataDeepMerge(true);
/* Markdown Overrides */
let markdownLibrary = markdownIt({
html: true,
breaks: false,
linkify: true,
typographer: true,
})
.use(markdownitLinkAttributes, {
pattern: /^http/,
attrs: {
target: '_blank',
rel: 'noopener',
},
})
.use(markdownItAnchor, {
permalink: true,
permalinkClass: 'anchor',
permalinkSymbol: '#',
permalinkBefore: true,
level: 2,
})
.use(markdownItLazyImg)
.use(markdownitAttrs);
eleventyConfig.setLibrary('md', markdownLibrary);
eleventyConfig.addCollection('tagList', function (collection) {
let tagSet = new Set();
collection.getAll().forEach(function (item) {
if ('tags' in item.data) {
const tags = item.data.tags.filter(function (tag) {
return !['all', 'post'].includes(tag);
});
for (const tag of tags) {
tagSet.add(tag);
}
}
});
// returning an array in addCollection works in Eleventy 0.5.3
return [...tagSet];
});
eleventyConfig.addNunjucksAsyncFilter('jsmin', async function (code, callback) {
try {
const terserOpts = { toplevel: true };
const minified = await minify(code, terserOpts);
callback(null, minified.code);
} catch (err) {
console.error('Terser error: ', err);
// Fail gracefully.
callback(null, code);
}
});
return {
pathPrefix: '/',
dir: {
input: 'src',
output: '_site',
data: '_data',
},
templateFormats: ['njk', 'md', '11ty.js'],
markdownTemplateEngine: 'njk',
};
};