forked from vlcn-io/js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-docs.js
More file actions
90 lines (83 loc) · 2.84 KB
/
build-docs.js
File metadata and controls
90 lines (83 loc) · 2.84 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const fs = require('fs');
const path = require('path');
const marked = require('marked');
const cheerio = require('cheerio');
// Function to recursively find all README.md files
function findReadmeFiles(dir) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(findReadmeFiles(file));
} else if (file.endsWith('README.md')) {
results.push(file);
}
});
return results;
}
// Find all README.md files
const readmeFiles = [
'CREATING_BROWSER_PERSISTENCE.md',
'README.md',
...findReadmeFiles('packages')
];
function generateTOC(content, fileInfo) {
const $ = cheerio.load(content);
let toc = `<h3>${fileInfo.title}</h3><ul>`;
$('h1, h2, h3, h4').each((i, elem) => {
const level = parseInt(elem.name[1]);
const text = $(elem).text();
const slug = `${fileInfo.slug}-${text.toLowerCase().replace(/[^\w]+/g, '-')}`;
$(elem).attr('id', slug);
toc += `<li class="toc-level-${level}"><a href="#${slug}">${text}</a></li>`;
});
toc += '</ul>';
return { toc, content: $.html() };
}
let fullContent = '';
let toc = '<h2>Table of Contents</h2>';
readmeFiles.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
const html = marked.parse(content);
const fileInfo = {
title: path.basename(path.dirname(file)),
slug: path.basename(path.dirname(file)).toLowerCase().replace(/[^\w]+/g, '-')
};
const { toc: sectionToc, content: processedHtml } = generateTOC(html, fileInfo);
toc += sectionToc;
fullContent += `<h2 id="${fileInfo.slug}">${fileInfo.title}</h2>${processedHtml}`;
});
const finalHtml = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CR-SQLite Documentation</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1000px; margin: 0 auto; padding: 20px; }
h1, h2 { border-bottom: 1px solid #ddd; }
.toc-level-2 { margin-left: 20px; }
.toc-level-3 { margin-left: 40px; }
.toc-level-4 { margin-left: 60px; }
pre { background-color: #f4f4f4; padding: 10px; border-radius: 5px; overflow-x: auto; }
code { background-color: #f4f4f4; padding: 2px 4px; border-radius: 3px; }
#toc { position: fixed; top: 0; left: 0; width: 300px; height: 100%; overflow-y: auto; padding: 20px; background-color: #f8f8f8; }
#content { margin-left: 320px; }
</style>
</head>
<body>
<div id="toc">
<h1>CR-SQLite Documentation</h1>
${toc}
</div>
<div id="content">
${fullContent}
</div>
</body>
</html>
`;
fs.writeFileSync('documentation.html', finalHtml);
console.log('Documentation generated: documentation.html');