Add markdown-pdf option as config output (#270)

This commit is contained in:
Simon Lecoq
2021-04-26 22:18:01 +02:00
committed by GitHub
parent 266b4d5726
commit 44cfde6845
7 changed files with 81 additions and 13 deletions

View File

@@ -23,7 +23,7 @@
prism_lang()
//Exports
export {fs, os, paths, url, util, processes, axios, git, opengraph, jimp, rss}
export {fs, os, paths, url, util, processes, axios, git, opengraph, jimp, rss, marked}
/**Returns module __dirname */
export function __module(module) {
@@ -238,6 +238,26 @@
/**SVG utils */
export const svg = {
/**Render as pdf */
async pdf(rendered, {paddings = "", style = ""} = {}) {
//Instantiate browser if needed
if (!svg.resize.browser) {
svg.resize.browser = await puppeteer.launch()
console.debug(`metrics/svg/pdf > started ${await svg.resize.browser.version()}`)
}
//Render through browser and print pdf
console.debug("metrics/svg/pdf > loading svg")
const page = await svg.resize.browser.newPage()
page.on("console", ({_text:text}) => console.debug(`metrics/svg/pdf > puppeteer > ${text}`))
await page.setContent(`<main class="markdown-body">${rendered}</main>`, {waitUntil:["load", "domcontentloaded", "networkidle2"]})
console.debug("metrics/svg/pdf > loaded svg successfully")
await page.addStyleTag({content:`main { margin: ${(Array.isArray(paddings) ? paddings : paddings.split(",")).join(" ")}; }${await fs.readFile(paths.join(__module(import.meta.url), "../../../node_modules", "@primer/css/dist/markdown.css")).catch(_ => "")}${style}`})
rendered = await page.pdf()
//Result
await page.close()
console.debug("metrics/svg/pdf > rendering complete")
return {rendered, mime:"application/pdf"}
},
/**Render and resize svg */
async resize(rendered, {paddings, convert}) {
//Instantiate browser if needed