Skip to content

Commit 0dfb8a3

Browse files
author
kondouagi
committed
feat: add syntax hilight on pdf
Signed-off-by: kondouagi <kondouagi@gmail.com>
1 parent 56cc64e commit 0dfb8a3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ config.sslCAPath.forEach(function (capath, i, array) {
200200
array[i] = path.resolve(appRootPath, capath)
201201
})
202202

203+
config.appRootPath = appRootPath
203204
config.sslCertPath = path.resolve(appRootPath, config.sslCertPath)
204205
config.sslKeyPath = path.resolve(appRootPath, config.sslKeyPath)
205206
config.dhParamPath = path.resolve(appRootPath, config.dhParamPath)

lib/response.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,22 @@ function actionPDF (req, res, note) {
316316
var content = extracted.markdown
317317
var title = models.Note.decodeTitle(note.title)
318318

319+
var highlightCssPath = path.join(config.appRootPath, '/node_modules/highlight.js/styles/github-gist.css')
320+
319321
if (!fs.existsSync(config.tmpPath)) {
320322
fs.mkdirSync(config.tmpPath)
321323
}
322-
var path = config.tmpPath + '/' + Date.now() + '.pdf'
324+
var pdfPath = config.tmpPath + '/' + Date.now() + '.pdf'
323325
content = content.replace(/\]\(\//g, '](' + url + '/')
324-
markdownpdf().from.string(content).to(path, function () {
325-
if (!fs.existsSync(path)) {
326-
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path)
326+
var markdownpdfOptions = {
327+
highlightCssPath: highlightCssPath
328+
}
329+
markdownpdf(markdownpdfOptions).from.string(content).to(pdfPath, function () {
330+
if (!fs.existsSync(pdfPath)) {
331+
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath)
327332
return errorInternalError(res)
328333
}
329-
var stream = fs.createReadStream(path)
334+
var stream = fs.createReadStream(pdfPath)
330335
var filename = title
331336
// Be careful of special characters
332337
filename = encodeURIComponent(filename)
@@ -336,7 +341,7 @@ function actionPDF (req, res, note) {
336341
res.setHeader('Content-Type', 'application/pdf; charset=UTF-8')
337342
res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling
338343
stream.pipe(res)
339-
fs.unlinkSync(path)
344+
fs.unlinkSync(pdfPath)
340345
})
341346
}
342347

0 commit comments

Comments
 (0)