-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.js
101 lines (79 loc) · 2.42 KB
/
doc.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
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
91
92
93
94
95
96
97
98
99
100
101
const fs = require('fs');
const path = require('path');
function readMarkdownFile(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
function extractModuleInfo(content) {
const lines = content.split('\n');
let moduleName = lines[0].replace('# ', '').replace(' Module', '');
let moduleDescription = '';
let moduleExample = '';
let inExample = false;
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim().startsWith('```') && !inExample) {
inExample = true;
moduleExample = lines[i];
} else if (lines[i].trim().startsWith('```') && inExample) {
inExample = false;
moduleExample += '\n' + lines[i];
break;
} else if (inExample) {
moduleExample += '\n' + lines[i];
} else if (lines[i].trim() !== '' && !moduleDescription) {
moduleDescription = lines[i];
}
}
return { moduleName, moduleDescription, moduleExample };
}
function extractMethods(content) {
const methodRegex = /^###\s(.+)/gm;
const methods = [];
let match;
while ((match = methodRegex.exec(content)) !== null) {
methods.push(match[1]);
}
return methods;
}
function generateMethodLinks(methods, fileName) {
return methods.map(method => {
const methodId = method.toLowerCase().replace(/[^\w]+/g, '-');
return `* [${method}](./docs/${fileName}#${methodId})`;
}).join('\n');
}
function generateModuleSection(filePath) {
const fileName = path.basename(filePath);
const content = readMarkdownFile(filePath);
const { moduleName, moduleDescription, moduleExample } = extractModuleInfo(content);
const methods = extractMethods(content);
const methodLinks = generateMethodLinks(methods, fileName);
let section = `## [${moduleName}](./docs/${fileName})
${moduleDescription}
`;
if (moduleExample) {
section += `${moduleExample}
`;
}
section += `Methods:
${methodLinks}
`;
return section;
}
function generateFullDocumentation(docsDir) {
const files = fs.readdirSync(docsDir).filter(file => file.endsWith('.md'));
let fullDoc = '# Tile Documentation\n\n';
files.forEach(file => {
const filePath = path.join(docsDir, file);
fullDoc += generateModuleSection(filePath);
});
return fullDoc;
}
function main() {
const docsDir = process.argv[2];
if (!docsDir) {
console.error('Please provide the docs directory path.');
process.exit(1);
}
const fullDocumentation = generateFullDocumentation(docsDir);
console.log(fullDocumentation);
}
main();