markugen
Version:
Markdown to HTML/PDF static site generation tool
251 lines (250 loc) • 9.08 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const colors_1 = __importDefault(require("colors"));
const node_path_1 = __importDefault(require("node:path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const node_os_1 = __importDefault(require("node:os"));
const package_json_1 = require("../package.json");
const node_child_process_1 = require("node:child_process");
const pdfgenerator_1 = __importDefault(require("./pdfgenerator"));
const htmlgenerator_1 = __importDefault(require("./htmlgenerator"));
__exportStar(require("./markugendetails"), exports);
__exportStar(require("./htmlgenerator"), exports);
__exportStar(require("./pdfgenerator"), exports);
__exportStar(require("./markugenoptions"), exports);
__exportStar(require("./utils"), exports);
class Markugen {
/**
* The version of Markugen
*/
static version = package_json_1.version;
/**
* The name of Markugen
*/
static name = package_json_1.name;
/**
* The home page of Markugen
*/
static homepage = package_json_1.homepage;
/**
* Root path to the Markugen package
*/
root;
/**
* Set to true if this is being ran from the cli
*/
_options = {
browser: Markugen.findChrome() ?? '',
sandbox: true,
color: true,
quiet: false,
debug: false,
cli: false,
};
/**
* Constructs a new instance with the given {@link options}.
*/
constructor(options) {
this.root = node_path_1.default.dirname(__dirname);
if (options)
this.options = options;
}
/**
* @returns the current configuration options
*/
get options() { return this._options; }
/**
* Sets the configuration options
*/
set options(options) {
this._options.browser = options.browser ?? this._options.browser;
this._options.sandbox = options.sandbox ?? this._options.sandbox;
this._options.color = options.color ?? this._options.color;
this._options.quiet = options.quiet ?? this._options.quiet;
this._options.debug = options.debug ?? this._options.debug;
this._options.cli = options.cli ?? this._options.cli;
// enable/disable console colors
if (this.options.color)
colors_1.default.enable();
else
colors_1.default.disable();
}
/**
* This is a synchronous version of {@link generate}. Calling this version
* will ignore the {@link HtmlOptions.pdf} flag and only generate html output.
* See {@link generate} for more details.
* @returns the paths to all generated pages, the html if format === 'string', or
* undefined if an error occurred
*/
mdtohtml(options) {
return new htmlgenerator_1.default(this, options).generate();
}
/**
* Generates PDF documents for the given {@link PdfOptions options}.
* @param options the {@link PdfOptions} for generation
* @returns a list of files that were generated
*/
async htmltopdf(options) {
return await new pdfgenerator_1.default(this, options).generate();
}
/**
* Generates the HTML documentation and PDF files if {@link HtmlOptions.pdf}
* is true.
* @returns the paths to all generated pages, the html if format === 'string', or
* undefined if an error occurred
*/
async generate(options) {
const gen = new htmlgenerator_1.default(this, options);
const generated = gen.generate();
if (gen.options.pdf && generated) {
const result = await new pdfgenerator_1.default(this, {
input: generated,
remove: gen.options.pdfOnly,
}).generate();
if (gen.options.pdfOnly && !gen.options.keepAssets)
gen.clearAssets();
return result;
}
return generated;
}
/**
* Starts a console group
*/
group(...args) {
if (!this.options.quiet)
console.group(...args);
}
/**
* Ends a console group
*/
groupEnd() { if (!this.options.quiet)
console.groupEnd(); }
/**
* Use in place of console.log so the app can handle coloring
* and any cli options that were given
*/
log(label, ...args) {
const ol = typeof label === 'string' ? { label: label } : label;
if (!this.options.quiet && ol.ignoreQuiet !== true) {
const color = ol.color ? ol.color : colors_1.default.green;
if (ol.label)
console.log(color(ol.label), ...args);
else
console.log(...args);
}
}
/**
* Use in place of console.log so the app can handle coloring
* and any cli options that were given
*/
warning(...args) {
this.log({ label: 'Warning:', color: colors_1.default.yellow }, ...args);
}
/**
* Outputs the given error message
* @param e the error to log
*/
error(e) {
const msg = this.options.debug && e.stack ?
colors_1.default.red(e.stack) : `${colors_1.default.red('Error:')} ${e.message}`;
console.error(msg);
}
/**
* Escapes all markdown special characters in the given value and returns
* the new string.
*/
static escape(md) {
return md ? md.toString().replace(/([\\`*_{}[\]()#+\-.!:])/g, '\\$1') : '';
}
/**
* Returns an object representing the Markugen properties
*/
static toObject(date) {
return {
version: Markugen.version,
name: Markugen.name,
timestamp: date,
platform: process.platform === 'win32' ? 'windows' : 'linux',
homepage: Markugen.homepage,
};
}
/**
* Attempts to locate an executable for Google Chrome
* @returns the path to chrome if found, else returns undefined
*/
static findChrome() {
switch (process.platform) {
case 'win32': return Markugen.findChromeWindows();
case 'darwin': return Markugen.findChromeMac();
case 'linux': return Markugen.findChromeLinux();
}
return undefined;
}
/**
* Attempts to locate an executable for Google Chrome on Windows
* @returns the path to chrome if found, else returns undefined
*/
static findChromeWindows() {
const suffix = '\\Google\\Chrome\\Application\\chrome.exe';
const prefixes = [
process.env.LOCALAPPDATA,
process.env.PROGRAMFILES,
process.env['PROGRAMFILES(X86)']
];
for (const prefix of prefixes) {
if (prefix) {
const test = node_path_1.default.join(prefix, suffix);
if (fs_extra_1.default.existsSync(test))
return test;
}
}
return undefined;
}
/**
* Attempts to locate an executable for Google Chrome on Mac
* @returns the path to chrome if found, else returns undefined
*/
static findChromeMac() {
const toExec = '/Contents/MacOS/Google Chrome';
const regPath = '/Applications/Google Chrome.app' + toExec;
const altPath = node_path_1.default.join(node_os_1.default.homedir(), regPath);
const mdFindCmd = 'mdfind \'kMDItemDisplayName == "Google Chrome" && kMDItemKind == Application\'';
if (fs_extra_1.default.existsSync(regPath))
return regPath;
if (fs_extra_1.default.existsSync(altPath))
return altPath;
// try the md command last
const result = (0, node_child_process_1.spawnSync)(mdFindCmd);
if (result.status === 0 && result.stdout)
return result.stdout.toString().trim() + toExec;
return undefined;
}
/**
* Attempts to locate an executable for Google Chrome on Linux
* @returns the path to chrome if found, else returns undefined
*/
static findChromeLinux() {
const result = (0, node_child_process_1.spawnSync)('which google-chrome');
if (result.status === 0 && result.stdout)
return result.stdout.toString().trim();
return undefined;
}
}
exports.default = Markugen;