-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathgene.js
43 lines (33 loc) · 1.12 KB
/
gene.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
const fs = require('fs');
const path = require('path');
const docsDir = './docs';
function extractTitle(content) {
const match = content.match(/^---[\s\S]*?title:\s*(.+)[\s\S]*?---/m);
return match ? match[1].trim() : null;
}
function slugify(title) {
return title.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
function generateTOC() {
let tocEntries = [];
fs.readdirSync(docsDir).forEach(file => {
if (path.extname(file) === '.md') {
const filePath = path.join(docsDir, file);
const content = fs.readFileSync(filePath, 'utf8');
const title = extractTitle(content);
if (title) {
tocEntries.push(`* [${title}](/${file})`);
}
}
});
// Sort entries in reverse order
tocEntries.sort((a, b) => b.localeCompare(a));
return tocEntries.join('\n');
}
const tableOfContents = generateTOC();
console.log(tableOfContents);
// Optionally, write the TOC to a file
// fs.writeFileSync('TABLE_OF_CONTENTS.md', tableOfContents);