-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (50 loc) · 1.56 KB
/
index.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
"use strict";
const collections = require("svgo/plugins/_collections");
exports.type = "visitor";
exports.name = "addClassesToColors";
exports.active = true;
exports.description =
"Add corresponding class to element that has specific colors";
const regHEX = /^#([a-fA-F0-9]){3,6}$/;
function findMapping(mapping, name) {
const result = mapping[name];
if (typeof result === "object") {
return result;
}
if (typeof result !== "string" || !collections.colorsProps.includes(result)) {
return {};
}
return findMapping(mapping, result);
}
/**
* Add corresponding class to element that has specific colors.
*
* @author Jiajun Chen
*
* @type {import('svgo/lib/types').Plugin<{
* mapping?: Record<string, string>,
* }>}
*/
exports.fn = (_root, params) => {
const { mapping = {} } = params;
return {
element: {
enter: (node) => {
const classes = new Set(node.attributes.class?.split(" "));
for (const [name, oriVal] of Object.entries(node.attributes)) {
if (collections.colorsProps.includes(name)) {
const value = oriVal.match(regHEX) != null ? oriVal.toLowerCase() : oriVal;
const mappedClass = mapping[value];
if (typeof mappedClass === "string") classes.add(mappedClass);
else {
const mappedClass = findMapping(mapping, name)[value];
if (typeof mappedClass === "string") classes.add(mappedClass);
}
}
}
if (classes.size > 0)
node.attributes.class = [...classes].sort().join(" ");
},
},
};
};