From bfd40662076a8b802a3330d49a034372967b1ec0 Mon Sep 17 00:00:00 2001 From: Aaron Reisman Date: Mon, 4 Dec 2017 21:15:48 +0700 Subject: [PATCH] feat: add option to keeps IDs from SVG --- README.md | 11 ++++++++++ src/cli/__snapshots__/index.test.js.snap | 27 ++++++++++++++++++++++++ src/cli/index.js | 1 + src/cli/index.test.js | 5 +++++ src/configToOptions.js | 2 ++ 5 files changed, 46 insertions(+) diff --git a/README.md b/README.md index c73b7ce8..3e93805c 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ powerful and configurable HTML transpiler. It uses AST (like --no-prettier disable Prettier --template specify a custom template to use --no-expand-props disable props expanding + --ids keep ids within the svg --icon use "1em" as width and height --replace-attr-value [old=new] replace an attribute value -p, --precision set the number of digits in the fractional part (svgo) @@ -289,6 +290,16 @@ Setting this to `false` will remove the viewBox property. | ------- | --------------- | ----------------- | | `true` | `--no-view-box` | `viewBox: ` | +### Ids + +Setting this to `true` will keep ids. It can be useful to target specific ids +using CSS or third party library (eg: +[react-mutate-icon](https://github.com/lifeiscontent/react-mutate-icon)). + +| Default | CLI Override | API Override | +| ------- | ------------ | ------------- | +| `false` | `--ids` | `ids: ` | + ### Replace attribute value Replace an attribute value by an other. The main usage of this option is to diff --git a/src/cli/__snapshots__/index.test.js.snap b/src/cli/__snapshots__/index.test.js.snap index 9e9002db..25df2c6b 100644 --- a/src/cli/__snapshots__/index.test.js.snap +++ b/src/cli/__snapshots__/index.test.js.snap @@ -14,6 +14,33 @@ export default One; " `; +exports[`cli --ids 1`] = ` +"import React from \\"react\\"; + +const One = props => ( + + Rectangle 5 + + + + + + + + + + + + + + +); + +export default One; + +" +`; + exports[`cli --no-expand-props 1`] = ` "import React from \\"react\\"; diff --git a/src/cli/index.js b/src/cli/index.js index 1ef98f16..bae447c5 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -23,6 +23,7 @@ program .option('--no-prettier', 'disable Prettier') .option('--template ', 'specify a custom template to use') .option('--no-expand-props', 'disable props expanding') + .option('--ids', 'keep ids within the svg') .option('--icon', 'use "1em" as width and height') .option('--no-view-box', 'remove viewBox') .option( diff --git a/src/cli/index.test.js b/src/cli/index.test.js index 513a1b9f..49b8af22 100644 --- a/src/cli/index.test.js +++ b/src/cli/index.test.js @@ -40,6 +40,11 @@ describe('cli', () => { expect(stdout).toMatchSnapshot() }) + it('--ids', async () => { + const [stdout] = await exec('babel-node src/cli --ids __fixtures__/one.svg') + expect(stdout).toMatchSnapshot() + }) + it('--no-view-box', async () => { const [stdout] = await exec( 'babel-node src/cli --no-view-box __fixtures__/one.svg', diff --git a/src/configToOptions.js b/src/configToOptions.js index 64ff6110..2c8caacf 100644 --- a/src/configToOptions.js +++ b/src/configToOptions.js @@ -15,6 +15,7 @@ const defaultConfig = { replaceAttrValues: [], expandProps: true, title: true, + ids: false, precision: 3, // default to svgo semi: undefined, // default to prettier singleQuote: undefined, // default to prettier @@ -46,6 +47,7 @@ function configToOptions(config = {}) { if (!config.title || config.icon) plugins.push({ removeTitle: {} }) else if (config.title) plugins.push({ removeTitle: false }) if (config.viewBox) plugins.push({ removeViewBox: false }) + if (config.ids) plugins.push({ cleanupIDs: { remove: false } }) if (config.precision === 'number') svgoConfig.floatPrecision = Number(svgoConfig.precision) return svgoConfig