-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (46 loc) · 1.42 KB
/
index.ts
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
import fs from 'fs';
import readline from 'readline';
function getInputFile() {
const args = process.argv;
let inputFile = args[2];
if (!inputFile) {
throw new Error('Input html file required as argument');
}
if (inputFile.split('.').pop() !== 'html') {
throw new Error('Input file must be an html file');
}
return inputFile || '';
}
async function processHTML(inputFile: string) {
const sectionRegex =
/Section (\d{0,4}\.{0,1}\d{0,1}\.{0,1}\d{0,1}\.{0,1}\d{0,1})/;
const titleRegex = /<span class="section_title">/;
const fileStream = fs.createReadStream(inputFile);
const readLines = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (let line of readLines) {
// Dont want to add links to the title
if (!titleRegex.test(line)) {
const sectionMatch = line.match(sectionRegex);
if (sectionMatch) {
// remove any trailing spaces from the section number for the href link
const formattedSection = sectionMatch[1].replace(/\.$/, '');
line = line.replace(
sectionRegex,
`<a href="#${formattedSection}">Section ${sectionMatch[1]}</a>`
);
}
}
// console.log prints to stdout with a new line at the end
// could also use process.stdout.write(line + '\n');
console.log(line);
}
}
try {
const inputFile = getInputFile();
processHTML(inputFile);
} catch (e) {
console.error(e);
}