31 lines
996 B
JavaScript
31 lines
996 B
JavaScript
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);
|
||
}
|
||
|
||
// Eleventy Image shortcode
|
||
// https://www.11ty.dev/docs/plugins/image/
|
||
eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, sizes) {
|
||
let file = relativeToInputPath(this.page.inputPath, src);
|
||
let metadata = await eleventyImage(file, {
|
||
widths: ["auto"],
|
||
// You can add "avif" or "jpeg" here if you’d like!
|
||
formats: ["webp", "png"],
|
||
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
|
||
});
|
||
let imageAttributes = {
|
||
alt,
|
||
sizes,
|
||
loading: "lazy",
|
||
decoding: "async",
|
||
};
|
||
return eleventyImage.generateHTML(metadata, imageAttributes);
|
||
});
|
||
};
|