-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
110 lines (96 loc) · 3.29 KB
/
util.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
const fs = require("fs");
function replaceInvalidChars(filename, recover = false) {
// https://mythofechelon.co.uk/blog/2020/3/6/how-to-work-around-windows-restricted-characters
const replaceMap = [
['/', '∕'],
['\\', '⧵'],
[':', '꞉'],
['*', '⁎'],
['?', '?'],
['"', '”'],
['<', '<'],
['>', '>'],
['|', '⏐']
];
for (let [old, replacement] of replaceMap) {
filename = recover ? filename.replaceAll(replacement, old) : filename.replaceAll(old, replacement);
}
return filename;
}
function getFilenameFromLevels(filename, levels) {
let prefix = '';
for (let l of levels) {
prefix += `${l}.`;
}
return (levels.length === 1 ? prefix : prefix.slice(0, -1)) + ' ' + filename;
}
function getLevelsFromFilename(filename) {
const levels = filename.trim().split(' ')[0].split('.');
return levels.at(-1) === '' ? levels.slice(0, -1) : levels;
}
function getIndentLevel(line) {
return line.match(/^\t*/)[0].length;
}
function removeLeadingNumbers(line) {
const regExp = /^\d+(\.\d+)*\.? /;
const result = line.match(regExp);
if (!result) {
return line;
}
// 对于 '123 title' 这种情况,不替换,只替换 '123. title' 这种
if (!result[0].endsWith('. ') && result[1] === undefined) {
return line;
}
return line.replace(regExp, '');
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
function checkIndentErrors(filename, corrected = false) {
const contents = fs.readFileSync(filename, 'utf8');
let prevIndent = -1;
let lines = contents.split('\n');
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let indent = getIndentLevel(line);
if (indent > prevIndent + 1) {
if (corrected) {
let wrongIndent = indent;
const start = i;
indent = prevIndent + 1;
lines[i] = '\t'.repeat(indent) + lines[i].trim();
while (i + 1 < lines.length && getIndentLevel(lines[i + 1]) === wrongIndent) {
i++;
lines[i] = '\t'.repeat(indent) + lines[i].trim();
}
console.log(`Indent corrected line ${start} -> ${i}: ${lines[i].trim()}`);
} else {
console.log(`Indent error line ${i} -> ${i + 1}: ${line.trim()}`);
}
}
prevIndent = indent;
}
fs.writeFileSync(filename, lines.join('\n'));
console.log('Indent check done.');
}
function checkDuplicateLines(filename) {
const contents = fs.readFileSync(filename, 'utf8');
const lines = contents.split('\n');
let prevLine = '';
for (let i = 0; i < lines.length; i++) {
const line = removeLeadingNumbers(lines[i].trim());
if (line === prevLine) {
console.log(`Duplicate line: ${i} -> ${i + 1}: ${line}`);
}
prevLine = line;
}
console.log('Duplicate check done.');
}
module.exports = {
replaceInvalidChars,
getFilenameFromLevels,
getLevelsFromFilename,
getIndentLevel,
removeLeadingNumbers,
checkIndentErrors,
checkDuplicateLines,
sleep,
};