-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.images.js
62 lines (56 loc) · 2.03 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
const path = require("path");
const eleventyImage = require("@11ty/eleventy-img");
const { createBase64FromURL } = require("./libs/createBase64FromURL");
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
module.exports = (eleventyConfig) => {
/**
*
* @param {string} inputPath
* @param {string} relativeFilePath
* @returns
*/
function relativeToInputPath(inputPath, relativeFilePath) {
let split = inputPath.split("/");
split.pop();
const res = path.resolve(split.join(path.sep), relativeFilePath);
return res;
}
eleventyConfig.addAsyncShortcode("convert", createBase64FromURL);
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode(
"image",
async function imageShortcode(src, alt = "image alt") {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let formats = ["avif", "webp", "auto"];
let file = relativeToInputPath(this.page.inputPath, src);
let metadata = await eleventyImage(file, {
widths: [900, 1200],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
});
let data = metadata.webp[metadata.webp.length - 1];
return `<div class="img-container"><img class="border-radius10" src="${data.url}" width="100%" height="auto" alt="${alt}" loading="lazy" decoding="async"></div>`;
}
);
eleventyConfig.addAsyncShortcode(
"cover",
/**
* @param {string} file
* @param {string} alt
* @returns
*/
async function imageShortcode(src, alt) {
let formats = ["avif", "webp", "auto"];
let file = "/covers/" + src;
let metadata = await eleventyImage(file, {
widths: [300, 600],
formats,
outputDir: "_site/covers",
});
let data = metadata.webp[metadata.webp.length - 1];
return `<div class="img-container"><img class="border-radius10" src="${data.url}" alt="${alt}" decoding="async"></div>`;
}
);
};