fix icon preview comment (#1823)

This commit is contained in:
Eric Fennis
2024-01-21 19:45:21 +01:00
committed by GitHub
parent 3bad7f6ced
commit 114fb08556
2 changed files with 18 additions and 5 deletions
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { shuffle, readSvgDirectory, getCurrentDirPath } from './helpers.mjs';
import { shuffle, readSvgDirectory, getCurrentDirPath, minifySvg } from './helpers.mjs';
const currentDir = getCurrentDirPath(import.meta.url);
const ICONS_DIR = path.resolve(currentDir, '../icons');
@@ -17,11 +17,10 @@ const getImageTagsByFiles = (files, getBaseUrl, width) =>
files
.map((file) => {
const svgContent = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
const strippedAttrsSVG = svgContent
.replace(/<svg[^>]*>/, '<svg>')
.replaceAll(/\n| {2}|\t/g, '');
const strippedAttrsSVG = svgContent.replace(/<svg[^>]*>/, '<svg>')
const minifiedSvg = minifySvg(strippedAttrsSVG)
const base64 = Buffer.from(strippedAttrsSVG).toString('base64');
const base64 = Buffer.from(minifiedSvg).toString('base64');
const url = getBaseUrl(file);
const widthAttr = width ? `width="${width}"` : '';
+14
View File
@@ -208,3 +208,17 @@ export const shuffle = (array) => {
}
return array;
};
/**
* Minifies SVG
*
* @param {string} string
* @returns string
*/
export function minifySvg(string){
return string ? string
.replace(/\>[\r\n ]+</g, "><")
.replace(/(<.*?>)|\s+/g, (m, $1) => $1 || ' ')
.trim()
: ""
}