Skip to content

Commit

Permalink
Add configuration for SVG optimization tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mataha committed May 29, 2024
1 parent d924af8 commit 81f5885
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 14 deletions.
19 changes: 5 additions & 14 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@ root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.json]
indent_style = tab
indent_size = 4

[*.md]
indent_size = 4
indent_size = unset
trim_trailing_whitespace = false

[*.py]
indent_size = 4

[*.sublime-*]
indent_style = tab
indent_size = 4
[*.{html,svg,toml,xhtml,yaml,yml}]
indent_size = 2

[*.tmPreferences]
[*.{json,sublime-,tmPreferences}*]
indent_style = tab
indent_size = 4
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
/.flake8 export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/biome.json export-ignore
/pyproject.toml export-ignore
/requirements-dev.txt export-ignore
/svglint.config.mjs export-ignore
/svgo.config.mjs export-ignore
21 changes: 21 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.0/schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": { "useLiteralKeys": "off" }
}
},
"formatter": {
"enabled": false
},
"organizeImports": {
"enabled": false
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}
71 changes: 71 additions & 0 deletions svglint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const NEWLINE_INDENT_REGEX = /(?:\n(?:\s*))+/g;

function t(strings, ...values) {
if (typeof strings === 'function') {
return (...args) => t(['', ''], strings(...args));
}
if (!Array.isArray(strings)) {
return t([strings]);
}
return strings
.reduce((result, string, index) => result + values[index - 1] + string)
.replace(NEWLINE_INDENT_REGEX, ' ')
.trim();
}

const SVG_PATH_REGEX = /^m[-mzlhvcsqtae\d,. ]+$/i;

export default {
rules: {
elm: {
'svg': 1,
'svg > path': 1,
'*': false,
},
attr: [
{
'fill' :'#000',
'fill-rule': 'evenodd',
'viewBox': '0 0 16 16',
'xmlns': 'http://www.w3.org/2000/svg',
'rule::selector': 'svg',
'rule::whitelist': true,
'rule::order': ['xmlns', 'fill', 'fill-rule', 'viewBox'],
},
{
'd': SVG_PATH_REGEX,
'rule::selector': 'svg > path',
'rule::whitelist': true,
'rule::order': true,
},
],
valid: true,
custom: [
(reporter, $, ast, { filename }) => {
reporter.name = 'no-self-closing-path';

// Don't allow explicit '</path>' closing tag
if (!ast.source.includes('</path>')) {
return;
}

const index = ast.source.indexOf('</path>');
const message = 'Invalid SVG content format';
const reason = t`
found a closing "path" tag at index ${index}.
The path should be self-closing,
use "/>" instead of "></path>"
`;
reporter.error(`${message}: ${reason}`);
},
]
},
ignore: [
// TODO: go through these and simplify/remove fill paths;
// I've barely skimmed all svgs so there are probably more
'icons/svg/file_type_fsharp.svg',
'icons/svg/file_type_nix.svg',
'icons/svg/file_type_python.svg',
'icons/svg/file_type_sublime.svg',
],
};
88 changes: 88 additions & 0 deletions svgo.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export default {
multipass: true,
js2svg: {
indent: 2,
pretty: true,
},
plugins: [
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeMetadata',
'removeEditorsNSData',
'cleanupAttrs',
'cleanupIds',
'cleanupNumericValues',
'removeUselessDefs',
'convertColors',
'removeUnknownsAndDefaults',
'removeNonInheritableGroupAttrs',
{
name: 'removeUselessStrokeAndFill',
params: {
removeNone: true,
},
},
'removeDimensions',
'cleanupEnableBackground',
'removeHiddenElems',
'removeEmptyText',
'cleanupListOfValues',
{
name: 'convertShapeToPath',
params: {
convertArcs: true,
},
},
'convertEllipseToCircle',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'collapseGroups',
{
name: 'convertPathData',
params: {
floatPrecision: 3,
noSpaceAfterFlags: false,
},
},
'convertTransform',
'removeEmptyAttrs',
'removeEmptyContainers',
'removeUnusedNS',
{
name: 'mergePaths',
params: {
force: true,
noSpaceAfterFlags: true,
},
},
{
name: 'addAttributesToSVGElement',
params: {
attributes: [{
'fill' :'#000',
'fill-rule': 'evenodd',
'viewBox': '0 0 16 16',
'xmlns': 'http://www.w3.org/2000/svg',
}],
},
},
{
name: 'removeAttrs',
params: {
attrs: [
'svg:.*(?<!((xmlns)|(fill)|(fill-rule)|(viewBox)))',
'path:(?!d)',
],
},
},
'sortAttrs',
'sortDefsChildren',
'removeOffCanvasPaths',
'removeRasterImages',
'removeStyleElement',
'removeScriptElement',
'removeTitle',
'removeDesc',
],
};

0 comments on commit 81f5885

Please sign in to comment.