Add support for full URLs in image plugin.
This commit is contained in:
parent
d78b03ae1d
commit
e8a7253811
@ -1,22 +1,38 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const eleventyImage = require("@11ty/eleventy-img");
|
const eleventyImage = require("@11ty/eleventy-img");
|
||||||
|
|
||||||
module.exports = eleventyConfig => {
|
function relativeToInputPath(inputPath, relativeFilePath) {
|
||||||
function relativeToInputPath(inputPath, relativeFilePath) {
|
let split = inputPath.split("/");
|
||||||
let split = inputPath.split("/");
|
split.pop();
|
||||||
split.pop();
|
|
||||||
|
|
||||||
return path.resolve(split.join(path.sep), relativeFilePath);
|
return path.resolve(split.join(path.sep), relativeFilePath);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFullUrl(url) {
|
||||||
|
try {
|
||||||
|
new URL(url);
|
||||||
|
return true;
|
||||||
|
} catch(e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = function(eleventyConfig) {
|
||||||
// Eleventy Image shortcode
|
// Eleventy Image shortcode
|
||||||
// https://www.11ty.dev/docs/plugins/image/
|
// https://www.11ty.dev/docs/plugins/image/
|
||||||
eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, widths, sizes) {
|
eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, widths, sizes) {
|
||||||
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
|
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
|
||||||
// Warning: Avif can be resource-intensive so take care!
|
// Warning: Avif can be resource-intensive so take care!
|
||||||
let formats = ["avif", "webp", "auto"];
|
let formats = ["avif", "webp", "auto"];
|
||||||
let file = relativeToInputPath(this.page.inputPath, src);
|
let input;
|
||||||
let metadata = await eleventyImage(file, {
|
if(isFullUrl(src)) {
|
||||||
|
input = src;
|
||||||
|
} else {
|
||||||
|
input = relativeToInputPath(this.page.inputPath, src);
|
||||||
|
}
|
||||||
|
|
||||||
|
let metadata = await eleventyImage(input, {
|
||||||
widths: widths || ["auto"],
|
widths: widths || ["auto"],
|
||||||
formats,
|
formats,
|
||||||
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
|
outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
|
||||||
@ -29,6 +45,7 @@ module.exports = eleventyConfig => {
|
|||||||
loading: "lazy",
|
loading: "lazy",
|
||||||
decoding: "async",
|
decoding: "async",
|
||||||
};
|
};
|
||||||
|
|
||||||
return eleventyImage.generateHTML(metadata, imageAttributes);
|
return eleventyImage.generateHTML(metadata, imageAttributes);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user