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