Files
NodeBB/src/middleware/render.js

165 lines
4.8 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');
var meta = require('../meta');
var translator = require('../translator');
var widgets = require('../widgets');
2017-12-01 11:11:37 -05:00
var utils = require('../utils');
2015-10-11 23:05:33 -04:00
module.exports = function (middleware) {
middleware.processRender = function processRender(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 renderOverride(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;
2018-06-01 08:23:25 -04:00
var templateToRender;
2016-10-05 15:22:35 +03:00
async.waterfall([
function (next) {
2018-12-17 16:03:01 -05:00
options.loggedIn = req.uid > 0;
2016-10-05 15:22:35 +03:00
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;
2017-11-07 12:39:34 -05:00
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
2018-02-13 14:49:30 -05:00
options.bodyClass = buildBodyClass(req, res, options);
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) {
2018-06-01 08:23:25 -04:00
templateToRender = data.templateData.templateToRender || template;
2017-06-07 20:18:04 -04:00
plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: data.templateData }, next);
2016-12-13 14:22:37 +03:00
},
function parseTags(data, next) {
meta.tags.parse(req, data, res.locals.metaTags, res.locals.linkTags, function (err, tags) {
options._header = {
tags: tags,
};
next(err, data);
});
},
2016-12-13 14:22:37 +03:00
function (data, next) {
options = data.templateData;
widgets.render(req.uid, {
template: template + '.tpl',
url: options.url,
templateData: options,
req: req,
res: res,
}, next);
},
function (data, next) {
options.widgets = data;
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]]';
}
2018-06-06 15:46:43 -04:00
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
2016-10-05 15:22:35 +03:00
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) {
2018-06-01 08:23:25 -04:00
render.call(self, templateToRender, options, next);
2016-10-05 15:22:35 +03:00
},
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) {
var str = results.header +
(res.locals.postHeader || '') +
results.content + '<script id="ajaxify-data"></script>' +
(res.locals.preFooter || '') +
results.footer;
2016-10-05 15:22:35 +03:00
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';
2018-02-12 17:12:18 -05:00
if (res.locals.renderAdminHeader) {
language = (res.locals.config && res.locals.config.acpLang) || '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));
});
}
2018-02-13 14:49:30 -05:00
function buildBodyClass(req, res, templateData) {
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 {
2018-03-23 09:48:24 -04:00
p = utils.slugify(decodeURIComponent(p));
2017-02-02 13:11:27 +03:00
} catch (err) {
winston.error(err);
2017-02-02 13:11:27 +03:00
p = '';
}
2017-03-09 20:18:42 +03:00
p = validator.escape(String(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
});
2017-12-01 11:11:37 -05:00
if (templateData.template.topic) {
parts.push('page-topic-category-' + templateData.category.cid);
parts.push('page-topic-category-' + utils.slugify(templateData.category.name));
}
2018-02-13 14:49:30 -05:00
parts.push('page-status-' + res.statusCode);
2015-10-16 18:43:40 -04:00
return parts.join(' ');
}
2016-03-20 15:11:32 -05:00
};