Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: icon preview comment #1823

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
: ""
}