-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathremark.js
More file actions
65 lines (58 loc) · 1.67 KB
/
remark.js
File metadata and controls
65 lines (58 loc) · 1.67 KB
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
const fs = require('fs');
const path = require('path');
const unified = require('unified');
const vfile = require('to-vfile');
const remarkParse = require('remark-parse');
const remarkBreaks = require('remark-breaks');
const remarkToc = require('remark-toc');
const remarkStringify = require('remark-stringify');
function getMarkdownFiles(dir) {
let results = [];
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat && stat.isDirectory()) {
results = results.concat(getMarkdownFiles(fullPath));
} else if (file.endsWith('.md')) {
results.push(fullPath);
}
});
return results;
}
const markdownFiles = getMarkdownFiles('./');
let pending = markdownFiles.length;
if (pending === 0) {
console.log('ℹ️ No Markdown files found.');
process.exit(0);
}
markdownFiles.forEach(input => {
unified()
.use(remarkParse)
.use(remarkBreaks)
.use(remarkToc, {
heading: 'TABLE OF CONTENTS',
maxDepth: 3
})
.use(remarkStringify)
.process(vfile.readSync(input), function (err, file) {
if (err) {
console.error(`❌ Error processing ${input}:`, err);
pending--;
if (pending === 0) process.exit(0);
return;
}
const newContent = String(file);
fs.writeFile(input, newContent, function (err) {
if (err) {
console.error(`❌ Error writing ${input}:`, err);
} else {
console.log(`✅ TOC updated: ${input}`);
}
pending--;
if (pending === 0) {
console.log('🏁 All Markdown files processed.');
process.exit(0);
}
});
});
});