mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-09 07:25:46 +01:00
re-implementing meta and link tags part 1
This commit is contained in:
@@ -3,10 +3,10 @@
|
||||
var categoriesController = {},
|
||||
async = require('async'),
|
||||
qs = require('querystring'),
|
||||
categoryTools = require('../categoryTools'),
|
||||
user = require('../user'),
|
||||
categories = require('../categories'),
|
||||
topics = require('../topics');
|
||||
categoryTools = require('./../categoryTools'),
|
||||
user = require('./../user'),
|
||||
categories = require('./../categories'),
|
||||
topics = require('./../topics');
|
||||
|
||||
categoriesController.recent = function(req, res, next) {
|
||||
var uid = (req.user) ? req.user.uid : 0;
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
var topicsController = {},
|
||||
async = require('async'),
|
||||
S = require('string'),
|
||||
validator = require('validator'),
|
||||
nconf = require('nconf'),
|
||||
qs = require('querystring'),
|
||||
user = require('../user'),
|
||||
topics = require('../topics'),
|
||||
threadTools = require('../threadTools');
|
||||
user = require('./../user'),
|
||||
meta = require('./../meta'),
|
||||
topics = require('./../topics'),
|
||||
threadTools = require('./../threadTools'),
|
||||
utils = require('./../../public/src/utils');
|
||||
|
||||
topicsController.get = function(req, res, next) {
|
||||
var tid = req.params.topic_id,
|
||||
@@ -72,15 +77,12 @@ topicsController.get = function(req, res, next) {
|
||||
|
||||
next(null, topicData);
|
||||
|
||||
/*var ogImageUrl = meta.config['brand:logo'];
|
||||
var ogImageUrl = meta.config['brand:logo'];
|
||||
if(ogImageUrl && ogImageUrl.indexOf('http') === -1) {
|
||||
ogImageUrl = nconf.get('url') + ogImageUrl;
|
||||
}
|
||||
|
||||
app.build_header({
|
||||
req: req,
|
||||
res: res,
|
||||
metaTags: [
|
||||
res.locals.metaTags = [
|
||||
{
|
||||
name: "title",
|
||||
content: topicData.title
|
||||
@@ -125,8 +127,9 @@ topicsController.get = function(req, res, next) {
|
||||
property: 'article:section',
|
||||
content: topicData.category.name
|
||||
}
|
||||
],
|
||||
linkTags: [
|
||||
];
|
||||
|
||||
res.locals.linkTags = [
|
||||
{
|
||||
rel: 'alternate',
|
||||
type: 'application/rss+xml',
|
||||
@@ -136,13 +139,8 @@ topicsController.get = function(req, res, next) {
|
||||
rel: 'up',
|
||||
href: nconf.get('url') + '/category/' + topicData.category.slug
|
||||
}
|
||||
]
|
||||
}, function (err, header) {
|
||||
next(err, {
|
||||
header: header,
|
||||
posts: topicData
|
||||
});
|
||||
});*/
|
||||
];
|
||||
|
||||
}
|
||||
], function (err, posts) {
|
||||
if (err) {
|
||||
|
||||
@@ -102,20 +102,16 @@ function catch404(req, res, next) {
|
||||
res.status(404);
|
||||
|
||||
if (isClientScript.test(req.url)) {
|
||||
// Handle missing client-side scripts
|
||||
res.type('text/javascript').send(200, '');
|
||||
} else if (isLanguage.test(req.url)) {
|
||||
// Handle languages by sending an empty object
|
||||
res.json(200, {});
|
||||
} else if (req.accepts('html')) {
|
||||
// respond with html page
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn('Route requested but not found: ' + req.url);
|
||||
}
|
||||
|
||||
res.redirect(nconf.get('relative_path') + '/404');
|
||||
} else if (req.accepts('json')) {
|
||||
// respond with json
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn('Route requested but not found: ' + req.url);
|
||||
}
|
||||
@@ -124,7 +120,6 @@ function catch404(req, res, next) {
|
||||
error: 'Not found'
|
||||
});
|
||||
} else {
|
||||
// default to plain-text. send()
|
||||
res.type('txt').send('Not found');
|
||||
}
|
||||
}
|
||||
@@ -137,7 +132,6 @@ module.exports = function(app, data) {
|
||||
app.set('view engine', 'tpl');
|
||||
app.set('views', path.join(__dirname, '../../public/templates'));
|
||||
|
||||
// Pre-router middlewares
|
||||
app.use(express.compress());
|
||||
|
||||
app.use(express.favicon(path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico')));
|
||||
@@ -163,28 +157,21 @@ module.exports = function(app, data) {
|
||||
|
||||
app.use(express.csrf()); // todo, make this a conditional middleware
|
||||
|
||||
// Local vars, other assorted setup
|
||||
app.use(function (req, res, next) {
|
||||
res.locals.csrf_token = req.session._csrf;
|
||||
|
||||
// Disable framing
|
||||
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(middleware.processRender);
|
||||
|
||||
// Authentication Routes
|
||||
auth.initialize(app);
|
||||
|
||||
routeCurrentTheme(app, data.currentThemeData);
|
||||
|
||||
// Route paths to screenshots for installed themes
|
||||
routeThemeScreenshots(app, data.themesData);
|
||||
|
||||
app.use(app.router);
|
||||
|
||||
// Static directory /public
|
||||
app.use(nconf.get('relative_path'), express.static(path.join(__dirname, '../../', 'public'), {
|
||||
maxAge: app.enabled('cache') ? 5184000000 : 0
|
||||
}));
|
||||
|
||||
@@ -6,6 +6,7 @@ var app,
|
||||
middleware = {},
|
||||
async = require('async'),
|
||||
path = require('path'),
|
||||
winston = require('winston'),
|
||||
validator = require('validator'),
|
||||
fs = require('fs'),
|
||||
nconf = require('nconf'),
|
||||
@@ -89,13 +90,8 @@ middleware.buildHeader = function(req, res, next) {
|
||||
async.parallel([
|
||||
function(next) {
|
||||
// temp, don't forget to set metaTags and linkTags to res.locals.header
|
||||
middleware.build_header({
|
||||
req: req,
|
||||
res: res
|
||||
}, function(err, template) {
|
||||
res.locals.header = template;
|
||||
next(err);
|
||||
});
|
||||
res.locals.header = true;
|
||||
next();
|
||||
},
|
||||
function(next) {
|
||||
// this is slower than the original implementation because the rendered template is not cached
|
||||
@@ -115,10 +111,11 @@ middleware.buildHeader = function(req, res, next) {
|
||||
};
|
||||
|
||||
/**
|
||||
* TODO: switch signature to req, res, callback
|
||||
* `options` object requires: req, res
|
||||
* accepts: metaTags, linkTags
|
||||
*/
|
||||
middleware.build_header = function (options, callback) {
|
||||
middleware.renderHeader = function (options, callback) {
|
||||
var custom_header = {
|
||||
'navigation': []
|
||||
};
|
||||
@@ -168,9 +165,9 @@ middleware.build_header = function (options, callback) {
|
||||
};
|
||||
|
||||
var uid = '0';
|
||||
|
||||
console.log(options.res.locals.metaTags);
|
||||
// Meta Tags
|
||||
/*templateValues.metaTags = defaultMetaTags.concat(options.metaTags || []).map(function(tag) {
|
||||
templateValues.metaTags = defaultMetaTags.concat(options.res.locals.metaTags || []).map(function(tag) {
|
||||
if(!tag || typeof tag.content !== 'string') {
|
||||
winston.warn('Invalid meta tag. ', tag);
|
||||
return tag;
|
||||
@@ -180,7 +177,7 @@ middleware.build_header = function (options, callback) {
|
||||
return escapeList[tag] || tag;
|
||||
});
|
||||
return tag;
|
||||
});*/
|
||||
});
|
||||
|
||||
// Link Tags
|
||||
/*templateValues.linkTags = defaultLinkTags.concat(options.linkTags || []);
|
||||
@@ -204,7 +201,7 @@ middleware.build_header = function (options, callback) {
|
||||
async.parallel([
|
||||
function(next) {
|
||||
translator.get('pages:' + path.basename(options.req.url), function(translated) {
|
||||
/*var metaTitle = templateValues.metaTags.filter(function(tag) {
|
||||
var metaTitle = templateValues.metaTags.filter(function(tag) {
|
||||
return tag.name === 'title';
|
||||
});
|
||||
if (translated) {
|
||||
@@ -213,7 +210,7 @@ middleware.build_header = function (options, callback) {
|
||||
templateValues.browserTitle = metaTitle[0].content;
|
||||
} else {
|
||||
templateValues.browserTitle = meta.config.browserTitle || 'NodeBB';
|
||||
}*/
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
@@ -260,18 +257,21 @@ middleware.processRender = function(req, res, next) {
|
||||
}
|
||||
|
||||
render.call(self, template, options, function(err, str) {
|
||||
if (res.locals.header) {
|
||||
str = res.locals.header + str;
|
||||
}
|
||||
|
||||
if (res.locals.footer) {
|
||||
str = str + res.locals.footer;
|
||||
}
|
||||
|
||||
if (str) {
|
||||
if (res.locals.header) {
|
||||
middleware.renderHeader({
|
||||
req: req,
|
||||
res: res
|
||||
}, function(err, template) {
|
||||
str = template + str;
|
||||
|
||||
translator.translate(str, function(translated) {
|
||||
fn(err, translated);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
fn(err, str);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user