UNPKG

md-html-to-pdf

Version:

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

176 lines (175 loc) 6.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertMdsToPdfs = exports.convertMdToPdf = exports.convertMdToHtml = void 0; const fs_1 = require("fs"); const gray_matter_1 = __importDefault(require("gray-matter")); const path_1 = require("path"); const generate_output_1 = require("./generate-output"); const get_html_1 = require("./get-html"); const get_output_file_path_1 = require("./get-output-file-path"); const helpers_1 = require("./helpers"); const read_file_1 = require("./read-file"); /** * Convert html to pdf. */ const convertHtmlToPdf = async (html, input, config) => { const relativePath = "path" in input ? (0, path_1.resolve)(input.path).replace(config.basedir, "") : "/"; const output = await (0, generate_output_1.generateOutput)(html, relativePath, config); if (!output) { if (config.devtools) { throw new Error("No file is generated with --devtools."); } throw new Error(`Failed to create ${config.as_html ? "HTML" : "PDF"}.`); } if (output.filename) { if (output.filename === "stdout") { process.stdout.write(output.content); } else { await fs_1.promises.writeFile(output.filename, output.content); } } return output; }; /** * Convert htmls to pdf. */ const convertHtmlsToPdf = async (htmls, input, config) => { const relativePath = "path" in input ? (0, path_1.resolve)(input.path).replace(config.basedir, "") : "/"; const outputs = await (0, generate_output_1.generateOutputs)(htmls.map((param => { return { html: param.html, dest: param.dest, }; })), relativePath, config); if (!outputs) { if (config.devtools) { throw new Error("No file is generated with --devtools."); } throw new Error(`Failed to create ${config.as_html ? "HTML" : "PDF"}.`); } Promise.all(outputs.map(async (output) => { if (output.filename) { if (output.filename === "stdout") { process.stdout.write(output.content); } else { await fs_1.promises.writeFile(output.filename, output.content); } } })); return outputs; }; /** * Convert markdown to html. */ const convertMdToHtml = async (input, config, args = {}) => { let md = ""; let frontMatterConfig = {}; let isHtml = false; if ("html" in input) { isHtml = true; } else { const mdFileContent = "content" in input ? input.content : await (0, read_file_1.readFile)(input.path, args["--md-file-encoding"] ?? config.md_file_encoding); const { content, data } = (0, gray_matter_1.default)(mdFileContent); md = content; frontMatterConfig = data; } // merge front-matter config config = { ...config, ...frontMatterConfig, pdf_options: { ...config.pdf_options, ...(frontMatterConfig.pdf_options || {}), }, isHtml }; const { headerTemplate, footerTemplate, displayHeaderFooter } = config.pdf_options; if ((headerTemplate || footerTemplate) && displayHeaderFooter === undefined) { config.pdf_options.displayHeaderFooter = true; } const arrayOptions = ["body_class", "script", "stylesheet"]; // sanitize frontmatter array options for (const option of arrayOptions) { if (!Array.isArray(config[option])) { config[option] = [config[option]].filter(Boolean); } } const jsonArgs = new Set([ "--marked-options", "--pdf-options", "--launch-options", ]); // merge cli args into config for (const arg of Object.entries(args)) { const [argKey, argValue] = arg; const key = argKey.slice(2).replace(/-/g, "_"); config[key] = jsonArgs.has(argKey) ? JSON.parse(argValue) : argValue; } // sanitize the margin in pdf_options if (typeof config.pdf_options.margin === "string") { config.pdf_options.margin = (0, helpers_1.getMarginObject)(config.pdf_options.margin); } // set output destination if (config.dest === undefined) { config.dest = "path" in input ? (0, get_output_file_path_1.getOutputFilePath)(input.path, config.as_html ? "html" : "pdf") : "stdout"; } const highlightStylesheet = (0, path_1.resolve)((0, path_1.dirname)(require.resolve("highlight.js")), "..", "styles", `${config.highlight_style}.css`); config.stylesheet = [...new Set([...config.stylesheet, highlightStylesheet])]; let html; if ("html" in input) { html = input.html; } else { html = (0, get_html_1.getHtml)(md, config); } return { html, config, }; }; exports.convertMdToHtml = convertMdToHtml; /** * Convert markdown to pdf. */ const convertMdToPdf = async (input, config, args = {}) => { const { html, config: mergedConfig } = await (0, exports.convertMdToHtml)(input, config, args); return convertHtmlToPdf(html, input, mergedConfig); }; exports.convertMdToPdf = convertMdToPdf; /** * Convert markdowns to pdfs. */ const convertMdsToPdfs = async (params) => { if (params.length === 0) { return; } let newConfig; let input; const htmls = await Promise.all(params.map(async (param) => { const args = param.args || {}; const { html, config: mergedConfig } = await (0, exports.convertMdToHtml)(param.input, param.config, args); newConfig = mergedConfig; input = param.input; return { html, dest: param.config.dest, args }; })); return convertHtmlsToPdf(htmls, input, newConfig); }; exports.convertMdsToPdfs = convertMdsToPdfs;