-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc.js
193 lines (173 loc) · 5.79 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const {
copyFileSync,
rmSync,
readdirSync,
lstatSync,
readFileSync,
writeFileSync,
mkdirSync,
appendFileSync,
} = require('fs');
const path = require('path');
const ignore = [
'./README.md',
'NOTE.md',
'node_modules',
'waveshare/',
'hardware/ZicPad/neotrillis/.pio',
'hardware/encoderModule/.pio',
'.git',
'dustscript',
'wiki',
];
const extensions = ['.c', '.h', '.cpp'];
const docsFolder = 'wiki/docs';
const rootFolder = '.';
function folderExists(folder) {
try {
if (lstatSync(folder).isDirectory()) {
return true;
}
} catch {}
return false;
}
if (folderExists(docsFolder)) {
rmSync(docsFolder, { recursive: true, force: true });
} else {
console.error(`${docsFolder} not found. Are you sure you loaded wiki submodule?`);
if (!process.argv.includes('--force')) {
process.exit(1);
}
}
function isIgnored(file) {
file = file.toLowerCase();
for (const i of ignore) {
if (file.includes(i.toLowerCase())) {
return true;
}
}
return false;
}
function isAllowedExtension(file) {
file = file.toLowerCase();
for (const e of extensions) {
if (file.endsWith(e)) {
return true;
}
}
return false;
}
// Should extract content from those comment format:
/*md content */
/*md
content
*/
// use https://regexr.com/ to test
const reg = /\/\*md\s*((.*?[^\*\/]\n)+|(.*?))\s*\*\//g;
function extractMdComment(content) {
const result = [];
let match;
while ((match = reg.exec(content)) !== null) {
result.push(match[1]);
}
return result.join('\n\n');
}
function ensureDir(file) {
try {
if (lstatSync(folder).isDirectory()) {
return;
}
} catch {}
const folder = path.dirname(file);
mkdirSync(folder, { recursive: true });
}
function extractTitle2(lines) {
return lines
.filter((line) => line.trim().startsWith('## '))
.map((line) => line.replace('## ', ''));
}
const fileList = [];
function docs(folder) {
const contents = [];
const files = readdirSync(folder);
for (const file of files) {
const filepath = path.join(folder, file);
if (isIgnored(filepath)) {
continue;
}
if (lstatSync(filepath).isDirectory()) {
docs(filepath);
} else if (filepath.toLowerCase().endsWith('readme.md')) {
// Ignore root readme
if (filepath.toLowerCase() === 'readme.md') {
continue;
}
const content = readFileSync(filepath, 'utf8');
contents.unshift(content);
} else if (filepath.endsWith('.md')) {
const content = readFileSync(filepath, 'utf8');
contents.push(content);
} else if (isAllowedExtension(filepath)) {
const content = readFileSync(filepath, 'utf8');
const md = extractMdComment(content);
if (md) {
contents.push(md);
}
}
}
if (contents.length) {
const lines = contents.join('\n\n').split('\n');
const header = lines.shift();
const content = lines.join('\n');
const title2 = extractTitle2(lines);
const filename = header.replace(/^#+\s+/, '').replaceAll(' ', '-');
const wikiFile = path.join(docsFolder, filename, `${filename}.md`);
// if filename in fileList, prepend data to file because parent folder will always come last
const fileListItem = fileList.find((f) => f.filename === filename);
if (fileListItem) {
fileListItem.title2 = [...title2, ...fileListItem.title2];
const appendContent = readFileSync(wikiFile, 'utf8');
writeFileSync(wikiFile, content + '\n\n' + appendContent);
} else {
fileList.push({ filename, title2 });
ensureDir(wikiFile);
writeFileSync(wikiFile, content);
}
}
}
docs(rootFolder);
fileList.sort((a, b) => a.filename.localeCompare(b.filename));
for (const [index, fileActive] of fileList.entries()) {
const sidebar = fileList
.map((file) => {
if (file.filename === fileActive.filename) {
const title2 = file.title2
.map(
(title) =>
` - [${title}](https://github.com/apiel/zicBox/wiki/${
file.filename
}#${title.toLowerCase().replaceAll(' ', '-')})`
)
.join('\n');
return `**${file.filename}**\n${title2}`;
}
return `[${file.filename}](https://github.com/apiel/zicBox/wiki/${file.filename})`;
})
.join('\n\n');
writeFileSync(path.join(docsFolder, fileActive.filename, '_Sidebar.md'), sidebar);
const previous = index > 0 ? `<b>Previous</b>: <a href="https://github.com/apiel/zicBox/wiki/${fileList[index - 1].filename}">${fileList[index - 1].filename}</a>` : `<b>Previous</b>: <a href="https://github.com/apiel/zicBox/wiki">Home</a>`;
const next = index < fileList.length - 1 ? '<b>Next</b>: <a href="https://github.com/apiel/zicBox/wiki/' + fileList[index + 1].filename + '">' + fileList[index + 1].filename + '</a>' : '';
const footer = `
<p align="center">
${previous} ${previous && next ? ' | ' : ''} ${next}
</p>
`;
writeFileSync(path.join(docsFolder, fileActive.filename, '_Footer.md'), footer);
}
const sidebar = fileList
.map(({ filename }) => `[${filename}](https://github.com/apiel/zicBox/wiki/${filename})`)
.join('\n\n');
writeFileSync(path.join(docsFolder, '..', '_Sidebar.md'), sidebar);
const root = readFileSync('README.md', 'utf8');
const home = root.split('\n').slice(1).join('\n');
writeFileSync(path.join('wiki', 'Home.md'), home);