Skip to content

Commit

Permalink
Configure image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Jun 8, 2024
1 parent 00a23a3 commit 32cd942
Show file tree
Hide file tree
Showing 3 changed files with 763 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const CleanCSS = require("clean-css");
const yaml = require("js-yaml");
const Image = require("@11ty/eleventy-img");

/**
* @template T
Expand All @@ -21,6 +22,27 @@ module.exports = function (config) {
return new CleanCSS({}).minify(code).styles;
});

config.addShortcode("image", async function (src, alt, sizes, style) {
let metadata = await Image(src, {
widths: [300, 600],
formats: ["avif", "jpeg"],
outputDir: "dist/img",
});

let imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
style,
};

// You bet we throw an error on a missing alt (alt="" works okay)
return Image.generateHTML(metadata, imageAttributes);
});

config.addPassthroughCopy("assets");

config.addDataExtension("yaml", (contents) => yaml.load(contents));

config.addGlobalData("layout", "base.njk");
Expand Down
Loading

0 comments on commit 32cd942

Please sign in to comment.