prepare-svgs-for-icon-block 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/bin/prepare-svgs.js +66 -0
- package/lib/process.js +66 -0
- package/package.json +32 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 James Hunt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Prepare SVGs for The Icon Block for WordPress
|
2
|
+
|
3
|
+
[The Icon Block](https://wordpress.org/plugins/icon-block/) plugin by [Nick Diego](https://nickdiego.com) registers a single, easy-to-use block that allows you to add custom SVG icons and graphics to the WordPress block editor (Gutenberg).
|
4
|
+
|
5
|
+
It's a great plugin and an essential on any WordPress site I build (๐๐ป Hello, [I'm James](https://www.thetwopercent.co.uk)) but all the sites I create need custom icon libraries. Adding custom icons in bulk to The Icon Block is a bit tiring, so I made this utility to spit out a folder of icons in a JS file ready for you to import in to your icon registration file.
|
6
|
+
|
7
|
+
Install this npm package on your system and you'll get a zero-config CLI to convert raw SVG files into clean, optimized, ready-to-use icons for your WordPress site.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- ๐พ Exports ready-to-use JavaScript for The Icon Block
|
12
|
+
- โ
Bulk SVG processing โ point it at a folder, done.
|
13
|
+
- ๐งน Removes bloat โ strips out background fills, comments, metadata, etc.
|
14
|
+
- ๐ฆ SVGO-powered โ optimized using preset-default for max compression + clarity.
|
15
|
+
- ๐ง Smart naming โ auto-generates name and title from filenames.
|
16
|
+
- ๐จ Works with Framed icons (use Advanced options)
|
17
|
+
- ๐งน Clean path fills โ adds your chosen fill to all icons.
|
18
|
+
- โก๏ธ No config required โ runs out of the box with good defaults.
|
19
|
+
- ๐ฅ๏ธ Cross-platform โ works on macOS, Linux, and Windows.
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Install globally via npm:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
npm install --global prepare-svgs-for-icon-block
|
27
|
+
```
|
28
|
+
|
29
|
+
This will make the command available anywhere on your system.
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
After you've installed you can navigate to the folder of SVGs you want to convert, and run:
|
34
|
+
|
35
|
+
```sh
|
36
|
+
$ prepare-svgs
|
37
|
+
```
|
38
|
+
|
39
|
+
The follow the prompts.
|
40
|
+
|
41
|
+
The script is non-destructive on the original SVGs, it doesnt touch them or edit them in anyway.
|
42
|
+
|
43
|
+
## Output
|
44
|
+
|
45
|
+
Whichever folder you run the script in will get an output.js file in it, which will include all your optimised SVGs.
|
46
|
+
|
47
|
+
## Adding custom libraries to The Icon Block
|
48
|
+
|
49
|
+
The Icons Block is a static block built with React. Therefore, icons need to be added using JavaScript. Nick Diego has written a great article on [how to add custom icons](https://nickdiego.com/adding-custom-icons-to-the-icon-block/). This script will provide your icons for the part that is specifed:
|
50
|
+
|
51
|
+
```js
|
52
|
+
const customIcons = []
|
53
|
+
```
|
54
|
+
|
55
|
+
## Update prepare-svgs-for-icon-block
|
56
|
+
|
57
|
+
To update to the latest version you can just run the install command again - `npm install -g prepare-svgs-for-icon-block`
|
58
|
+
|
59
|
+
## Feedback and issues
|
60
|
+
|
61
|
+
Please open an issue in the [GitHub repo](https://github.com/thetwopct/prepare-svgs-for-icon-block/issues). Thanks.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
66
|
+
|
67
|
+
## Changelog
|
68
|
+
|
69
|
+
1.0.0
|
70
|
+
Initial release
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
import inquirer from 'inquirer';
|
4
|
+
import { processSvgs } from '../lib/process.js';
|
5
|
+
import path from 'path';
|
6
|
+
import { fileURLToPath } from 'url';
|
7
|
+
|
8
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
9
|
+
|
10
|
+
const questions = [
|
11
|
+
{
|
12
|
+
type: 'input',
|
13
|
+
name: 'directory',
|
14
|
+
message: 'Folder of SVGs:',
|
15
|
+
default: process.cwd(),
|
16
|
+
},
|
17
|
+
{
|
18
|
+
type: 'confirm',
|
19
|
+
name: 'advanced',
|
20
|
+
message: 'Advanced options:',
|
21
|
+
default: false,
|
22
|
+
}
|
23
|
+
];
|
24
|
+
|
25
|
+
const answers = await inquirer.prompt(questions);
|
26
|
+
|
27
|
+
let options = {
|
28
|
+
directory: path.resolve(answers.directory),
|
29
|
+
framedIcons: false,
|
30
|
+
forcePathColor: false,
|
31
|
+
fillColor: '#252321'
|
32
|
+
};
|
33
|
+
|
34
|
+
if (answers.advanced) {
|
35
|
+
const advancedAnswers = await inquirer.prompt([
|
36
|
+
{
|
37
|
+
type: 'confirm',
|
38
|
+
name: 'framedIcons',
|
39
|
+
message: 'Framed Icons - remove fills from outer frame:',
|
40
|
+
default: false,
|
41
|
+
},
|
42
|
+
{
|
43
|
+
type: 'confirm',
|
44
|
+
name: 'forcePathColor',
|
45
|
+
message: 'Force Path color:',
|
46
|
+
default: false,
|
47
|
+
}
|
48
|
+
]);
|
49
|
+
|
50
|
+
options.framedIcons = advancedAnswers.framedIcons;
|
51
|
+
options.forcePathColor = advancedAnswers.forcePathColor;
|
52
|
+
|
53
|
+
if (advancedAnswers.forcePathColor) {
|
54
|
+
const colorAnswer = await inquirer.prompt([
|
55
|
+
{
|
56
|
+
type: 'input',
|
57
|
+
name: 'fillColor',
|
58
|
+
message: 'Fill color for <path>:',
|
59
|
+
default: '#252321',
|
60
|
+
}
|
61
|
+
]);
|
62
|
+
options.fillColor = colorAnswer.fillColor;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
processSvgs(options);
|
package/lib/process.js
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
import { optimize } from 'svgo';
|
4
|
+
import chalk from 'chalk';
|
5
|
+
|
6
|
+
export function processSvgs(options) {
|
7
|
+
const { directory: svgDir, framedIcons, forcePathColor, fillColor } = options;
|
8
|
+
const output = [];
|
9
|
+
const files = fs.readdirSync(svgDir);
|
10
|
+
|
11
|
+
files.forEach(file => {
|
12
|
+
if (!file.endsWith('.svg')) return;
|
13
|
+
|
14
|
+
const filePath = path.join(svgDir, file);
|
15
|
+
let svg = fs.readFileSync(filePath, 'utf8');
|
16
|
+
|
17
|
+
const { data: optimised } = optimize(svg, {
|
18
|
+
path: filePath,
|
19
|
+
multipass: true,
|
20
|
+
plugins: [
|
21
|
+
{
|
22
|
+
name: 'preset-default',
|
23
|
+
params: {
|
24
|
+
overrides: {
|
25
|
+
removeViewBox: false
|
26
|
+
}
|
27
|
+
}
|
28
|
+
},
|
29
|
+
],
|
30
|
+
});
|
31
|
+
|
32
|
+
let cleaned = optimised;
|
33
|
+
|
34
|
+
// Apply framed icons transformations if enabled
|
35
|
+
if (framedIcons) {
|
36
|
+
cleaned = cleaned
|
37
|
+
.replace(/<svg([^>]*)\sfill="[^"]*"/, '<svg$1')
|
38
|
+
.replace(/<rect([^>]*)\sfill="[^"]*"/, '<rect$1')
|
39
|
+
.replace(/<path([^>]*?)\sfill="[^"]*"/g, '<path$1');
|
40
|
+
}
|
41
|
+
|
42
|
+
// Apply force path color if enabled
|
43
|
+
if (forcePathColor) {
|
44
|
+
cleaned = cleaned.replace(/<path([^>]*?)>/g, `<path fill="${fillColor}"$1>`);
|
45
|
+
}
|
46
|
+
|
47
|
+
const rawName = path.basename(file, '.svg');
|
48
|
+
const name = rawName.toLowerCase().replace(/\s+/g, '-');
|
49
|
+
const title = rawName.replace(/[-_]/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
|
50
|
+
|
51
|
+
output.push({ name, title, icon: cleaned });
|
52
|
+
});
|
53
|
+
|
54
|
+
const outPath = path.join(svgDir, 'output.js');
|
55
|
+
const outputJs = output
|
56
|
+
.map(({ name, title, icon }) => {
|
57
|
+
return `\t{\n\t\tname: '${name}',\n\t\ttitle: '${title}',\n\t\ticon: '${icon}'\n\t}`;
|
58
|
+
})
|
59
|
+
.join(',\n');
|
60
|
+
|
61
|
+
fs.writeFileSync(
|
62
|
+
path.join(svgDir, 'output.js'),
|
63
|
+
`const customIcons = [\n${outputJs}\n];\n`
|
64
|
+
);
|
65
|
+
console.log(chalk.green(`โ
Saved to ${outPath}`));
|
66
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"name": "prepare-svgs-for-icon-block",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "Zero-config CLI to convert raw SVG files into clean, optimized, ready-to-use icons suitable for The Icon Block for WordPress",
|
5
|
+
"author": "James Hunt",
|
6
|
+
"license": "MIT",
|
7
|
+
"bin": {
|
8
|
+
"prepare-svgs": "./bin/prepare-svgs.js"
|
9
|
+
},
|
10
|
+
"dependencies": {
|
11
|
+
"chalk": "^5.4.1",
|
12
|
+
"inquirer": "^9.3.7",
|
13
|
+
"svgo": "^3.3.2"
|
14
|
+
},
|
15
|
+
"type": "module",
|
16
|
+
"keywords": [
|
17
|
+
"svg",
|
18
|
+
"icon",
|
19
|
+
"wordpress",
|
20
|
+
"cli",
|
21
|
+
"optimize",
|
22
|
+
"the-icon-block"
|
23
|
+
],
|
24
|
+
"engines": {
|
25
|
+
"node": ">=18"
|
26
|
+
},
|
27
|
+
"files": [
|
28
|
+
"bin/",
|
29
|
+
"lib/",
|
30
|
+
"README.md"
|
31
|
+
]
|
32
|
+
}
|