-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrepl.mjs
43 lines (37 loc) · 968 Bytes
/
repl.mjs
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
import { parse } from '@textlint/markdown-to-ast';
import { readFileSync, writeFileSync } from 'fs';
// eslint-disable-next-line import/extensions
import repls from './repls_data.mjs';
const inputPath = process.argv[2];
const markdown = readFileSync(inputPath, 'utf8');
const ast = parse(markdown);
// какие блоки парсим
const replTypes = [
'Paragraph',
'BlockQuote',
'Header',
'List',
'Table',
];
let result = '';
ast.children.forEach((child, index) => {
if (replTypes.includes(child.type)) {
let tmpResult = child.raw;
repls.forEach((repl) => {
tmpResult = tmpResult.replaceAll(
repl.from,
repl.to
);
});
result += `${tmpResult}\n\n`;
} else {
result += `${child.raw}\n\n`;
}
// eslint-disable-next-line no-console
console.log(
`${index + 1}/${ast.children.length} replaced`
);
});
writeFileSync(inputPath, result, 'utf8');
// eslint-disable-next-line no-console
console.log(`===========================`);