forked from MrWangJustToDo/git-diff-view
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.mjs
More file actions
38 lines (30 loc) · 960 Bytes
/
Copy pathgen.mjs
File metadata and controls
38 lines (30 loc) · 960 Bytes
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
import fs from "fs";
import path from "path";
import { parse } from "postcss";
import process from "process";
const readCssFile = async (filePath) => {
const fullPath = path.resolve(process.cwd(), filePath);
return await fs.promises.readFile(fullPath, { encoding: "utf-8" });
};
function cssToJson(cssString) {
const root = parse(cssString);
const result = {};
root.walkRules((rule) => {
const styles = {};
rule.walkDecls((decl) => {
styles[decl.prop] = decl.value;
});
rule.selectors.forEach((selector) => {
if (selector.includes(" ")) return;
result[selector.startsWith(".") ? selector.slice(1) : selector] = styles;
});
});
return result;
}
const start = async () => {
const cssFilePath = "node_modules/highlight.js/styles/github-dark.css";
const cssContent = await readCssFile(cssFilePath);
const re = cssToJson(cssContent);
console.log("CSS to JSON conversion result:", re);
};
start();