2023-01-24 09:08:52 -05:00
|
|
|
|
const path = require("path");
|
|
|
|
|
const eleventyImage = require("@11ty/eleventy-img");
|
|
|
|
|
|
2024-01-13 18:35:07 -05:00
|
|
|
|
function relativeToInputPath(inputPath, relativeFilePath) {
|
|
|
|
|
let split = inputPath.split("/");
|
|
|
|
|
split.pop();
|
2023-01-24 09:08:52 -05:00
|
|
|
|
|
2024-01-13 18:35:07 -05:00
|
|
|
|
return path.resolve(split.join(path.sep), relativeFilePath);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isFullUrl(url) {
|
|
|
|
|
try {
|
|
|
|
|
new URL(url);
|
|
|
|
|
return true;
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return false;
|
2023-01-24 09:08:52 -05:00
|
|
|
|
}
|
2024-01-13 18:35:07 -05:00
|
|
|
|
}
|
2023-01-24 09:08:52 -05:00
|
|
|
|
|
2024-01-13 18:35:07 -05:00
|
|
|
|
module.exports = function(eleventyConfig) {
|
2023-01-24 09:08:52 -05:00
|
|
|
|
// Eleventy Image shortcode
|
|
|
|
|
// https://www.11ty.dev/docs/plugins/image/
|
2023-01-24 09:41:50 -05:00
|
|
|
|
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 formats = ["avif", "webp", "auto"];
|
2024-01-13 18:35:07 -05:00
|
|
|
|
let input;
|
|
|
|
|
if(isFullUrl(src)) {
|
|
|
|
|
input = src;
|
|
|
|
|
} else {
|
|
|
|
|
input = relativeToInputPath(this.page.inputPath, src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let metadata = await eleventyImage(input, {
|
2023-01-24 09:41:50 -05:00
|
|
|
|
widths: widths || ["auto"],
|
|
|
|
|
formats,
|
2023-01-24 09:08:52 -05:00
|
|
|
|
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
|
|
|
|
|
});
|
2023-01-24 09:41:50 -05:00
|
|
|
|
|
|
|
|
|
// TODO loading=eager and fetchpriority=high
|
2023-01-24 09:08:52 -05:00
|
|
|
|
let imageAttributes = {
|
|
|
|
|
alt,
|
|
|
|
|
sizes,
|
|
|
|
|
loading: "lazy",
|
|
|
|
|
decoding: "async",
|
|
|
|
|
};
|
2024-01-13 18:35:07 -05:00
|
|
|
|
|
2023-01-24 09:08:52 -05:00
|
|
|
|
return eleventyImage.generateHTML(metadata, imageAttributes);
|
|
|
|
|
});
|
|
|
|
|
};
|