mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
- Replace live-server with existing Browsersync dependency as web server - Remove duplicate `index.html` file - Add `build:html` script to generate `/docs/preview.html`
43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
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 srcFile = 'index.html';
|
|
const srcPath = path.resolve(__dirname, '..', 'docs');
|
|
const srcHTML = fs.readFileSync(path.resolve(srcPath, srcFile), 'utf8');
|
|
const outFile = 'preview.html';
|
|
const outPath = path.resolve(__dirname, '..', 'docs');
|
|
const outHTML = srcHTML
|
|
// Append comment
|
|
.replace(/(<!DOCTYPE html>)/, `${comment}\n$1`)
|
|
// Modify title
|
|
.replace(/(<\/title>)/, ' (Preview)$1')
|
|
// Replace CDN URLs with local paths
|
|
.replace(/\/\/cdn.jsdelivr.net\/npm\/docsify@4\//g, '/');
|
|
const formattedHTML = prettier.format(outHTML, {
|
|
...prettierConfig,
|
|
filepath: outFile,
|
|
});
|
|
|
|
console.log(`\nBuilding ${outFile} in ${outPath}`);
|
|
|
|
fs.writeFileSync(path.resolve(outPath, outFile), formattedHTML);
|
|
}
|
|
|
|
generatePreview();
|