Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829import { mkdir, readFile, writeFile } from 'node:fs/promises';import { dirname } from 'node:path';import sharp from 'sharp';
const output = '.astro/generated/styles/_icons.scss';
export async function generateIcons() { const { glyphs, size } = JSON.parse(await readFile('src/assets/pixel-icons.json', 'utf8')); const masks = []; for (const [name, { rows }] of Object.entries(glyphs)) { const pixels = Buffer.alloc(size * size * 4); const xOffset = Math.floor((size - rows[0].length) / 2); const yOffset = Math.floor((size - rows.length) / 2); rows.forEach((row, y) => [...row].forEach((bit, x) => { if (bit === '1') pixels.set([255, 255, 255, 255], ((y + yOffset) * size + x + xOffset) * 4); }), ); const png = await sharp(pixels, { raw: { width: size, height: size, channels: 4 } }) .png() .toBuffer(); const uri = `url('data:image/png;base64,${png.toString('base64')}')`; masks.push(`.pixel-icon[data-icon='${name}'] { --icon: ${uri}; }`); if (name === 'external') masks.push(`:root { --external-icon: ${uri}; }`); } await mkdir(dirname(output), { recursive: true }); await writeFile(output, `/* Generated icon masks. */\n${masks.join('\n')}\n`);}