-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
128 lines (103 loc) · 3.38 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
const Image = require('@11ty/eleventy-img');
const outdent = require('outdent');
const imageShortcode = async (
src,
alt,
className = undefined,
widths = [400, 800, 1280],
formats = ['webp', 'jpeg'],
sizes = '100vw'
) => {
const imageMetadata = await Image(src, {
widths: [...widths, null],
formats: [...formats, null],
outputDir: 'dist/img',
urlPath: '/img',
});
/** Maps a config of attribute-value pairs to an HTML string
* representing those same attribute-value pairs.
*/
const stringifyAttributes = (attributeMap) => {
return Object.entries(attributeMap)
.map(([attribute, value]) => {
if (typeof value === 'undefined') return '';
return `${attribute}="${value}"`;
})
.join(' ');
};
const sourceHtmlString = Object.values(imageMetadata)
// Map each format to the source HTML markup
.map((images) => {
// The first entry is representative of all the others
// since they each have the same shape
const { sourceType } = images[0];
// Use our util from earlier to make our lives easier
const sourceAttributes = stringifyAttributes({
type: sourceType,
// srcset needs to be a comma-separated attribute
srcset: images.map((image) => image.srcset).join(', '),
sizes,
});
// Return one <source> per format
return `<source ${sourceAttributes}>`;
})
.join('\n');
const getLargestImage = (format) => {
const images = imageMetadata[format];
return images[images.length - 1];
}
const largestUnoptimizedImg = getLargestImage(formats[0]);
const imgAttributes = stringifyAttributes({
src: largestUnoptimizedImg.url,
width: largestUnoptimizedImg.width,
height: largestUnoptimizedImg.height,
alt,
loading: 'lazy',
decoding: 'async',
});
const imgHtmlString = `<img ${imgAttributes}>`;
const pictureAttributes = stringifyAttributes({
class: className,
});
const picture = `<picture ${pictureAttributes}>
${sourceHtmlString}
${imgHtmlString}
</picture>`;
return outdent`${picture}`;
};
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const { DateTime } = require("luxon");
module.exports = function (eleventyConfig) {
eleventyConfig.setUseGitIgnore(false);
// base.njk aliased to base for easier access
eleventyConfig.addLayoutAlias('base', 'layouts/base.njk');
// Adds syntax highlighting
// https://www.11ty.dev/docs/plugins/syntaxhighlight/
eleventyConfig.addPlugin(syntaxHighlight);
// Watch for CSS changes
eleventyConfig.addWatchTarget("./.build/css/style.css");
// Copy CSS build changes to dist css/style.css
eleventyConfig.addPassthroughCopy({ "./.build/css/style.css": "css/style.css" });
// Open localhost url on runtime
eleventyConfig.setBrowserSyncConfig({
open: true
});
// Image shortcode
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode);
// Date shortcodes
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
eleventyConfig.addFilter("postDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED);
});
return {
dir: {
input: "src",
includes: "_includes",
output: "dist"
},
passthroughFileCopy: true,
templateFormats: ["html", "njk", "md"],
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
};
};