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 @@ {{ title or metadata.title }} + {% favicon 'src/images/favicon.svg' %} diff --git a/src/pages/manifset.11tydata.js b/src/pages/manifset.11tydata.js new file mode 100644 index 0000000..e700074 --- /dev/null +++ b/src/pages/manifset.11tydata.js @@ -0,0 +1,3 @@ +module.exports = { + permalink: '/manifest.json' +} diff --git a/src/pages/manifset.njk b/src/pages/manifset.njk new file mode 100644 index 0000000..2c28a25 --- /dev/null +++ b/src/pages/manifset.njk @@ -0,0 +1,9 @@ +{ + "name": "{{ metadata.title }}", + "lang": "ru", + "start_url": "/", + "display": "standalone", + "background_color": "#fff", + "description": "Вики с различными полезными статьями", + "icons": {% manifest_icons 'src/images/favicon.svg', 48, 72, 96, 144, 148 %} +} \ No newline at end of file