human-anatomy-3d/eleventy.config.images.js

35 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2023-05-01 11:58:46 +00:00
const path = require('path');
const eleventyImage = require('@11ty/eleventy-img');
module.exports = (eleventyConfig) => {
function relativeToInputPath(inputPath, relativeFilePath) {
const 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, 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!
const formats = ['avif', 'webp', 'auto'];
const file = relativeToInputPath(this.page.inputPath, src);
const metadata = await eleventyImage(file, {
widths: widths || ['auto'],
formats,
outputDir: path.join(eleventyConfig.dir.output, 'img'), // Advanced usage note: `eleventyConfig.dir` works here because were using addPlugin.
});
// TODO loading=eager and fetchpriority=high
const imageAttributes = {
alt,
sizes,
loading: 'lazy',
decoding: 'async',
};
return eleventyImage.generateHTML(metadata, imageAttributes);
});
};