-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheleventy.config.images.js
More file actions
62 lines (54 loc) · 1.61 KB
/
eleventy.config.images.js
File metadata and controls
62 lines (54 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import path from "path";
import eleventyImage from "@11ty/eleventy-img";
import markdownIt from "markdown-it";
export default async (eleventyConfig) => {
function relativeToInputPath(inputPath, relativeFilePath) {
if (relativeFilePath.startsWith("/")) {
return `./content${relativeFilePath}`;
}
const split = inputPath.split("/");
split.pop();
return path.resolve(split.join(path.sep), relativeFilePath);
}
eleventyConfig.addAsyncShortcode(
"image",
async function imageShortcode(src, alt, widths, sizes, attributes) {
const formats = ["avif", "webp", "auto", "png"];
const file = relativeToInputPath(this.page.inputPath, src);
const metadata = await eleventyImage(file, {
widths: widths || ["auto"],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img")
});
const imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
title: alt
};
return eleventyImage.generateHTML(
metadata,
Object.assign(imageAttributes, attributes || {})
);
}
);
eleventyConfig.addAsyncShortcode("rawImageUrl", async (post, src, width) => {
const formats = ["webp"];
const file = relativeToInputPath(post.inputPath, src);
const metadata = await eleventyImage(file, {
widths: width ? [width] : ["auto"],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img")
});
return metadata.webp[0].url;
});
eleventyConfig.setLibrary(
"md",
markdownIt({
html: true,
breaks: true,
linkify: true
})
);
};