-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpostprocess.js
73 lines (58 loc) · 1.75 KB
/
postprocess.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
// this only runs when building!
import * as fs from "fs"
import * as path from "path"
function correctDir() {
process.chdir(path.dirname(process.argv[1]));
}
/**
* @param {string} dir
* @param {string} ext
* @returns {Promise<string[]>}
*/
async function listFiles(dir, ext) {
const entries = await fs.promises.readdir(dir, { withFileTypes: true })
const files = await Promise.all(entries.map((entry) => {
const res = path.resolve(dir, entry.name)
if (entry.isDirectory()) {
if (entry.name.endsWith("node_modules")) {
return []
} else {
return listFiles(res, ext)
}
} else {
return res
}
}))
return files.flat().filter(name => name.endsWith(ext))
}
//const RE = /<strong aria-hidden="true">[0-9]+\.[0-9]+\.[0-9]+\.<\/strong>/g
//const RE = /<\/strong>/mg
const RE = /<strong\b[^>]*>([0-9]+\.){3,}<\/strong>/g
async function main() {
correctDir()
const htmlFiles = await listFiles(path.join(process.cwd(), "book"), ".html")
await Promise.all(htmlFiles.map(f => {
/**
* @type {Promise<void>}
*/
const promise = new Promise((resolve, reject) => {
fs.readFile(f, (err, data) => {
if (err) {
reject(err)
} else {
let src = data.toString()
src = src.replace(RE, "")
fs.writeFile(f, src, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
}
})
})
return promise
}))
}
main()