-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleventy.config.images.js
99 lines (89 loc) · 3.36 KB
/
eleventy.config.images.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
const path = require("path");
const eleventyImage = require("@11ty/eleventy-img");
module.exports = (eleventyConfig) => {
function relativeToInputPath(inputPath, relativeFilePath) {
let split = inputPath.split("/");
split.pop();
return path.resolve(split.join(path.sep), relativeFilePath);
}
const formats = ["avif", "webp", "png", "auto"];
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode(
"image",
async function imageShortcode(src, alt, widths = [320, 720, 1024, 1280]) {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let file = relativeToInputPath(this.page.inputPath, src);
let metadata = await eleventyImage(file, {
widths: widths || [320, 720, 1024, 1280],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
});
// TODO loading=eager and fetchpriority=high
let imageAttributes = {
alt,
sizes: widths,
loading: "lazy",
decoding: "async",
};
return eleventyImage.generateHTML(metadata, imageAttributes);
}
);
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode(
"externalImage",
async function externalImageShortcode(
src,
alt,
widths = [320, 720, 1024, 1280],
cssClass,
id = "externalImage"
) {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let srcUrl = src ? src : "";
if (srcUrl === "") {
return "";
}
let metadata = await eleventyImage(srcUrl, {
widths: widths || [320, 720, 1024, 1280],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
});
// TODO loading=eager and fetchpriority=high
let imageAttributes = {
alt,
sizes: widths,
loading: "lazy",
decoding: "async",
class: cssClass,
id: id,
};
return eleventyImage.generateHTML(metadata, imageAttributes);
}
);
eleventyConfig.addAsyncShortcode(
"figure",
async function imageShortcode(src, alt, widths = [320, 720, 1024, 1280]) {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let file = relativeToInputPath(this.page.inputPath, src);
let metadata = await eleventyImage(file, {
widths: widths || [320, 720, 1024, 1280],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
});
// TODO loading=eager and fetchpriority=high
let imageAttributes = {
alt,
sizes: widths,
loading: "lazy",
decoding: "async",
};
const img = eleventyImage.generateHTML(metadata, imageAttributes);
return `<figure class="figure">${img}<figcaption class="figure-caption">${alt}</figcaption></figure>`;
}
);
};