Skip to content

Commit

Permalink
feat: plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Mar 12, 2024
1 parent 242ce32 commit e861353
Show file tree
Hide file tree
Showing 4 changed files with 631 additions and 139 deletions.
56 changes: 56 additions & 0 deletions lib/plugins/add-groups.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function is(node, parentNode, params) {
return (
node.name === params.name &&
(params.match ? params.match(node) : true) &&
!(
parentNode.name === 'g' &&
(params.match ? params.match(parentNode) : true)
)
);
}

export const name = 'addGroups';

export const fn = (_, params) => {
const io = new Set();

return {
element: {
enter(node, parentNode) {
if (is(node, parentNode, params)) {
io.add({ ...node });
}
},
exit(node, parentNode) {
if (is(node, parentNode, params)) {
parentNode.children = parentNode.children.filter(
(child) => child !== node,
);
}
},
},
root: {
enter() {
io.clear();
},
exit(node) {
if (io.size > 0) {
const newNode = {
type: 'element',
name: 'g',
attributes: {},
children: [...io],
};

if (params.before) {
node.children[0].children.splice(1, 0, newNode);
} else {
node.children[0].children.push(newNode);
}

io.clear();
}
},
},
};
};
33 changes: 33 additions & 0 deletions lib/plugins/remove-empty-path.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const name = 'removeEmptyPath';

function is(node) {
return (
node.name === 'path' &&
(node.attributes.fill === 'none' ||
node.attributes.fill === 'transparent') &&
!node.attributes.stroke
);
}

export const fn = () => {
return {
element: {
enter: (node) => {
if (node.attributes.fill === 'transparent') {
node.attributes.fill = 'none';
}

if (node.attributes.stroke) {
node.attributes.fill ||= 'none';
}
},
exit(node, parentNode) {
if (is(node)) {
parentNode.children = parentNode.children.filter(
(child) => child !== node,
);
}
},
},
};
};
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svgo-config",
"version": "0.6.0",
"version": "0.7.0",
"description": "A svgo config to keep svg files clean",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -39,28 +39,28 @@
"test": "ava --fail-fast"
},
"devDependencies": {
"@bring-it/npm": "^0.5.1",
"@bring-it/npm": "^0.5.2",
"@nice-move/cli": "^0.11.3",
"@nice-move/eslint-config-base": "^0.11.1",
"@nice-move/prettier-config": "^0.10.0",
"ava": "^6.1.0",
"eslint": "^8.56.0",
"ava": "^6.1.2",
"eslint": "^8.57.0",
"eslint-plugin-ava": "^14.0.0",
"garou": "^0.6.23",
"prettier": "^3.2.4",
"garou": "^0.7.1",
"prettier": "^3.2.5",
"svgo": "^3.2.0"
},
"peerDependencies": {
"svgo": "^3.2.0"
},
"engines": {
"node": ">=18.12.0 || ^14.17.0 || ^16.13.0"
"node": ">=20.0.0 || ^16.13.0 || ^18.12.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"packageManager": "pnpm@8.14.1",
"packageManager": "pnpm@8.15.4",
"eslintConfig": {
"extends": "@nice-move/eslint-config-base"
},
Expand Down
Loading

0 comments on commit e861353

Please sign in to comment.