-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (31 loc) · 1.24 KB
/
index.ts
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
const map = new Map();
Array.from(document.querySelectorAll("style")).map((style) => {
const lines = style.innerHTML.split("\n").map((l) => l.trim());
let toSkip = 0;
lines.forEach((line, i) => {
if (toSkip) return toSkip--;
const group = line.trim().match(/^@group\s+([A-Za-z_-]+)\s+\{/)?.[0];
if (!group) return;
const block = lines.slice(i + 1, i + lines.slice(i).indexOf(lines.slice(i).find((l) => l.trim().endsWith("}")) !));
toSkip = block.length + 1;
const name = group.split("{")[0].slice("@group".length).trim();
return map.set(name, block);
});
return { style, lines };
}).map(({ style, lines }) => {
let toSkip = 0;
lines.forEach((line, i) => {
if (toSkip) return toSkip--;
const groups = line.trim().match(/^groups\s*:(.+);$/)?.[0];
if (!groups) return;
const names = groups.split(":")[1].trim().slice(0, -1).split(/\s+/);
lines.splice(i, 1);
return names.forEach((name) => {
const block = map.get(name);
if (!block) return;
lines.splice(i, 0, ...block);
toSkip += block.length + 1;
});
});
return (style.innerHTML = lines.join("\n"));
});