-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerateleetcode.js
203 lines (180 loc) · 5.59 KB
/
generateleetcode.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const Utils = require("./utils");
const Logger = require("./logger");
const {
SUPPORT_LANGUAGE,
DB_JSON_OUTPUT_DIR,
RAW_MARKDOWN_OUTPUT_DIR,
CRAWL_IGNORE_SUFFIX,
} = require("./constants");
const COMPANY_MAPPER = {
阿里: "阿里巴巴",
字节: "字节跳动",
};
const genertateLeetcodeToJson = () => {
console.time("genertateLeetcodeToJson");
const rawMarkdowns = Utils.getDirsFileNameSync(
RAW_MARKDOWN_OUTPUT_DIR
).filter(
(name) => !CRAWL_IGNORE_SUFFIX.some((suffix) => name.endsWith(suffix))
);
rawMarkdowns.forEach((filename) => {
const languageResloved = [];
const preKnowledge = [];
let companies = [];
let keyPoints = [];
let markdown;
try {
Logger.success(`开始读取${filename}`);
markdown = Utils.readFileSync(RAW_MARKDOWN_OUTPUT_DIR, filename);
Logger.success(`读取${filename}完毕`);
} catch (error) {
Logger.error(`读取${filename}失败`, error);
}
/**
* 以下替换是为了统一markdown语言标识
*/
markdown = markdown.replace(/```javascript/g, "```js");
markdown = markdown.replace(/```python/g, "```py");
markdown = markdown.replace(/```c\+\+/g, "```cpp");
SUPPORT_LANGUAGE.forEach((lang) => {
markdown.replace(Utils.genCodeRegByLang(lang), (noUseMatch, $1) => {
languageResloved.push({
language: lang,
text: $1,
});
});
});
// 解析前置知识
markdown.replace(Utils.getSatelliteDataReg().pre, (noUseMatch, $1) => {
$1.replace(/^-/gm, "")
.split("\n")
.map((tag) => tag.trim())
.filter(Boolean)
.forEach((preTagName) => {
// 形式1: [滑动窗口](https://github.com/azl397985856/leetcode/blob/master/thinkings/slidewindow.md)
// 形式2: 滑动窗口
const match = /\[(.+)\]\((.+)\)/.exec(preTagName);
if (match) {
// 形式1
// 有可能有相对定位的文件(比如 ../thinkings),需要特别处理
preKnowledge.push({
text: match[1],
link: match[2].replace(
"../thinkings/",
"https://github.com/azl397985856/leetcode/blob/master/thinkings/"
),
color: Utils.getColor(match[1]),
});
} else {
// 形式 2
preKnowledge.push({
text: preTagName,
link: null,
color: Utils.getColor(preTagName),
});
}
});
});
// 解析公司
markdown.replace(
Utils.getSatelliteDataReg().companies,
(noUseMatch, $1) => {
$1.replace(/^-/gm, "")
.split("\n")
.map((company) => company.trim())
.filter(Boolean)
.filter((company) => company !== "暂无")
.map((company) => {
// 统一公司名字
if (company in COMPANY_MAPPER) return COMPANY_MAPPER[company];
return company;
})
.forEach((company) => {
// 形式1: 百度,阿里
// 形式2: 百度
const match = /u3001/.exec(company);
if (match) {
// 形式1
companies = companies.concat(
company.split("\u3001").map((c) => ({ name: c }))
);
} else {
// 形式 2
companies.push({
name: company,
});
}
});
}
);
// 解析关键点
markdown.replace(
Utils.getSatelliteDataReg().keyPoints,
(noUseMatch, $1) => {
keyPoints = $1
.replace(/\s/g, "")
.split("-")
.filter((s) => s && s !== "\u89e3\u6790")
.map((s) => ({ text: s, link: null, color: "blue" }));
}
);
/**
* TODO 这边解析字段不全
*/
const [questionID, name] = filename.split(".");
let oCustomStruct = {
id: questionID,
name,
pre: preKnowledge,
keyPoints,
companies,
giteeSolution: `https://gitee.com/golong/leetcode/blob/master/problems/${filename}`,
solution: `https://github.com/azl397985856/leetcode/blob/master/problems/${filename}`,
code: languageResloved,
};
console.log(oCustomStruct);
Logger.success(`开始生成 "${filename}"`);
Utils.writeFileSync(
"spider/yield-db-json",
`${filename}.json`,
JSON.stringify(oCustomStruct, null, 2)
);
Logger.success(`生成 "${filename}" 完毕`);
});
console.timeEnd("genertateLeetcodeToJson");
};
const generateCollectionIndexFile = () => {
Logger.success("\u5f00\u59cb\u751f\u4ea7index\u6587\u4ef6");
console.time("generateCollectionIndexFile");
const jsonsName = Utils.getDirsFileNameSync(DB_JSON_OUTPUT_DIR).sort(
(a, b) => {
const numberA = a.split(".")[0]; // 题号
const numberB = b.split(".")[0]; // 题号
return Number(numberA) - Number(numberB);
}
);
let rootContent = `
export const db_collection = {
${jsonsName.reduce((acc, next) => {
return (
acc +
('"' +
next.split(".")[1] +
'":' +
JSON.stringify(
require("../spider/yield-db-json/" + next),
null,
4
) +
",\n")
);
}, "")}
}
`;
Utils.writeFileSync("src/db", "root.db.js", rootContent);
Logger.success("index\u6587\u4ef6\u751f\u6210\u5b8c\u6bd5");
console.timeEnd("generateCollectionIndexFile");
};
Utils.mkdirSync(DB_JSON_OUTPUT_DIR);
genertateLeetcodeToJson();
generateCollectionIndexFile();