const Image = require("@11ty/eleventy-img"); const path = require("path"); function relativeToInputPath(inputPath, relativeFilePath) { let split = inputPath.split("/"); split.pop(); return path.resolve(split.join(path.sep), relativeFilePath); } function isFullUrl(url) { try { new URL(url); return true; } catch(e) { return false; } } // Only one module.exports per configuration file, please! module.exports = function (eleventyConfig) { eleventyConfig.addPassthroughCopy("content/albums/**/*.mp3"); eleventyConfig.addShortcode("image", async function (src, alt, sizes) { let input; if(isFullUrl(src)) { input = src; } else { input = relativeToInputPath(this.page.inputPath, src); } let metadata = await Image(input, { widths: [300, 600], formats: ["avif", "jpeg"], outputDir: path.join(eleventyConfig.dir.output, "img"), }); let imageAttributes = { alt, sizes, loading: "lazy", decoding: "async", }; // You bet we throw an error on a missing alt (alt="" works okay) return Image.generateHTML(metadata, imageAttributes); }); };