-
Notifications
You must be signed in to change notification settings - Fork 6
/
upgrade-noteblock.js
92 lines (83 loc) · 1.88 KB
/
upgrade-noteblock.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
#!/bin/node
/*
* upgrade-noteblock.js - © 2024 @queengooborg
* Written by Queen Vinyl Da.i'gyu-Kazotetsu <https://www.queengoob.org>
* Script to replace old noteblock syntax with new, GFM syntax
*
* Requirements:
* - NodeJS
* - mdn/content or mdn/translated-content as CWD
*/
import fs from "node:fs/promises";
import path from "node:path";
const regex = (strings) => new RegExp(`( *)>\\s\\*\\*(${strings.join('|')})\\*\\*\\s(?![\\*\\[\`\\{_])(>\\s(?!>)|>(?=\\n))?`, 'g');
const types = {
"NOTE": [
"Note:",
"Hinweis:",
"Nota:",
"Note :",
"メモ:",
"참고:",
"Uwaga:",
"Nota:",
"Примечание:",
"备注:",
"備註:"
],
"WARNING": [
"Warning:",
"Warnung:",
"Advertencia:",
"Attention :",
"警告:",
"경고:",
"Ważne:",
"Aviso:",
"Предупреждение:",
"警告:",
"警告:"
],
"CALLOUT": [
"Callout:",
"Bemerkung:",
"Observación:",
"Remarque :",
"注目:",
"알림:",
"Obserwacja:",
"Observação:",
"Сноска:",
"标注:",
"標註:"
],
};
// https://gist.github.com/lovasoa/8691344
async function* walk(dir) {
for await (const d of await fs.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile() && d.name != ".DS_Store") yield entry;
}
}
// END SNIPPET
const main = async (filepath) => {
for await (const p of walk((filepath || './files'))) {
// Skip non-Markdown files
if (!(p.endsWith('.md'))) continue;
let contents = await fs.readFile(p, 'utf-8');
let changed = false;
for (const [type, strings] of Object.entries(types)) {
const re = regex(strings);
if (contents.match(re)) {
// If there's any match, replace all matches
contents = contents.replace(re, `$1> [!${type}]\n$1> `);
changed = true;
}
if (changed) {
await fs.writeFile(p, contents);
}
}
}
}
await main(process.argv[2]);