Skip to content

Commit bfd4066

Browse files
lifeiscontentgregberge
authored andcommitted
feat: add option to keeps IDs from SVG
1 parent 82d5350 commit bfd4066

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ powerful and configurable HTML transpiler. It uses AST (like
8989
--no-prettier disable Prettier
9090
--template <file> specify a custom template to use
9191
--no-expand-props disable props expanding
92+
--ids keep ids within the svg
9293
--icon use "1em" as width and height
9394
--replace-attr-value [old=new] replace an attribute value
9495
-p, --precision <value> set the number of digits in the fractional part (svgo)
@@ -289,6 +290,16 @@ Setting this to `false` will remove the viewBox property.
289290
| ------- | --------------- | ----------------- |
290291
| `true` | `--no-view-box` | `viewBox: <bool>` |
291292

293+
### Ids
294+
295+
Setting this to `true` will keep ids. It can be useful to target specific ids
296+
using CSS or third party library (eg:
297+
[react-mutate-icon](https://github.com/lifeiscontent/react-mutate-icon)).
298+
299+
| Default | CLI Override | API Override |
300+
| ------- | ------------ | ------------- |
301+
| `false` | `--ids` | `ids: <bool>` |
302+
292303
### Replace attribute value
293304

294305
Replace an attribute value by an other. The main usage of this option is to

src/cli/__snapshots__/index.test.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ export default One;
1414
"
1515
`;
1616

17+
exports[`cli --ids 1`] = `
18+
"import React from \\"react\\";
19+
20+
const One = props => (
21+
<svg width={48} height={1} viewBox=\\"0 0 48 1\\" {...props}>
22+
<title>Rectangle 5</title>
23+
<g id=\\"Page-1\\" fill=\\"none\\" fillRule=\\"evenodd\\">
24+
<g id=\\"19-Separator\\" transform=\\"translate(-129 -156)\\" fill=\\"#063855\\">
25+
<g id=\\"Controls/Settings\\" transform=\\"translate(80)\\">
26+
<g id=\\"Content\\" transform=\\"translate(0 64)\\">
27+
<g id=\\"Group\\" transform=\\"translate(24 56)\\">
28+
<g id=\\"Group-2\\">
29+
<path id=\\"Rectangle-5\\" d=\\"M25 36h48v1H25z\\" />
30+
</g>
31+
</g>
32+
</g>
33+
</g>
34+
</g>
35+
</g>
36+
</svg>
37+
);
38+
39+
export default One;
40+
41+
"
42+
`;
43+
1744
exports[`cli --no-expand-props 1`] = `
1845
"import React from \\"react\\";
1946

src/cli/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ program
2323
.option('--no-prettier', 'disable Prettier')
2424
.option('--template <file>', 'specify a custom template to use')
2525
.option('--no-expand-props', 'disable props expanding')
26+
.option('--ids', 'keep ids within the svg')
2627
.option('--icon', 'use "1em" as width and height')
2728
.option('--no-view-box', 'remove viewBox')
2829
.option(

src/cli/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ describe('cli', () => {
4040
expect(stdout).toMatchSnapshot()
4141
})
4242

43+
it('--ids', async () => {
44+
const [stdout] = await exec('babel-node src/cli --ids __fixtures__/one.svg')
45+
expect(stdout).toMatchSnapshot()
46+
})
47+
4348
it('--no-view-box', async () => {
4449
const [stdout] = await exec(
4550
'babel-node src/cli --no-view-box __fixtures__/one.svg',

src/configToOptions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const defaultConfig = {
1515
replaceAttrValues: [],
1616
expandProps: true,
1717
title: true,
18+
ids: false,
1819
precision: 3, // default to svgo
1920
semi: undefined, // default to prettier
2021
singleQuote: undefined, // default to prettier
@@ -46,6 +47,7 @@ function configToOptions(config = {}) {
4647
if (!config.title || config.icon) plugins.push({ removeTitle: {} })
4748
else if (config.title) plugins.push({ removeTitle: false })
4849
if (config.viewBox) plugins.push({ removeViewBox: false })
50+
if (config.ids) plugins.push({ cleanupIDs: { remove: false } })
4951
if (config.precision === 'number')
5052
svgoConfig.floatPrecision = Number(svgoConfig.precision)
5153
return svgoConfig

0 commit comments

Comments
 (0)