-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
32 lines (28 loc) · 1.18 KB
/
build.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
import fs from 'fs';
import { renderMDPage, renderMDXPage } from './build-react.jsx'
import { getAllFiles } from './utils/index.js';
import * as path from 'path'
(async function () {
if (fs.existsSync('./public')) fs.rmSync('./public', { recursive: true });
fs.mkdirSync('./public', { recursive: true });
const filesArr = getAllFiles('./content', []);
for (const file of filesArr) {
const destinationPath = path.join('public', path.relative('content', file));
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
if (/\.mdx$/.test(file)) {
renderFile(file, destinationPath.replace('.mdx', '.html'), renderMDXPage)
}
else if (/\.md$/.test(file)) {
renderFile(file, destinationPath.replace('.md', '.html'), renderMDPage)
}
else {
fs.cpSync(file, destinationPath);
}
}
})().catch(console.error)
async function renderFile(sourcePath: string, destinationPath: string, renderer: (contents: string) => Promise<string>) {
const htmlDestinationPath = destinationPath.replace('.md', '.html');
const contents = fs.readFileSync(sourcePath, "utf-8")
const html = await renderer(contents)
fs.writeFileSync(htmlDestinationPath, html)
}