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

@@ -213,7 +213,7 @@
info.break()
info.group({metadata, name:"core", inputs:config})
info("Plugin errors", die ? "(exit with error)" : "(displayed in generated image)")
const convert = ["jpeg", "png", "json", "markdown"].includes(config["config.output"]) ? config["config.output"] : null
const convert = ["jpeg", "png", "json", "markdown", "markdown-pdf"].includes(config["config.output"]) ? config["config.output"] : null
Object.assign(q, config)
if (/markdown/.test(convert))
info("Markdown cache", _markdown_cache)

View File

@@ -26,7 +26,7 @@
const pending = []
const {queries} = conf
const data = {animated:true, base:{}, config:{}, errors:[], plugins:{}, computed:{}}
const imports = {plugins:Plugins, templates:Templates, metadata:conf.metadata, ...utils, ...(convert === "markdown" ? {imgb64(url, options) {
const imports = {plugins:Plugins, templates:Templates, metadata:conf.metadata, ...utils, ...(/markdown/.test(convert) ? {imgb64(url, options) {
return options?.force ? utils.imgb64(...arguments) : url
}} : null)}
const experimental = new Set(decodeURIComponent(q["experimental.features"] ?? "").split(" ").map(x => x.trim().toLocaleLowerCase()).filter(x => x))
@@ -65,7 +65,7 @@
}
//Markdown output
if (convert === "markdown") {
if (/markdown/.test(convert)) {
//Retrieving template source
console.debug(`metrics/compute/${login} > markdown render`)
let source = image
@@ -95,7 +95,10 @@
for (const delimiters of [{openDelimiter:"<", closeDelimiter:">"}, {openDelimiter:"{", closeDelimiter:"}"}])
rendered = await ejs.render(rendered, {...data, s:imports.s, f:imports.format, embed}, {views, async:true, ...delimiters})
console.debug(`metrics/compute/${login} > success`)
return {rendered, mime:"text/plain"}
//Output
if (convert === "markdown-pdf")
return imports.svg.pdf(await imports.marked(rendered), {paddings:q["config.padding"] || conf.settings.padding, style:(conf.settings.extras?.css ?? conf.settings.extras?.default ? q["extras.css"] ?? "" : "")})
return {rendered, mime:"text/plain"}
}
//Rendering

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

View File

@@ -261,7 +261,7 @@
graphql, rest, plugins, conf,
die:q["plugins.errors.fatal"] ?? false,
verify:q.verify ?? false,
convert:["jpeg", "png", "json", "markdown"].includes(q["config.output"]) ? q["config.output"] : null,
convert:["jpeg", "png", "json", "markdown", "markdown-pdf"].includes(q["config.output"]) ? q["config.output"] : null,
}, {Plugins, Templates})
//Cache
if ((!debug)&&(cached)) {

View File

@@ -188,10 +188,11 @@ inputs:
default: svg
values:
- svg
- png # Does not support animations
- jpeg # Does not support animations and transparency
- json # Outputs a JSON file instead of an image
- markdown # Outputs a Markdown file instead of an image
- png # Does not support animations
- jpeg # Does not support animations and transparency
- json # Outputs a JSON file instead of an image
- markdown # Outputs a Markdown file instead of an image
- markdown-pdf # Outputs a Markdown file as PDF instead of an image
# Number of retries in case rendering fail
retries: