-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
177 lines (148 loc) · 5.48 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
const htmlmin = require('html-minifier');
const fs = require('fs');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const { format } = require('date-fns');
const Image = require('@11ty/eleventy-img');
const { parseHTML } = require('linkedom');
const md = require('markdown-it')();
const removeMd = require('remove-markdown');
function markdown(text) {
return md.render(text);
}
function removeMarkdown(text) {
return removeMd(text);
}
function getImageMeta(src, widths) {
const options = {
widths: widths || [100, 300, 600, 900, 1200, null],
formats: ['webp', 'jpg', 'png', 'svg'],
outputDir: './build/assets/images/11ty',
urlPath: '/assets/images/11ty',
};
const url = `./src/assets/images/${src}`;
Image(url, options);
return Image.statsSync(url, options);
}
function getImageUrl(src, width, format = 'jpeg', widths) {
const metadata = getImageMeta(src, widths);
return metadata[format]?.find((image) => image.width === width)?.url || '';
}
function getImageTag(src, alt = '', sizes, attrs = {}, widths) {
const metadata = getImageMeta(src, widths);
const imageAttributes = {
alt,
loading: 'lazy',
decoding: 'async',
sizes: sizes || '(min-width: 768px) 50vw, 100vw',
...attrs,
};
return Image.generateHTML(metadata, imageAttributes);
}
module.exports = function (eleventyConfig) {
eleventyConfig.setUseGitIgnore(false);
// Watch our compiled assets for changes
eleventyConfig.addWatchTarget('./src/compiled-assets/main.css');
eleventyConfig.addWatchTarget('./src/compiled-assets/main.js');
eleventyConfig.addWatchTarget('./src/compiled-assets/vendor.js');
// Copy src/compiled-assets to /assets
eleventyConfig.addPassthroughCopy({ 'src/compiled-assets': 'assets' });
// Copy all images
eleventyConfig.addPassthroughCopy('src/assets/images');
eleventyConfig.addPassthroughCopy('src/assets/vendor');
eleventyConfig.addPassthroughCopy({ 'src/assets/meta': '/' });
eleventyConfig.addPassthroughCopy({ public: '/' });
eleventyConfig.addPlugin(syntaxHighlight, {
alwaysWrapLineHighlights: true,
});
eleventyConfig.setBrowserSyncConfig({
server: {
baseDir: ['./build'],
serveStaticOptions: {
extensions: ['html'],
},
},
callbacks: {
ready: function (err, bs) {
bs.addMiddleware('*', (req, res) => {
const content_404 = fs.readFileSync('build/404/index.html');
res.writeHead(404, {
'Content-Type': 'text/html; charset=UTF-8',
});
res.write(content_404);
res.end();
});
},
},
});
eleventyConfig.on('afterBuild', () => {
const data = require('./src/_data/site.js');
fs.writeFile(`./build/site.json`, JSON.stringify(data), function (err) {
if (err) {
throw err;
}
});
});
eleventyConfig.addTransform(
'transform-blog-images',
function (content, outputPath) {
if (outputPath.endsWith('.html') && outputPath.includes('blog')) {
const { document } = parseHTML(content);
const images = [
...document.querySelectorAll('img:not(#hero-image)'),
];
images
.filter(
(i) =>
!i.src.includes('arrow--') &&
!i.src.includes('tag') &&
!i.src.includes('footer__bg') &&
!i.src.includes('http')
)
.forEach((i) => {
if (i.src.includes('gif')) {
i.src = `/assets/images/blog/${i.src}`;
return;
}
i.outerHTML = getImageTag(`blog/${i.src}`, i.alt, null);
});
return `<!DOCTYPE html>${document.documentElement.outerHTML}`;
}
return content;
}
);
if (process.env.ELEVENTY_ENV === 'production') {
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
if (outputPath.endsWith('.html')) {
const minified = htmlmin.minify(content, {
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
removeComments: true,
sortClassName: true,
useShortDoctype: true,
});
return minified;
}
return content;
});
}
eleventyConfig.addFilter('keys', (obj) => Object.keys(obj));
eleventyConfig.addFilter('format', format);
eleventyConfig.addFilter('getImageTag', getImageTag);
eleventyConfig.addFilter('getImageUrl', getImageUrl);
eleventyConfig.addFilter('markdown', markdown);
eleventyConfig.addFilter('removeMarkdown', removeMarkdown);
global.filters = eleventyConfig.javascriptFunctions;
eleventyConfig.setPugOptions({
globals: ['filters'],
});
return {
dir: {
includes: '_components',
input: 'src',
layouts: '_layouts',
output: 'build',
},
templateFormats: ['pug', 'md'],
htmlTemplateEngine: 'pug',
};
};