-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathhtml.js
47 lines (42 loc) · 1.58 KB
/
html.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
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
import prettier from 'prettier';
import stripIndent from 'common-tags/lib/stripIndent/index.js';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const prettierConfig = prettier.resolveConfig.sync(__dirname);
// Preview
// =============================================================================
function generatePreview() {
const comment = stripIndent`
<!--
This file is generated by the build/html.js script.
Do not edit this file directly.
-->
`;
const basePath = "basePath: '/docs/',";
const srcFile = 'index.html';
const srcPath = path.resolve(__dirname, '..', 'docs');
const srcHTML = fs.readFileSync(path.resolve(srcPath, srcFile), 'utf8');
const outFile = 'index.html';
const outPath = path.resolve(__dirname, '..');
const outHTML = srcHTML
// Append comment
.replace(/(<!DOCTYPE html>)/, `${comment}\n$1`)
// Modify title
.replace(/(<\/title>)/, ' (Preview)$1')
// Replace docsify.min.js to docsify.js
.replace(/docsify.min.js/, 'docsify.js')
// Replace CDN URLs with local paths
.replace(/\/\/cdn.jsdelivr.net\/npm\/docsify@4\//g, '/')
// Inject basePath, use nameLink as an "archor"
.replace(/(nameLink)/, `${basePath}\n$1`);
const formattedHTML = prettier.format(outHTML, {
...prettierConfig,
filepath: outFile,
});
console.log(`\nBuilding ${outFile} in ${outPath}`);
fs.writeFileSync(path.resolve(outPath, outFile), formattedHTML);
}
generatePreview();