UNPKG

md-html-to-pdf

Version:

CLI tool for converting Markdown files or Html files to PDF.

81 lines (80 loc) 2.89 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mdsToPdfs = exports.mdToPdf = void 0; const get_port_1 = __importDefault(require("get-port")); const config_1 = require("./lib/config"); const helpers_1 = require("./lib/helpers"); const md_to_pdf_1 = require("./lib/md-to-pdf"); const serve_dir_1 = require("./lib/serve-dir"); const hasContent = (input) => "content" in input; const hasPath = (input) => "path" in input; const hasHtml = (input) => "html" in input; async function mdToPdf(input, config = {}) { if (!hasContent(input) && !hasPath(input) && !hasHtml(input)) { throw new Error('The input is missing one of the properties "content" or "path" or "html".'); } if (!config.port) { config.port = await (0, get_port_1.default)(); } if (!config.basedir) { config.basedir = "path" in input ? (0, helpers_1.getDir)(input.path) : process.cwd(); } if (!config.dest) { config.dest = ""; } const mergedConfig = { ...config_1.defaultConfig, ...config, pdf_options: { ...config_1.defaultConfig.pdf_options, ...config.pdf_options }, }; const server = await (0, serve_dir_1.serveDirectory)(config.basedir, config.port); const pdf = await (0, md_to_pdf_1.convertMdToPdf)(input, mergedConfig); server.close(); return pdf; } exports.mdToPdf = mdToPdf; async function mdsToPdfs(params, options) { params.forEach(param => { const input = param.input; if (!hasContent(input) && !hasPath(input) && !hasHtml(input)) { throw new Error('The input is missing one of the properties "content" or "path" or "html".'); } }); let port = options.port; if (!port) { port = await (0, get_port_1.default)(); } let basedir = options.basedir; const data = params.map(param => { const { input, config = {} } = param; if (!basedir) { basedir = config.basedir; if (!basedir) { basedir = "path" in input ? (0, helpers_1.getDir)(input.path) : process.cwd(); } } if (!config.dest) { config.dest = ""; } const mergedConfig = { ...config_1.defaultConfig, ...config, port, pdf_options: { ...config_1.defaultConfig.pdf_options, ...config.pdf_options }, }; return { input, config: mergedConfig }; }); const server = await (0, serve_dir_1.serveDirectory)(basedir, port); const result = await (0, md_to_pdf_1.convertMdsToPdfs)(data); server.close(); return result; } exports.mdsToPdfs = mdsToPdfs; exports.default = mdToPdf;