docsify/build/html.js
John Hildenbiddle 1c5a7013f1
Chore: Clean up server implementation and update test docs (#2316)
- Replace live-server with existing Browsersync dependency as web server
- Remove duplicate `index.html` file
- Add `build:html` script to generate `/docs/preview.html`
2023-12-11 15:00:33 -06:00

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();