Repository navigation
Expand file tree
/
Copy pathprocess-icon.js
More file actions
98 lines (87 loc) · 1.95 KB
/
Copy pathprocess-icon.js
File metadata and controls
98 lines (87 loc) · 1.95 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
89
90
91
92
93
94
95
96
97
98
// Performs a few transformations on SVG icons:
//
// - Removes the Inkscape editor metadata
// - Removes all inline styles on <path>s (added by Inkscape)
// - Applies color-scheme-sensitive styling to each <path> via CSS
//
// Run it like:
// node process-icon.js <icon.svg>
//
// The revised SVG is printed to stdout.
import {optimize} from "svgo";
import {readFileSync} from "node:fs";
const light_theme = readFileSync(
import.meta.dirname + "/styles/themes/light.less",
{encoding: "utf8"},
);
const dark_theme = readFileSync(
import.meta.dirname + "/styles/themes/dark.less",
{encoding: "utf8"},
);
function find_css_var(v, css) {
return css
.split("\n")
.find(l => l.includes(`--${v}`))
.replace(/^.*: */g, "")
.replace(/;.*$/g, "");
}
const light_fg = find_css_var("page-fg", light_theme);
const dark_fg = find_css_var("page-fg", dark_theme);
// Colors come from styles/themes/*.less
const CSS = `
path {
fill: #808080;
}
@media (prefers-color-scheme: light) {
path {
fill: ${light_fg};
}
}
@media (prefers-color-scheme: dark) {
path {
fill: ${dark_fg};
}
}
`
.replace(/[\r\n]+/g, " ")
.replace(/ +/g, " ")
.trim();
const stripStyles = {
name: "stripStyles",
type: "visitor",
fn() {
return {
element: {
enter(node) {
if (node.name === "path") delete node.attributes.style;
},
},
};
},
};
const injectCSS = {
name: "injectCSS",
type: "visitor",
fn() {
return {
element: {
enter(node) {
if (node.name !== "svg") return;
node.children.unshift({
type: "element",
name: "style",
attributes: {},
children: [{type: "text", value: CSS}],
});
},
},
};
},
};
const svg_path = process.argv[2];
const svg = readFileSync(svg_path, {encoding: "utf8"});
process.stdout.write(
optimize(svg, {
plugins: ["removeEditorsNSData", stripStyles, injectCSS],
}).data,
);