-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathplugins.ts
More file actions
87 lines (79 loc) 路 2.46 KB
/
Copy pathplugins.ts
File metadata and controls
87 lines (79 loc) 路 2.46 KB
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
79
80
81
82
83
84
85
86
87
import { PluginConfig } from 'svgo';
const setRootIoniconClass: PluginConfig = {
name: 'addClassesToSVGElement',
params: {
className: 'ionicon',
},
};
const addFillNoneCss: PluginConfig = {
name: 'addFillNoneCss',
fn: () => ({
element: {
enter: (element) => {
if (element.attributes.fill) {
if (element.attributes.fill === 'none') {
element.attributes.class = [...(element.attributes.class?.split(' ') || []), 'ionicon-fill-none'].join(' ');
}
delete element.attributes.fill;
}
if (element.attributes.stroke) {
delete element.attributes.stroke;
}
if (
element.attributes['stroke-width'] &&
(element.attributes['stroke-width'] === '32px' || element.attributes['stroke-width'] === '32')
) {
delete element.attributes['stroke-width'];
element.attributes.class = [...(element.attributes.class?.split(' ') || []), 'ionicon-stroke-width'].join(
' ',
);
}
},
},
}),
};
const forceCurrentColor: PluginConfig = {
name: 'forceCurrentColor',
fn: () => ({
element: {
enter: (element) => {
const attr = element.attributes.stroke || element.attributes.fill;
const attrName = element.attributes.stroke ? 'stroke' : element.attributes.fill ? 'fill' : undefined;
if (attrName) {
if (attr === '#000' || attr === 'currentColor') {
element.attributes[attrName] = 'currentColor';
} else if (attr !== 'none') {
throw new Error(`invalid "${attrName}" value: ${element.attributes[attrName]}`);
}
}
},
},
}),
};
const validatePlugin: PluginConfig = {
name: 'validate',
fn: () => ({
element: {
enter: (element) => {
if (element.attributes.style) {
console.warn(
`Inline style attributed detected: <${element.name} style="${element.attributes.style}">...</${element.name}>`,
);
}
if (element.name === 'style') {
console.warn('Inline style element detected');
}
},
},
}),
};
const basePlugins: PluginConfig[] = [
'removeStyleElement',
'convertStyleToAttrs',
'removeScripts',
'removeDimensions',
setRootIoniconClass,
validatePlugin,
];
export const webComponentPassPlugins: PluginConfig[] = [...basePlugins, addFillNoneCss];
export const sourcePassPlugins: PluginConfig[] = [...basePlugins, forceCurrentColor];