Files
NodeBB/src/middleware/render.js

132 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-10-11 23:05:33 -04:00
'use strict';
2016-10-05 15:22:35 +03:00
var async = require('async');
2015-10-11 23:05:33 -04:00
var nconf = require('nconf');
2016-07-08 17:09:47 -04:00
var validator = require('validator');
2017-02-02 13:11:27 +03:00
var winston = require('winston');
2016-04-30 10:47:28 +03:00
var plugins = require('../plugins');
2015-10-11 23:05:33 -04:00
var translator = require('../../public/src/modules/translator');
module.exports = function (middleware) {
middleware.processRender = function (req, res, next) {
2015-10-11 23:05:33 -04:00
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
var render = res.render;
res.render = function (template, options, fn) {
2016-04-30 10:47:28 +03:00
var self = this;
var req = this.req;
var defaultFn = function (err, str) {
2016-04-30 10:47:28 +03:00
if (err) {
return next(err);
}
self.send(str);
};
2015-10-11 23:05:33 -04:00
2016-04-30 10:47:28 +03:00
options = options || {};
2017-02-18 14:00:29 -07:00
if (typeof options === 'function') {
2015-10-11 23:05:33 -04:00
fn = options;
options = {};
}
2017-02-18 14:00:29 -07:00
if (typeof fn !== 'function') {
2016-10-05 15:22:35 +03:00
fn = defaultFn;
}
2015-10-11 23:05:33 -04:00
2016-10-05 15:22:35 +03:00
var ajaxifyData;
async.waterfall([
function (next) {
2016-10-05 15:22:35 +03:00
options.loggedIn = !!req.uid;
options.relative_path = nconf.get('relative_path');
2017-02-18 12:30:49 -07:00
options.template = { name: template };
2016-10-05 15:22:35 +03:00
options.template[template] = true;
options.url = (req.baseUrl + req.path).replace(/^\/api/, '');
options.bodyClass = buildBodyClass(req);
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options }, next);
2016-12-13 14:22:37 +03:00
},
function (data, next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:middleware.render', { req: res, res: res, templateData: data.templateData }, next);
2016-12-13 14:22:37 +03:00
},
function (data, next) {
options = data.templateData;
2016-10-05 15:22:35 +03:00
res.locals.template = template;
options._locals = undefined;
if (res.locals.isAPI) {
if (req.route && req.route.path === '/api/') {
options.title = '[[pages:home]]';
}
return res.json(options);
2016-04-30 10:47:28 +03:00
}
2015-10-11 23:05:33 -04:00
2016-10-05 15:22:35 +03:00
ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
async.parallel({
header: function (next) {
2016-10-05 15:22:35 +03:00
renderHeaderFooter('renderHeader', req, res, options, next);
},
content: function (next) {
2016-10-05 15:22:35 +03:00
render.call(self, template, options, next);
},
footer: function (next) {
2016-10-05 15:22:35 +03:00
renderHeaderFooter('renderFooter', req, res, options, next);
2017-02-17 19:31:21 -07:00
},
2016-10-05 15:22:35 +03:00
}, next);
},
function (results, next) {
2016-10-05 15:22:35 +03:00
var str = results.header +
(res.locals.postHeader || '') +
2017-02-12 20:36:12 +03:00
results.content + '<script id="ajaxify-data"></script>' +
2016-10-05 15:22:35 +03:00
(res.locals.preFooter || '') +
results.footer;
translate(str, req, res, next);
},
function (translated, next) {
2017-02-13 22:42:47 +03:00
translated = translated.replace('<script id="ajaxify-data"></script>', function () {
2017-02-13 22:24:17 +03:00
return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
});
2017-02-12 20:36:12 +03:00
next(null, translated);
2017-02-17 19:31:21 -07:00
},
2016-10-05 15:22:35 +03:00
], fn);
2015-10-11 23:05:33 -04:00
};
next();
};
2016-10-05 15:22:35 +03:00
function renderHeaderFooter(method, req, res, options, next) {
if (res.locals.renderHeader) {
middleware[method](req, res, options, next);
} else if (res.locals.renderAdminHeader) {
middleware.admin[method](req, res, options, next);
} else {
next(null, '');
}
}
function translate(str, req, res, next) {
2017-02-18 01:51:11 -07:00
var language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
2016-10-05 15:22:35 +03:00
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
translator.translate(str, language, function (translated) {
2016-10-05 15:22:35 +03:00
next(null, translator.unescape(translated));
});
}
2015-10-16 18:43:40 -04:00
function buildBodyClass(req) {
var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
2015-10-16 18:43:40 -04:00
var parts = clean.split('/').slice(0, 3);
parts.forEach(function (p, index) {
2017-02-02 13:11:27 +03:00
try {
p = decodeURIComponent(p);
} catch (err) {
winston.error(err.message);
p = '';
}
2015-10-30 15:43:04 -04:00
parts[index] = index ? parts[0] + '-' + p : 'page-' + (p || 'home');
2015-10-16 18:43:40 -04:00
});
return parts.join(' ');
}
2016-03-20 15:11:32 -05:00
};