From 0dfb8a320d8366b08f7548dbd17f1623ffae1dd9 Mon Sep 17 00:00:00 2001 From: kondouagi Date: Mon, 21 Oct 2019 13:37:59 +0900 Subject: [PATCH] feat: add syntax hilight on pdf Signed-off-by: kondouagi --- lib/config/index.js | 1 + lib/response.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/config/index.js b/lib/config/index.js index 22e7ec47..b422da74 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -200,6 +200,7 @@ config.sslCAPath.forEach(function (capath, i, array) { array[i] = path.resolve(appRootPath, capath) }) +config.appRootPath = appRootPath config.sslCertPath = path.resolve(appRootPath, config.sslCertPath) config.sslKeyPath = path.resolve(appRootPath, config.sslKeyPath) config.dhParamPath = path.resolve(appRootPath, config.dhParamPath) diff --git a/lib/response.js b/lib/response.js index 10b57c79..ce7e4fc3 100644 --- a/lib/response.js +++ b/lib/response.js @@ -316,17 +316,22 @@ function actionPDF (req, res, note) { var content = extracted.markdown var title = models.Note.decodeTitle(note.title) + var highlightCssPath = path.join(config.appRootPath, '/node_modules/highlight.js/styles/github-gist.css') + if (!fs.existsSync(config.tmpPath)) { fs.mkdirSync(config.tmpPath) } - var path = config.tmpPath + '/' + Date.now() + '.pdf' + var pdfPath = config.tmpPath + '/' + Date.now() + '.pdf' content = content.replace(/\]\(\//g, '](' + url + '/') - markdownpdf().from.string(content).to(path, function () { - if (!fs.existsSync(path)) { - logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path) + var markdownpdfOptions = { + highlightCssPath: highlightCssPath + } + markdownpdf(markdownpdfOptions).from.string(content).to(pdfPath, function () { + if (!fs.existsSync(pdfPath)) { + logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath) return errorInternalError(res) } - var stream = fs.createReadStream(path) + var stream = fs.createReadStream(pdfPath) var filename = title // Be careful of special characters filename = encodeURIComponent(filename) @@ -336,7 +341,7 @@ function actionPDF (req, res, note) { res.setHeader('Content-Type', 'application/pdf; charset=UTF-8') res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling stream.pipe(res) - fs.unlinkSync(path) + fs.unlinkSync(pdfPath) }) }