@knennigtri/merge-markdown
Version:
Prepare input files to merge together and optionally output html/pdf/word presentation
157 lines (153 loc) • 10.8 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var packageInfo = require("../package.json");
var npmModuleName = packageInfo.name.replace(/@[^/]+\//, "");
var manifestUtil = require("./manifest.js");
var merge = require("./merge.js");
var presentation = require("./presentation.js");
var dockerMerger = require("./docker.js");
var path = require("path");
var fs = require("fs");
var minimist = require("minimist");
var args = minimist(process.argv.slice(2));
//https://www.npmjs.com/package/debug
//Mac: DEBUG=* merge-markdown....
//WIN: set DEBUG=* & merge-markdown....
var debug = require("debug");
var debugArgs = debug("args");
var debugCLI = debug("cli");
var debbugOptions = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, {
"*": "Output all debugging messages",
"args": "See CLI argument messages",
"cli": "Validate CLI logic"
}), manifestUtil.debbugOptions), merge.debbugOptions), presentation.debbugOptions), dockerMerger.debbugOptions);
function run() {
var argsHelp = args.h || args.help;
var argsVersion = args.v || args.version;
var argsDebug = args.d || args.debug;
var argsManifest = args.m || args.manifest;
var argsQA = args.qa;
var argsSkipLinkcheck = args.skipLinkcheck;
var argsCreate = args.c || args.create;
var argsMaintainAssetPaths = args.maintainAssetPaths;
var argsGetDockerFiles = args.getDockerFiles;
var argsUseDocker = args.docker || args.Docker;
var argsFullProject = args.fullProject || args.fullproject || args.FullProject;
var argsPDF = args.pdf || args.PDF;
var argsHTML = args.html || args.HTML;
var argsWORD = args.word || args.WORD;
debugArgs(JSON.stringify(args, null, 2));
// Show CLI help
if (argsHelp) {
var helpType = argsHelp === true ? "default" : argsHelp.toLowerCase();
if (HELP[helpType]) console.log(HELP[helpType]);else console.log(HELP["default"]);
return;
}
if (argsVersion) {
console.log(packageInfo.version);
return;
}
if (argsDebug) {
console.log("[Mac] $ DEBUG=<option> ".concat(npmModuleName, " -m <file>"));
console.log("[Win] $ set DEBUG=<option> & ".concat(npmModuleName, " -m <file>"));
console.log("Options: ".concat(JSON.stringify(debbugOptions, null, 2)));
return;
}
if (argsQA) console.log("QA mode");
if (argsSkipLinkcheck) console.log("noLinkcheck mode");
if (argsMaintainAssetPaths) console.log("maintainAssetPaths mode");
if (argsCreate) {
if (argsUseDocker) console.log("Docker cannot be used with --Create mode");
var inputFilesPath = ".";
if (typeof argsCreate === "string") {
inputFilesPath = argsCreate;
}
manifestUtil.createManifestFile(inputFilesPath, argsFullProject);
if (argsFullProject) {
var themeSourcePath = path.join(__dirname, "theme");
debugCLI("Theme folder source: " + themeSourcePath);
var themeDestPath = path.join(process.cwd(), "theme");
fs.cpSync(themeSourcePath, themeDestPath, {
recursive: true
});
debugCLI("Theme folder copied to: " + themeDestPath);
var quickstartPath = path.join(__dirname, "quickstart.md");
fs.cpSync(quickstartPath, path.join(process.cwd(), "quickstart.md"));
debugCLI("Quickstart file copied to: " + path.join(process.cwd(), "quickstart.md"));
}
return;
}
if (argsGetDockerFiles) {
downloadDockerFiles();
return;
}
//Require -m
//If file, expect a manifest file, otherwise look for default file in given directory
var manifestFilePath;
try {
if (argsManifest && typeof argsManifest === "string") {
manifestFilePath = manifestUtil.exists(argsManifest);
} else {
//if there is no -m check for a default manifest file
manifestFilePath = manifestUtil.exists("./");
}
} catch (err) {
console.error(err);
console.log(HELP);
}
if (!manifestFilePath) {
console.log("No manifest found. Consider auto-creating with -c or specify a manifest with -m \n");
console.log(HELP["default"]);
return;
}
if (argsUseDocker) {
console.log("[Docker Mode] Building ".concat(npmModuleName, " in a container."));
dockerMerger.runMergeMarkdownInDocker(manifestFilePath, process.argv.slice(2).join(" "));
return;
}
debugCLI("manifest found at: " + manifestFilePath);
console.log("Using: " + manifestFilePath);
try {
merge.start(manifestFilePath, argsQA, argsSkipLinkcheck, argsMaintainAssetPaths).then(function (resultMarkdownFile) {
//Add presentation
var outputFormat = "";
if (argsWORD) outputFormat = presentation.EXTS.docx;
if (argsHTML) outputFormat = presentation.EXTS.html;
if (argsPDF) outputFormat = presentation.EXTS.pdf;
return presentation.build(resultMarkdownFile, outputFormat, manifestFilePath);
}).then(function (resultFile) {
console.log(resultFile + " created.");
})["catch"](function (error) {
console.error("Error creating file: ".concat(error));
});
} catch (err) {
console.error(err);
}
}
function downloadDockerFiles(manifestPath) {
var downloadPath = manifestPath || "./";
console.log("[Docker Mode] Downloading docker files...");
var dockerFileNames = ["docker-compose.yml", "Dockerfile"];
dockerFileNames.forEach(function (fileName) {
var sourcePath = path.join(__dirname, "../docker/", fileName);
var destinationPath = path.join(downloadPath, fileName);
// Copy the Docker file
fs.copyFileSync(sourcePath, destinationPath);
console.log(" Copied " + fileName + " to " + path.resolve(destinationPath));
});
}
var HELP = {
"default": "Usage: ".concat(npmModuleName, " [ARGS]\nArguments:\n -m, --manifest <manifestFile> Path to input folder, yaml, or json manifest\n -v, --version Displays version of this package\n -c, --create <path> auto-creates ./manifest.yml with input files from <path>\n --fullProject Add to -c to include a basic theme, frontmatter, and npm scripts\n --docker Run ").concat(npmModuleName, " commands in docker\n --getDockerFiles Downloads the Docker files to your local project\n --qa QA mode.\n --skipLinkcheck Skips linkchecking\n --maintainAssetPaths Retains original asset paths\n --pdf Output to PDF. Must have Pandoc and wkhtmltopdf installed!\n --html --word Output to HTML or Word. Must have Pandoc installed!\n -h, --help Displays this screen\n -h manifest | options |\n outputOptions | qa | docker See examples\n -d, --debug See debug Options\nDefault is ").concat(manifestUtil.DEFAULT_MANIFEST.FILE_TYPES, " unless specified in -m.\n\nDownload Docker: https://docs.docker.com/get-docker/\n"),
manifest: "Example yaml in a manifest file:\n input:\n global-frontmatter.md: {noYAML: false, doctoc: false}\n module1Folder/file1.md: {fileOptions}\n module2Folder/file2.md: {replace: {key:value}}\n noYAML: true\n doctoc: true\n output: \n name: merged/myOutput.md\n {outputOptions}\n qa: {exclude: regex}\n {projectOptions}\n\nAlso, consider auto creating a manifest for your project:\n\n$ ".concat(npmModuleName, " --create /path/to/project --fullProject\n"),
options: "fileOptions are applied first and then projectOptions second.\nSupported key/value pairs for {options}:\n noYAML: true|false Optionlly removes YAML. Default=false\n doctoc: true|false|\"TOC title\" doctoc arguments. See https://www.npmjs.com/package/doctoc\n option: <value>\n replace: Searches for key and replaces with value\n key: value\n <!--{key}-->: value Example key for a useful identifier\n *: \"stringVal\" Regular expressions are allowed\n",
outputOptions: // TODO: Easier help options for understanding PDF, HTML, Word branding settings
"Supported key/value pairs for {outputOptions}:\n doctoc: true|false|\"TOC title\" doctoc arguments. See https://www.npmjs.com/package/doctoc\n option: <value>\n pandoc: pandoc arguments added to <value>. See https://pandoc.org/MANUAL.html#options\n key1: \"-c mystyle.css\"\n key2: \"--template mytemplate.html\"\n key3: \"--reference-doc path/to/reference.docx\"\n wkhtmltopdf: wkhtmltopdf options. See https://www.npmjs.com/package/wkhtmltopdf#options\n pageSize: Letter\n footerLine: true\n",
qa: "QA mode can optionally exclude files from the output.\nExample: exclude all filenames with \"frontmatter\" by default\n qa: {exclude: \"(frontmatter|preamble)\"}\n ",
docker: "REQUIRED: Download Docker: https://docs.docker.com/get-docker/\n\n 1. Start Docker\n 2. Run:\n ".concat(npmModuleName, " -m manifest.yml --docker\n\n Alternatively you can download the docker files directly:\n ").concat(npmModuleName, " --getDockerFiles\n ")
};
exports.run = run;