-
Notifications
You must be signed in to change notification settings - Fork 5
/
render-docs.ts
65 lines (60 loc) · 2.39 KB
/
render-docs.ts
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
/*
Author: Alexey Usov ([email protected], https://github.com/doubleaxe)
Please don't remove this comment if you use unmodified file
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import {marked} from 'marked';
//npx ts-node render-docs.ts
//node --loader ts-node/esm --inspect-brk render-docs.ts
const _dirname = __dirname;
const _docs = path.join(_dirname, 'docs');
const _target = path.join(_dirname, 'site', 'public', 'docs');
const _css = path.join(_dirname, 'node_modules', 'github-markdown-css');
/*
TODO:
use github api to convert markdown to html (will require online connection, will work in action?)
see https://docs.github.com/en/rest/repos/contents
curl -L -H "Accept: application/vnd.github.html" https://api.github.com/repos/doubleaxe/daxfb-calculator/contents/docs/README.md
or see https://docs.github.com/en/rest/markdown
also https://github.com/octokit/octokit.js
*/
(async function() {
console.log('building docs...');
fs.rmSync(_target, {recursive: true, force: true});
fs.mkdirSync(_target, {recursive: true});
const ignoreFiles: Record<string, boolean> = {
'_config.yml': true,
'template.html': true,
};
copyFolderRecursiveSync(_docs, _target, ignoreFiles);
const markdown = fs.readFileSync(path.join(_docs, 'README.md'), 'utf8');
const template = fs.readFileSync(path.join(_docs, 'template.html'), 'utf8');
const html = await marked(markdown, {
gfm: true,
async: true,
});
fs.writeFileSync(path.join(_target, 'index.html'), template.replace('__MARKDOWN_CONTENT__', html), 'utf8');
for(const css of ['github-markdown.css', 'github-markdown-light.css', 'github-markdown-dark.css']) {
fs.copyFileSync(path.join(_css, css), path.join(_target, 'assets', css));
}
})()
.catch((err) => {
console.error(err.stack);
process.exit(1);
});
function copyFolderRecursiveSync(source: string, target: string, ignoreFiles?: Record<string, boolean>) {
const files = fs.readdirSync(source);
for(const file of files) {
if(ignoreFiles?.[file])
continue;
const sourceFile = path.join(source, file);
const targetFile = path.join(target, file);
if(fs.lstatSync(sourceFile).isDirectory()) {
fs.mkdirSync(targetFile);
copyFolderRecursiveSync(sourceFile, targetFile);
} else {
fs.copyFileSync(sourceFile, targetFile);
}
}
}