-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadd_classnames.svgo-plugin.js
78 lines (74 loc) · 1.81 KB
/
add_classnames.svgo-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
exports.name = "addClassNamesPlugin";
exports.type = "perItem";
exports.active = false;
exports.description = "add classnames based on attributes and values";
exports.params = {
rules: [],
};
/**
* Add class names based on given attributes and values
*
* @param rules:
*
* format: [ { attribute: 'attributeName', value: 'attribute-value, className: 'class-name-to-add' } ]
*
*
* example:
*
* > Add two different class name based on fill attribute and it's value
* ---
* rules: [
* {
* attribute: 'fill',
* value: '#A5ADB1',
* className: 'fi-icon-illustrative-base-stroke'
* },
* {
* attribute: 'fill',
* value: '#E97025',
* className: 'fi-icon-illustrative-highlight-fill'
* }
* ]
*
*
* > Add same class name based on two separate values
* ---
* rules: [
* {
* attribute: 'fill',
* value: '#A5ADB1|#A5ADD1',
* className: 'fi-icon-illustrative-base-stroke'
* }
* ]
*
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*
* Based on removeAttrs: https://github.com/svg/svgo/blob/master/plugins/removeAttrs.js
*
* @author Sami Korpela
*/
exports.fn = function (item, params) {
console.log("function start");
var patternMap = params.rules.map(function (rule) {
return {
pattern: [
new RegExp(["^", rule.attribute, "$"].join(""), "i"),
new RegExp(["^", rule.value, "$"].join(""), "i"),
],
className: rule.className,
};
});
patternMap.forEach(function (pm) {
if (item.attrs) {
for (const [name, value] of Object.entries(item.attrs)) {
if (pm.pattern[0].test(name) && pm.pattern[1].test(value.value)) {
item.class.add(pm.className);
}
}
}
});
};