fix download as PDF feature (#9)

This commit is contained in:
orenom
2021-08-14 13:29:20 +02:00
parent ea1e7a021b
commit 599afa7550
2 changed files with 16 additions and 2 deletions
+4 -2
View File
@@ -106,11 +106,13 @@ module.exports = (app, utils) => {
return res.redirect(back)
})
app.post('/wiki/Special::DownloadAsPdf', (req, res, next) => {
app.post(/DownloadAsPdf/, (req, res, next) => {
if(!req.body.page) {
return res.redirect('/')
}
return res.redirect(`/w/index.php?title=Special%3ADownloadAsPdf&page=${req.body.page}&action=redirect-to-electron`)
let lang = req.body.lang || req.cookies.default_lang || config.default_lang
return res.redirect(`/w/index.php?title=Special%3ADownloadAsPdf&page=${req.body.page}&action=redirect-to-electron&lang=${lang}`)
})
}
+12
View File
@@ -171,6 +171,18 @@ module.exports = function(redis) {
const wiki_href_regx = /(href=\"(https:|http:|)\/\/([A-z.-]+\.)?(wikipedia.org|wikimedia.org|wikidata.org|mediawiki.org))/gm
data.html = data.html.replace(wiki_href_regx, `href="${protocol}${config.domain}`)
/**
* If we are on DownloadAsPdf page, we have to inject a input which
* holds the language value. Without this input, we can't access the
* language (not avail from the POST data).
* See more on https://codeberg.org/orenom/Wikiless/issues/9
*/
if(url.includes('%3ADownloadAsPdf')) {
let lang_input = `<input type='hidden' name='lang' value='${lang}'>`
let replace_str = `<input type='hidden' name='page'`
data.html = data.html.replace(replace_str, `${lang_input}${replace_str}`)
}
redis.setex(url, config.setexs.wikipage, data.html, (error) => {
if(error) {
console.log(`Error setting the ${url} key to Redis. Error: ${error}`)