-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixExampleComments.js
82 lines (71 loc) · 2.13 KB
/
fixExampleComments.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
const fs = require('fs');
const readline = require('readline');
const glob = require('glob');
const { statSync } = require('fs');
async function processLineByLine(file) {
const fileStream = fs.createReadStream(file);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
const data = [];
let insideExampleBlock = false;
const codeExampleFencing = '* ```';
for await (let line of rl) {
if (insideExampleBlock) {
const openingTagOrEndComment = /^(\s*)\*( @|\/)/;
const match = line.match(openingTagOrEndComment);
if (match) {
const whitespace = match[1];
// console.log(whitespace + codeExampleFencing + '###');
// console.log(line);
data.push(whitespace + codeExampleFencing);
insideExampleBlock = false;
}
}
const includeTag = /^(\s*)\* @example <caption>include:/;
let match = line.match(includeTag);
if (match) {
data.push(line); // skip include tags for now
continue;
}
const exampleTag = /^(\s*)\* @example/;
match = line.match(exampleTag);
if (match) {
insideExampleBlock = true;
const whitespace = match[1];
const captionTag = line.match(/<caption>/);
if (captionTag) {
const closingCaptionTag = line.match(/<\/caption>/);
if (!closingCaptionTag) {
throw new Error(`closing caption missing: ${line}`);
}
line = line.replace(/<\/?caption>/g, '');
}
data.push(line);
data.push(whitespace + codeExampleFencing);
} else {
data.push(line);
}
}
fs.writeFileSync(file, data.join('\n'), 'utf-8');
}
const main = async () => {
const path = 'src/';
const { readdir } = fs.promises;
try {
const files = glob.sync(`${path}/**/*.ts`, {
ignore: ['node_modules'],
});
for (const file of files) {
const stat = statSync(file);
if (!stat.isFile()) continue;
processLineByLine(file);
}
} catch (err) {
console.error(err);
}
};
main();