Fix broken images in PDF caused by misconfigred server URL

As it turns out, if the serverURL can't be generated correctly, HackMD
will use relative paths in image upload. This causes broken links in
PDF.

With this commit we force absolute links during PDF creation which
hopefully fixes the problem.

Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
This commit is contained in:
Sheogorath 2018-06-24 01:00:20 +02:00
parent 7c7cc289f2
commit f69e77de42
No known key found for this signature in database
GPG Key ID: 1F05CC3635CDDFFD
1 changed files with 4 additions and 1 deletions

View File

@ -327,15 +327,18 @@ function actionInfo (req, res, note) {
} }
function actionPDF (req, res, note) { function actionPDF (req, res, note) {
var url = config.serverURL || 'http://' + req.get('host')
var body = note.content var body = note.content
var extracted = models.Note.extractMeta(body) var extracted = models.Note.extractMeta(body)
var content = extracted.markdown
var title = models.Note.decodeTitle(note.title) var title = models.Note.decodeTitle(note.title)
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 path = config.tmpPath + '/' + Date.now() + '.pdf'
markdownpdf().from.string(extracted.markdown).to(path, function () { content = content.replace(/\]\(\//g, '](' + url + '/')
markdownpdf().from.string(content).to(path, function () {
var stream = fs.createReadStream(path) var stream = fs.createReadStream(path)
var filename = title var filename = title
// Be careful of special characters // Be careful of special characters