md-html-to-pdf
Version:
CLI tool for converting Markdown files or Html files to PDF.
101 lines (100 loc) • 3.62 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateOutputs = exports.generateOutput = void 0;
const puppeteer_1 = __importDefault(require("puppeteer"));
const is_http_url_1 = require("./is-http-url");
async function generateOutput(html, relativePath, config) {
const browser = await puppeteer_1.default.launch({
devtools: config.devtools,
...config.launch_options,
});
const page = await browser.newPage();
const outputFileContent = await makeContent(page, html, relativePath, config);
await browser.close();
return config.devtools
? undefined
: { filename: config.dest, content: outputFileContent };
}
exports.generateOutput = generateOutput;
async function makeContent(page, html, relativePath, config) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await page.goto(`http://localhost:${config.port}${relativePath}`, {
waitUntil: 'load',
// Remove the timeout
timeout: 0
}); // make sure relative paths work as expected
if (config.waitContentTimeout !== undefined) {
await new Promise(resolve => setTimeout(resolve, config.waitContentTimeout));
}
await page.setContent(html, {
timeout: config.contentTimeout
}); // overwrite the page content with what was generated from the markdown
let index = 0;
for (const stylesheet of config.stylesheet) {
if (index === 0) {
index++;
// 如果是纯粹的html,不需要markdown的样式
if (config.isHtml) {
continue;
}
}
await page.addStyleTag((0, is_http_url_1.isHttpUrl)(stylesheet) ? { url: stylesheet } : { path: stylesheet });
}
if (config.css) {
await page.addStyleTag({ content: config.css });
}
for (const scriptTagOptions of config.script) {
await page.addScriptTag(scriptTagOptions);
}
/**
* Trick to wait for network to be idle.
*/
await Promise.all([
page.waitForNetworkIdle(),
page.evaluate(() => history.pushState(undefined, "", "#")), /* eslint no-undef: off */
]);
let outputFileContent = "";
if (config.devtools) {
await new Promise((resolve) => page.on("close", resolve));
}
else if (config.as_html) {
outputFileContent = await page.content();
}
else {
if (!config.isHtml) {
await page.emulateMediaType(config.page_media_type);
}
outputFileContent = await page.pdf(config.pdf_options);
}
return outputFileContent;
}
async function generateOutputs(params, relativePath, config) {
const browser = await puppeteer_1.default.launch({
devtools: config.devtools,
...config.launch_options,
});
const page = await browser.newPage();
const results = [];
for (let i = 0; i < params.length; i++) {
const param = params[i];
if (!param) {
continue;
}
const outputFileContent = await makeContent(page, param.html, relativePath, param.dest
? {
...config,
dest: param.dest,
}
: config);
results.push(config.devtools ? undefined : {
filename: param.dest ?? config.dest,
content: outputFileContent,
});
}
await browser.close();
return results;
}
exports.generateOutputs = generateOutputs;
;