markugen
Version:
Markdown to HTML/PDF static site generation tool
239 lines (238 loc) • 9.13 kB
JavaScript
"use strict";
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MdToHtml = void 0;
const package_json_1 = require("../../package.json");
const markugen_1 = __importStar(require("../markugen"));
const markugenargs_1 = require("./markugenargs");
const fs_extra_1 = __importDefault(require("fs-extra"));
const node_path_1 = __importDefault(require("node:path"));
class MdToHtml {
command = ['$0', 'html', 'mdtohtml'];
describe = 'Markdown to HTML and/or PDF file generation';
builder(args) {
args.options({
format: {
alias: ['input-format', 'if'],
describe: 'format of the input, string of markdown or path to file/directory',
choices: ['file', 'string'],
default: 'file',
},
'output-format': {
alias: ['of'],
describe: 'format of the output, html files or string of html ' +
'(string is only valid if format is also string or input is a file)',
choices: ['file', 'string'],
default: 'file',
},
input: {
alias: ['i'],
describe: 'the directory to locate the markdown files, a single file, ' +
'or a string of markdown',
type: 'string',
default: '.',
},
extensions: {
alias: ['exts'],
describe: 'list of file extensions to search the input directory for',
type: 'array',
default: ['md'],
},
output: {
alias: ['o'],
describe: 'directory to write the output',
default: './output',
},
outputName: {
alias: ['n', 'on'],
describe: 'base name of the file to output, only used when --input is ' +
'a file or string, defaults to index for strings and the file name for files',
},
pdf: {
alias: ['p'],
describe: 'generates PDF files instead of html files',
type: 'boolean',
default: false,
},
'pdf-only': {
alias: ['po'],
describe: 'implies --pdf and removes all html generated files, leaving only the PDFs',
type: 'boolean',
default: false,
},
exclude: {
alias: ['x'],
describe: 'list of files or folders to exclude from generation, paths ' +
'should be relative to the input directory',
type: 'array',
},
title: {
alias: ['t'],
describe: 'the title to use for the site',
default: 'Markugen v' + package_json_1.version,
type: 'string',
},
'inherit-title': {
alias: ['it'],
describe: 'if true, all pages not custom configured will inherit the site title',
type: 'boolean',
},
footer: {
alias: ['f'],
describe: 'overrides the default Markugen footer',
type: 'string',
},
timestamp: {
alias: ['ts'],
describe: 'if true, a timestamp will be embedded in the js output',
type: 'boolean',
default: true
},
home: {
alias: ['index'],
describe: 'sets the home page for the site, default uses the first root page',
type: 'string',
},
toc: {
describe: 'maximum header depth to output in the Table of Contents, values less than ' +
'or equal to zero will hide the Table of Contents.',
type: 'number',
default: 3,
},
embed: {
alias: ['e'],
describe: 'if true, css and javascript will be embedded in each page',
type: 'boolean',
},
favicon: {
describe: 'relative path to an icon to use as the favicon, must be ' +
'relative to the input directory',
type: 'string',
},
assets: {
alias: ['a'],
describe: 'list of files or folders to copy to the output',
type: 'array',
},
'keep-assets': {
alias: ['k', 'ka'],
describe: 'if true and --pdf, will keep the assets after generation',
type: 'boolean',
default: false,
},
script: {
describe: 'additional JavaScript to embed in the script tag at the end of the body',
type: 'string',
},
js: {
describe: 'additional JavaScript files to include on each page',
type: 'array',
},
style: {
describe: 'additional CSS to embed in the style tag',
type: 'string',
},
css: {
describe: 'additional CSS files to include on each page',
type: 'array',
},
'include-hidden': {
alias: ['ih'],
describe: 'include folders and files that begin with a dot (.)',
type: 'boolean',
default: false,
},
'clear-output': {
alias: ['co'],
describe: 'clears the output folder before building',
type: 'boolean',
default: false,
},
vars: {
alias: ['v'],
describe: 'path to a JSON file representing dynamic variables used in ' +
'template expansion',
type: 'string',
coerce: fs_extra_1.default.readJSONSync,
},
config: {
describe: 'path to a JSON file with an object containing CLI arguments',
type: 'string',
},
...markugenargs_1.MarkugenArgs,
});
return args;
}
async handler(args) {
// create the markugen instance
let mark = undefined;
try {
mark = new markugen_1.default({ cli: true });
await mark.generate(config(args));
}
catch (e) {
mark?.error(e);
process.exit(1);
}
}
}
exports.MdToHtml = MdToHtml;
function config(args) {
let options = { ...args, cli: true };
if (args.config) {
// check existence
if (!fs_extra_1.default.existsSync(args.config))
throw new Error(`No such file [${args.config}]`);
let json = {};
// attempt to parse json
try {
json = fs_extra_1.default.readJSONSync(args.config);
}
catch (e) {
throw new Error(`Invalid JSON, ${e.message}`);
}
// convert relative input path
if (json.input && !node_path_1.default.isAbsolute(json.input))
json.input = node_path_1.default.resolve(node_path_1.default.dirname(options.config), json.input);
// combine the arguments
options = { ...args, ...(0, markugen_1.keysToCamelCase)(json), cli: true };
// remove the config option
delete options.config;
}
return options;
}