You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

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.addPassthroughCopy("content/albums/**/*.zip");
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", "png"],
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);
});
};