Skip to content

Commit

Permalink
fix icon preview comment (#1823)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfennis authored Jan 21, 2024
1 parent 3bad7f6 commit 114fb08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 4 additions & 5 deletions scripts/generateChangedIconsCommentMarkup.mjs
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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}"` : '';

Expand Down
14 changes: 14 additions & 0 deletions scripts/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
: ""
}

0 comments on commit 114fb08

Please sign in to comment.