feat: add syntax hilight on pdf

Signed-off-by: kondouagi <kondouagi@gmail.com>
This commit is contained in:
kondouagi 2019-10-21 13:37:59 +09:00
parent 56cc64e910
commit 0dfb8a320d
2 changed files with 12 additions and 6 deletions

View File

@ -200,6 +200,7 @@ config.sslCAPath.forEach(function (capath, i, array) {
array[i] = path.resolve(appRootPath, capath) array[i] = path.resolve(appRootPath, capath)
}) })
config.appRootPath = appRootPath
config.sslCertPath = path.resolve(appRootPath, config.sslCertPath) config.sslCertPath = path.resolve(appRootPath, config.sslCertPath)
config.sslKeyPath = path.resolve(appRootPath, config.sslKeyPath) config.sslKeyPath = path.resolve(appRootPath, config.sslKeyPath)
config.dhParamPath = path.resolve(appRootPath, config.dhParamPath) config.dhParamPath = path.resolve(appRootPath, config.dhParamPath)

View File

@ -316,17 +316,22 @@ function actionPDF (req, res, note) {
var content = extracted.markdown var content = extracted.markdown
var title = models.Note.decodeTitle(note.title) 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)) { if (!fs.existsSync(config.tmpPath)) {
fs.mkdirSync(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 + '/') content = content.replace(/\]\(\//g, '](' + url + '/')
markdownpdf().from.string(content).to(path, function () { var markdownpdfOptions = {
if (!fs.existsSync(path)) { highlightCssPath: highlightCssPath
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path) }
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) return errorInternalError(res)
} }
var stream = fs.createReadStream(path) var stream = fs.createReadStream(pdfPath)
var filename = title var filename = title
// Be careful of special characters // Be careful of special characters
filename = encodeURIComponent(filename) filename = encodeURIComponent(filename)
@ -336,7 +341,7 @@ function actionPDF (req, res, note) {
res.setHeader('Content-Type', 'application/pdf; charset=UTF-8') res.setHeader('Content-Type', 'application/pdf; charset=UTF-8')
res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling res.setHeader('X-Robots-Tag', 'noindex, nofollow') // prevent crawling
stream.pipe(res) stream.pipe(res)
fs.unlinkSync(path) fs.unlinkSync(pdfPath)
}) })
} }