diff --git a/eleventy.config.js b/eleventy.config.js index 800015d..41a52dc 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -7,11 +7,13 @@ import markdownIt from 'markdown-it'; const MARKDOWNIT_OPTIONS = { html: true, linkify: true, typographer: true }; const IMAGE_TRANSFORM_OPTS = { + // Set global default options formats: ['avif', 'webp', 'auto'], sharpJpegOptions: { mozjpeg: true, optimiseScans: true, quality: 95 }, sharpPngOptions: { compressionLevel: 9 }, urlPath: '/img/', - outputDir: './public/img/', + // Notably `outputDir` is resolved automatically + // to the project output directory defaultAttributes: { loading: 'lazy', decoding: 'async', @@ -22,34 +24,20 @@ const IMAGE_TRANSFORM_OPTS = { const BASE = env.ELEVENTY_PRODUCTION ? 'https://ref.sebin-nyshkim.net' : 'http://localhost:8080'; // source: https://notes.jays.net/blog/11ty/ -async function getImage(type, src, key) { - let widths, formats; - - switch (type) { - case 'og': - widths = [1200]; - formats = ['webp']; - break; - case 'favicon': - widths = [200]; - formats = ['png']; - break; - } - - const opts = { - ...IMAGE_TRANSFORM_OPTS, - widths, - formats - }; - - const stats = await Image(src, opts); - const [format] = formats; +async function getOgImage(src, key) { + const stats = await Image(src, { + width: [1200], + formats: ['webp'], + urlPath: '/img/', + outputDir: './public/img/', + filenameFormat: (hash, src, width, format) => `${hash}-${width}.${format}` + }); if (key === 'url') { - return BASE + stats[format][0][key]; + return BASE + stats.webp[0][key]; } - return stats[format][0][key]; + return stats.webp[0][key]; } export default async function (eleventyConfig) { @@ -84,7 +72,7 @@ export default async function (eleventyConfig) { return content; }); - eleventyConfig.addFilter('getImage', getImage); + eleventyConfig.addFilter('getOgImage', getOgImage); eleventyConfig.addPlugin(eleventyPluginWebc, { components: ['src/components/**/*.webc', 'npm:@11ty/eleventy-img/*.webc'], diff --git a/src/components/head/page-head-meta.webc b/src/components/head/page-head-meta.webc index b3a6861..f682883 100644 --- a/src/components/head/page-head-meta.webc +++ b/src/components/head/page-head-meta.webc @@ -7,18 +7,11 @@ return 'Choose Your Character!'; }; - const getImageUrl = (char, type = 'og') => - char ? `src/img/${char.toLowerCase()}/${type}.png` : `src/img/${type}.png`; + const getImageUrl = (char) => (char ? `src/img/${char.toLowerCase()}/og.png` : 'src/img/og.png');