-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
161 lines (144 loc) · 4.65 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
// docs: https://www.11ty.io/docs/config/
const fs = require("fs");
const path = require("path");
// Image processing
// https://www.11ty.dev/docs/plugins/image/#installation
const Image = require("@11ty/eleventy-img");
async function imageShortcode(src, alt, htmlID, context) {
if (alt === undefined) {
// Throw error on missing alt attribute (alt="" works okay)
throw new Error(`Missing \`alt\` on myImage from: ${src}`);
}
let metadata = await Image(src, {
widths: [800, 2400],
formats: ["jpeg"],
outputDir: "./dist/img/",
filenameFormat: (id, src, width, format, options) => {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}__${width}.${format}`;
},
});
// Use metadata to determine several responsive image attributes
const lowResImg = metadata.jpeg[0];
const aspectRatio = (lowResImg.width / lowResImg.height).toFixed(4);
const isPortrait = aspectRatio < 1;
// Srcset
const srcSet = metadata.jpeg.map((item) => item.srcset).join(", ");
// Sizes
// Layout constants for calculating image sizes
const gutterMobile = 8;
const gutterDesktop = 18; // (at 600px)
const contentArea = 0.8;
let sizes;
// Thumbnails (grid view)
if (context == "thumb") {
sizes = `
(min-width: 1200px) calc(1/6 * ${contentArea} * (100vw - ${
gutterDesktop * 7
}px)),
(min-width: 900px) calc(1/4 * ${contentArea} * (100vw - ${
gutterDesktop * 5
}px)),
(min-width: 600px) calc(1/2 * ${contentArea} * (100vw - ${
gutterDesktop * 3
}px)),
calc(100vw - ${gutterMobile * 3}px)
`;
} else {
// Album view: Layout goes 80% width (and vert images are 50 of that%)
// Landscape images
sizes = `
(min-width: 800px) calc(${contentArea} * (100vw - ${
gutterMobile * 2
})),
calc(100vw - ${gutterMobile * 2})
`;
if (isPortrait) {
// Portrait images are half width
sizes = `
(min-width: 800px) calc(1/2 * ${contentArea} * (100vw - ${
gutterDesktop * 3
})),
(min-width: 600px) calc(1/2 * (100vw - ${gutterDesktop * 3})),
calc(1/2 * (100vw - ${gutterMobile * 3}))
`;
}
}
return `
<div class="album__block album__block--image album__block--image-${
isPortrait ? "portrait" : "landscape"
}" id="${htmlID}">
<img
alt="${alt}"
loading="lazy"
sizes="${sizes}"
src="${lowResImg.url}"
srcset="${srcSet}"
style="aspect-ratio: ${aspectRatio};"
>
</div>`;
}
// Feed shortcode (see feed.njk)
async function feedImageShortcode(src, urlBase) {
let metadata = await Image(src, {
widths: [2400],
formats: ["jpeg"],
outputDir: "./dist/img/",
filenameFormat: (id, src, width, format, options) => {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}__${width}.${format}`;
},
});
const medResImg = metadata.jpeg[0];
return `${urlBase}${medResImg.url}`;
}
// Og image shortcode returns og:image markup
async function ogImageShortcode(src, urlBase) {
let metadata = await Image(src, {
widths: [800],
formats: ["jpeg"],
outputDir: "./dist/img/",
filenameFormat: (id, src, width, format, options) => {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}__${width}.${format}`;
},
});
const lowResImg = metadata.jpeg[0];
return `
<meta property="og:image" content="${urlBase}${lowResImg.url}" />
<meta property="og:image:width" content="${lowResImg.width}" />
<meta property="og:image:height" content="${lowResImg.height}" />
`;
}
function getBuildDateShortcode() {
return new Date().toISOString();
}
module.exports = function (eleventyConfig) {
eleventyConfig.setServerOptions({
liveReload: true,
domDiff: true,
port: 3000,
watch: [],
showAllHosts: false,
});
// Pass through certain assets
eleventyConfig.addPassthroughCopy("src/css");
eleventyConfig.addPassthroughCopy("src/js");
eleventyConfig.addPassthroughCopy("src/robots.txt");
// Add shortcodes
eleventyConfig.addNunjucksShortcode("getBuildDate", getBuildDateShortcode);
eleventyConfig.addNunjucksAsyncShortcode("image", imageShortcode);
eleventyConfig.addNunjucksAsyncShortcode("ogImage", ogImageShortcode);
eleventyConfig.addNunjucksAsyncShortcode("feedImageSrc", feedImageShortcode);
return {
htmlTemplateEngine: "njk",
passthroughFileCopy: true,
dir: {
input: "src",
output: "dist",
},
};
};