overleaf/services/web/app/coffee/Features/Wiki/WikiController.coffee

47 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-07-30 10:44:03 -04:00
request = require("request")
settings = require("settings-sharelatex")
logger = require("logger-sharelatex")
ErrorController = require "../Errors/ErrorController"
module.exports = WikiController =
getPage: (req, res, next) ->
page = req.url.replace(/^\/learn/, "").replace(/^\//, "")
if page == ""
page = "Main_Page"
logger.log page: page, "getting page from wiki"
WikiController._getPageContent "Contents", (error, contents) ->
return next(error) if error?
WikiController._getPageContent page, (error, page) ->
return next(error) if error?
2014-08-05 12:08:38 -04:00
if page.title == "Main Page"
title = "Documentation"
else
title = page.title
res.render "wiki/page", {
page: page
contents: contents
2014-08-05 12:08:38 -04:00
title: title
}
2014-07-30 10:44:03 -04:00
_getPageContent: (page, callback = (error, data = { content: "", title: "" }) ->) ->
2014-07-30 10:44:03 -04:00
request {
url: "#{settings.apis.wiki.url}/learn-scripts/api.php"
2014-07-30 10:44:03 -04:00
qs: {
page: decodeURI(page)
2014-07-30 10:44:03 -04:00
action: "parse"
format: "json"
}
}, (err, response, data)->
return callback(err) if err?
2014-07-30 10:44:03 -04:00
try
data = JSON.parse(data)
catch err
logger.err err:err, data:data, "error parsing data from wiki"
callback null, {
content: data?.parse?.text?['*']
title: data?.parse?.title
2014-07-30 10:44:03 -04:00
}