Unverified Commit eec45c83 authored by Yukai Huang's avatar Yukai Huang Committed by GitHub
Browse files

Feature/syntax hilight on PDF (#1308)



Fix syntax highlight on PDF
Co-authored-by: default avatarkondouagi <kondouagi@gmail.com>
parents 56cc64e9 0dfb8a32
......@@ -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)
......
......@@ -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)
})
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment