forked from catppuccin/userstyles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized-svgs.js
More file actions
88 lines (77 loc) 路 2.54 KB
/
Copy pathoptimized-svgs.js
File metadata and controls
88 lines (77 loc) 路 2.54 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
88
import stylelint from "stylelint";
import valueParser from "postcss-value-parser";
import { optimize } from "svgo";
const {
createPlugin,
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = "catppuccin/optimized-svgs";
const meta = {
fixable: true,
};
const messages = ruleMessages(ruleName, {
rejected: () => `Unoptimized SVG detected`,
});
/** @type {import('npm:stylelint').Rule} */
const ruleFunction = (primary, _secondary, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
});
if (!validOptions) return;
root.walkRules((rule) => {
for (const node of rule.nodes) {
if (node.type === "rule") {
for (const subnode of node.nodes) {
if (subnode.type === "atrule" && subnode.name === "svg") {
const parsed = valueParser(subnode.value);
if (
parsed.nodes.length === 1 &&
parsed.nodes[0].type === "function" &&
parsed.nodes[0].value === "escape" &&
parsed.nodes[0].nodes.length === 1 &&
parsed.nodes[0].nodes[0].type === "string"
) {
const svg = parsed.nodes[0].nodes[0].value;
const optimized = optimize(svg, {
multipass: true,
plugins: [
"cleanupAttrs",
"cleanupIds",
"collapseGroups",
"convertPathData",
"convertTransform",
"convertStyleToAttrs",
"mergePaths",
"removeComments",
"removeUselessDefs",
"removeScriptElement",
],
}).data;
if (optimized !== svg) {
if (context.fix) {
parsed.nodes[0].nodes[0].quote = "'";
parsed.nodes[0].nodes[0].value = optimized;
subnode.value = parsed.toString();
} else {
report({
result,
ruleName,
message: messages.rejected(),
node: subnode,
});
}
}
}
}
}
}
}
});
};
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default createPlugin(ruleName, ruleFunction);