-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmlTocH2H3.js
54 lines (43 loc) · 1.35 KB
/
htmlTocH2H3.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
// npm install jsdom
const fs = require('fs');
const { JSDOM } = require('jsdom');
fs.readFile('index.html', 'utf8', (err, data) => {
if (err) {
console.error('Error reading index.html:', err);
return;
}
const dom = new JSDOM(data);
const document = dom.window.document;
const headers = document.querySelectorAll('h2, h3');
let tocListHTML = '<ul id="toc-list">\n';
let currentH2 = null;
headers.forEach((header) => {
if (!header.id) {
header.id = header.textContent
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/^-+|-+$/g, '');
}
if (header.tagName.toLowerCase() === 'h2') {
if (currentH2) {
tocListHTML += '</ul>\n</li>\n';
}
tocListHTML += `<li class="h2"><a href="#${header.id}">${header.textContent}</a>\n<ul>\n`;
currentH2 = header;
} else if (header.tagName.toLowerCase() === 'h3' && currentH2) {
tocListHTML += `<li class="h3"><a href="#${header.id}">${header.textContent}</a></li>\n`;
}
});
if (currentH2) {
tocListHTML += '</ul>\n</li>\n';
}
tocListHTML += '</ul>';
fs.writeFile('TOC.html', tocListHTML, 'utf8', (writeErr) => {
if (writeErr) {
console.error('Error writing toc.html:', writeErr);
} else {
console.log('TOC generated successfully in TOC.html');
}
});
});