diff --git a/.eleventy.js b/.eleventy.js index 521207a..538011c 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -22,15 +22,14 @@ module.exports = (eleventyConfig) => { mkdirSync('dist/css', { recursive: true }) } }) - - eleventyConfig.addPassthroughCopy('./src/fonts/') - eleventyConfig.addPlugin(svgContents) eleventyConfig.addPlugin(faviconPlugin, { destination: './dist' }) - eleventyConfig.addPlugin(syntaxHighlight) + eleventyConfig.addPlugin(require('./plugins/manifest-icons')) + + eleventyConfig.addPassthroughCopy('./src/fonts/') eleventyConfig.addCollection('notes', function (collectionApi) { return collectionApi.getFilteredByGlob('src/notes/**/*.md') diff --git a/plugins/manifest-icons/index.js b/plugins/manifest-icons/index.js new file mode 100644 index 0000000..5105409 --- /dev/null +++ b/plugins/manifest-icons/index.js @@ -0,0 +1,36 @@ +const sharp = require('sharp') +const fs = require('fs').promises +const getDirName = require('path').dirname + +async function writeFile (path, contents, cb) { + await fs.mkdir(getDirName(path), { recursive: true }) + fs.writeFile(path, contents, cb) +} + +function generatePngFavicon ({ density, width, height }, sourcePath, dimension) { + return sharp(sourcePath, { + density: (dimension / Math.max(width, height)) * density + }) + .resize(dimension, dimension) + .png() + .toBuffer() +} + +module.exports = (eleventyConfig) => { + eleventyConfig.addAsyncShortcode('manifest_icons', async function (faviconFile, ...dimensions) { + const metadata = await sharp(faviconFile).metadata() + + const res = await Promise.all(dimensions.map(async dimension => { + const favicon = await generatePngFavicon(metadata, faviconFile, dimension) + writeFile(`dist/icons/${dimension}/favicon.png`, favicon) + + return { + src: `/icons/${dimension}/favicon.png`, + sizes: `${dimension}x${dimension}`, + type: 'image/png' + } + })) + + return JSON.stringify(res) + }) +} diff --git a/src/layouts/base.njk b/src/layouts/base.njk index 74b031c..bf606b4 100644 --- a/src/layouts/base.njk +++ b/src/layouts/base.njk @@ -6,6 +6,7 @@