318c024589
* add pnpm * make it work * fix comamnds in package.jsons * move some scripts to modules * workflow fixes * test workflow * test #2 * minor fix * update lockflite * create workflows * update workflow * Add copy license command * Fix build * update workflows * update contributions.md * migrate site directory to pnpm * Fix peer dependencies when install * fix types in lucide-angular * fix testing
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
/* eslint-disable import/no-extraneous-dependencies */
|
|
import { optimize } from 'svgo';
|
|
import prettier from 'prettier';
|
|
import { parseSync, stringify } from 'svgson';
|
|
import DEFAULT_ATTRS from './default-attrs.json' assert { type: 'json' };
|
|
|
|
/**
|
|
* Optimize SVG with `svgo`.
|
|
* @param {string} svg - An SVG string.
|
|
* @returns {Promise<string>} An optimized svg
|
|
*/
|
|
async function optimizeSvg(svg) {
|
|
const result = optimize(svg, {
|
|
plugins: [
|
|
{
|
|
name: 'preset-default',
|
|
params: {
|
|
overrides: {
|
|
convertShapeToPath: false,
|
|
mergePaths: false,
|
|
removeAttrs: {
|
|
attrs: '(fill|stroke.*)',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
return result.data;
|
|
}
|
|
|
|
/**
|
|
* Set default attibutes on SVG.
|
|
* @param {string} svg - An SVG string.
|
|
* @returns {string} An SVG string, included with the default attributes.
|
|
*/
|
|
function setAttrs(svg) {
|
|
const contents = parseSync(svg);
|
|
|
|
contents.attributes = DEFAULT_ATTRS;
|
|
|
|
return stringify(contents);
|
|
}
|
|
|
|
/**
|
|
* Process SVG string.
|
|
* @param {string} svg An SVG string.
|
|
* @returns {Promise<string>} An optimized svg
|
|
*/
|
|
function processSvg(svg) {
|
|
return (
|
|
optimizeSvg(svg)
|
|
.then(setAttrs)
|
|
.then(optimizedSvg => prettier.format(optimizedSvg, { parser: 'babel' }))
|
|
// remove semicolon inserted by prettier
|
|
// because prettier thinks it's formatting JSX not HTML
|
|
.then(svg => svg.replace(/;/g, ''))
|
|
);
|
|
}
|
|
|
|
export default processSvg;
|