-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheleventy.config.images.js
71 lines (67 loc) · 2.33 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
const path = require("path");
const eleventyImage = require("@11ty/eleventy-img");
const markdownIt = require("markdown-it");
const markdownItEleventyImg = require("markdown-it-eleventy-img");
module.exports = (eleventyConfig) => {
const defaultImageWidths = [1210, 1044, 800, 400];
const defaultImageFormats = ["webp", "auto"];
const defaultImageSizes = "100vw";
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, widths, sizes) {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let metadata = await eleventyImage(src, {
widths: widths || defaultImageWidths,
formats: defaultImageFormats,
urlPath: "/assets/img/",
outputDir: path.join(eleventyConfig.dir.output, "/assets/img/"),
filenameFormat: function (id, src, width, format, options) {
// id: hash of the original image
// src: original image path
// width: current width in px
// format: current file format
// options: set of options passed to the Image call
const filename = path.basename(src, path.extname(src));
return `${filename}_${width}.${format}`;
},
});
// TODO loading=eager and fetchpriority=high
let imageAttributes = {
alt,
sizes: sizes || defaultImageSizes,
loading: "lazy",
decoding: "async",
};
return eleventyImage.generateHTML(metadata, imageAttributes);
});
eleventyConfig.setLibrary(
"md",
markdownIt({ html: true, breaks: true }).use(markdownItEleventyImg, {
imgOptions: {
widths: defaultImageWidths,
formats: defaultImageFormats,
urlPath: "/assets/img/",
outputDir: path.join(eleventyConfig.dir.output, "/assets/img/"),
sharpOptions: {
animated: true,
limitInputPixels: false,
},
filenameFormat: function (id, src, width, format, options) {
// id: hash of the original image
// src: original image path
// width: current width in px
// format: current file format
// options: set of options passed to the Image call
const filename = path.basename(src, path.extname(src));
return `${filename}_${width}.${format}`;
},
},
globalAttributes: {
class: "markdown-img",
decoding: "async",
sizes: defaultImageSizes,
},
}),
);
};