letter-press
Advanced tools
Comparing version 1.1.3 to 1.1.4
38
ghmd.js
@@ -9,23 +9,25 @@ // based on 1000ch/node-github-markdown | ||
module.exports = (title, markdown) => { | ||
const markdownIt = new MarkdownIt({ | ||
html: true, | ||
breaks: true, | ||
langPrefix: 'hljs ', | ||
highlight: (string, lang) => { | ||
try { | ||
if (lang) return hljs.highlight(lang, string).value | ||
return hljs.highlightAuto(string).value | ||
} catch (err) { | ||
console.error(err) | ||
return new Promise((resolve, reject) => { | ||
const markdownIt = new MarkdownIt({ | ||
html: true, | ||
breaks: true, | ||
langPrefix: 'hljs ', | ||
highlight: (string, lang) => { | ||
try { | ||
if (lang) return hljs.highlight(lang, string).value | ||
return hljs.highlightAuto(string).value | ||
} catch (err) { | ||
reject(err) | ||
} | ||
} | ||
return '' | ||
} | ||
}) | ||
}) | ||
return pug.renderFile(path.join(__dirname, 'ghmd.pug'), { | ||
pretty: true, | ||
title: title, | ||
basedir: path.resolve('node_modules'), | ||
content: markdownIt.render(markdown) | ||
const file = pug.renderFile(path.join(__dirname, 'ghmd.pug'), { | ||
pretty: true, | ||
title: title, | ||
basedir: path.resolve('node_modules'), | ||
content: markdownIt.render(markdown) | ||
}) | ||
resolve(file) | ||
}) | ||
} |
67
index.js
const fs = require('fs') | ||
const path = require('path') | ||
const pify = require('pify') | ||
const ghmd = require('./ghmd') | ||
@@ -10,32 +9,64 @@ const puppeteer = require('puppeteer') | ||
if (!fs.existsSync(dist)) fs.mkdirSync(dist) | ||
write(path.join(dist, id + '.md'), md) | ||
const html = ghmd(id, md) | ||
press(id, md, dist).catch(console.error) | ||
} | ||
async function press (id, md, dist) { | ||
const mdPath = path.join(dist, id + '.md') | ||
const htmlPath = path.join(dist, id + '.html') | ||
const pdfPath = path.join(dist, id + '.pdf') | ||
write(htmlPath, html) | ||
.then(htmlPath => print(htmlPath, pdfPath)) | ||
.catch(err => { throw err }) | ||
async function print (html, pdf) { | ||
const browser = await puppeteer.launch() | ||
try { | ||
// write md | ||
write(mdPath, md) | ||
.then(file => console.log('File written: ' + file)) | ||
.catch(console.error) | ||
// write html | ||
const html = await ghmd(id, md).catch(e => console.error(e)) | ||
await write(htmlPath, html) | ||
.then(file => console.log('File written: ' + file)) | ||
.catch(console.error) | ||
// write pdf | ||
pdf(htmlPath, pdfPath) | ||
.then(file => console.log('File written: ' + file)) | ||
.catch(console.error) | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
async function pdf (file, out) { | ||
let browser | ||
try { | ||
browser = await puppeteer.launch() | ||
const page = await browser.newPage() | ||
await page.goto('file://' + html, { | ||
await page.goto('file://' + file, { | ||
waitUntil: 'networkidle' | ||
}) | ||
await page.pdf({ | ||
path: pdf, | ||
path: out, | ||
format: 'A4' | ||
}) | ||
browser.close() | ||
console.log('File written: ' + pdf) | ||
return Promise.resolve(pdf) | ||
return out | ||
} catch (e) { | ||
if (browser && browser.close) { | ||
console.error('Error: Problem printing PDF. Closing browser...') | ||
browser.close() | ||
} | ||
throw e | ||
} | ||
} | ||
function write (file, data) { | ||
return pify(fs.writeFile)(file, data).then(err => { | ||
if (err) throw err | ||
console.log('File written: ' + file) | ||
return Promise.resolve(file) | ||
function write (file, data) { | ||
return new Promise((resolve, reject) => { | ||
fs.writeFile(file, data, err => { | ||
if (err) { | ||
reject(err.toString()) | ||
return | ||
} | ||
resolve(file) | ||
}) | ||
} | ||
}) | ||
} |
{ | ||
"name": "letter-press", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"main": "index.js", | ||
@@ -17,3 +17,2 @@ "scripts": { | ||
"markdown-it": "^8.4.0", | ||
"pify": "^3.0.0", | ||
"primer-css": "^9.3.0", | ||
@@ -20,0 +19,0 @@ "pug": "^2.0.0-rc.4", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7586
5
167
- Removedpify@^3.0.0
- Removedpify@3.0.0(transitive)