-
Notifications
You must be signed in to change notification settings - Fork 27
/
remove-notes.mjs
58 lines (48 loc) · 1.88 KB
/
remove-notes.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import fs from 'fs';
import path from 'path';
async function removeMultilineSequences(directoryPath) {
try {
const files = await fs.promises.readdir(directoryPath);
for (const file of files) {
if ((await fs.promises.lstat(path.join(directoryPath, "/", file))).isDirectory()) {
removeMultilineSequences(path.join(directoryPath, "/", file))
} else {
if (file.split(/[.]+/).pop() === "md") {
const filePath = path.join(directoryPath, file);
const fileContent = await fs.promises.readFile(filePath, 'utf-8');
const modifiedContent = removeSequences(fileContent);
if (modifiedContent !== fileContent) {
await fs.promises.writeFile(filePath, modifiedContent);
console.log(`Modified: ${filePath}`);
} else {
console.log(`No modifications: ${filePath}`);
}
}
}
}
console.log('Processing complete.');
} catch (error) {
console.error('Error occurred:', error);
}
}
function removeSequences(content) {
const lines = content.split('\n');
let modifiedContent = '';
let isInsideSequence = false;
for (const line of lines) {
if (!isInsideSequence && line.trim().startsWith('Notes:')) {
isInsideSequence = true;
}
if (!isInsideSequence) {
modifiedContent += line + '\n';
}
if (isInsideSequence && (line.trim() === '---' || line.trim() === '---v')) {
isInsideSequence = false;
modifiedContent += line
}
}
return modifiedContent.trim();
}
// Example usage
const directoryPath = `syllabus/${process.argv[3]}`; // Replace this with your directory path
removeMultilineSequences(directoryPath);