From c26f2743924777aeff47c91c178e9a3b451626d0 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 21 Apr 2015 12:24:37 -0400 Subject: [PATCH] moved meta and link tag parsing out to a new module, meta/tags --- src/meta/tags.js | 58 ++++++++++++++++++++++++++++++++++++ src/middleware/middleware.js | 50 ++++--------------------------- 2 files changed, 63 insertions(+), 45 deletions(-) create mode 100644 src/meta/tags.js diff --git a/src/meta/tags.js b/src/meta/tags.js new file mode 100644 index 0000000000..222f77727b --- /dev/null +++ b/src/meta/tags.js @@ -0,0 +1,58 @@ +var nconf = require('nconf'), + validator = require('validator'), + plugins = require('../plugins'); + +module.exports = function(Meta) { + Meta.tags = {}; + + Meta.tags.parse = function(meta, link, callback) { + var defaultMetaTags = [{ + name: 'viewport', + content: 'width=device-width, initial-scale=1.0, user-scalable=no' + }, { + name: 'content-type', + content: 'text/html; charset=UTF-8' + }, { + name: 'apple-mobile-web-app-capable', + content: 'yes' + }, { + property: 'og:site_name', + content: Meta.config.title || 'NodeBB' + }, { + name: 'keywords', + content: Meta.config.keywords || '' + }, { + name: 'msapplication-badge', + content: 'frequency=30; polling-uri=' + nconf.get('url') + '/sitemap.xml' + }, { + name: 'msapplication-square150x150logo', + content: Meta.config['brand:logo'] || '' + }], + defaultLinkTags = [{ + rel: 'apple-touch-icon', + href: nconf.get('relative_path') + '/apple-touch-icon' + }]; + + meta = defaultMetaTags.concat(meta || []).map(function(tag) { + if(!tag || typeof tag.content !== 'string') { + winston.warn('Invalid meta tag. ', tag); + return tag; + } + + tag.content = validator.escape(tag.content); + return tag; + }); + + link = defaultLinkTags.concat(link || []); + link.unshift({ + rel: "icon", + type: "image/x-icon", + href: nconf.get('relative_path') + '/favicon.ico' + }); + + callback(null, { + meta: meta, + link: link + }); + }; +}; \ No newline at end of file diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 0601bb18a9..7ef23b28b0 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -191,33 +191,7 @@ middleware.buildHeader = function(req, res, next) { }; middleware.renderHeader = function(req, res, callback) { - var defaultMetaTags = [{ - name: 'viewport', - content: 'width=device-width, initial-scale=1.0, user-scalable=no' - }, { - name: 'content-type', - content: 'text/html; charset=UTF-8' - }, { - name: 'apple-mobile-web-app-capable', - content: 'yes' - }, { - property: 'og:site_name', - content: meta.config.title || 'NodeBB' - }, { - name: 'keywords', - content: meta.config.keywords || '' - }, { - name: 'msapplication-badge', - content: 'frequency=30; polling-uri=' + nconf.get('url') + '/sitemap.xml' - }, { - name: 'msapplication-square150x150logo', - content: meta.config['brand:logo'] || '' - }], - defaultLinkTags = [{ - rel: 'apple-touch-icon', - href: nconf.get('relative_path') + '/apple-touch-icon' - }], - templateValues = { + var templateValues = { bootswatchCSS: meta.config['theme:src'], title: meta.config.title || '', description: meta.config.description || '', @@ -236,23 +210,6 @@ middleware.renderHeader = function(req, res, callback) { templateValues.configJSON = JSON.stringify(res.locals.config); - templateValues.metaTags = defaultMetaTags.concat(res.locals.metaTags || []).map(function(tag) { - if(!tag || typeof tag.content !== 'string') { - winston.warn('Invalid meta tag. ', tag); - return tag; - } - - tag.content = validator.escape(tag.content); - return tag; - }); - - templateValues.linkTags = defaultLinkTags.concat(res.locals.linkTags || []); - templateValues.linkTags.unshift({ - rel: "icon", - type: "image/x-icon", - href: nconf.get('relative_path') + '/favicon.ico' - }); - async.parallel({ customCSS: function(next) { templateValues.useCustomCSS = parseInt(meta.config.useCustomCSS, 10) === 1; @@ -294,7 +251,8 @@ middleware.renderHeader = function(req, res, callback) { }); } }, - navigation: async.apply(navigation.get) + navigation: async.apply(navigation.get), + tags: async.apply(meta.tags.parse, res.locals.metaTags, res.locals.linkTags) }, function(err, results) { if (err) { return callback(err); @@ -311,6 +269,8 @@ middleware.renderHeader = function(req, res, callback) { templateValues.browserTitle = results.title; templateValues.navigation = results.navigation + templateValues.metaTags = results.tags.meta; + templateValues.metalinkTags = results.tags.link; templateValues.isAdmin = results.user.isAdmin; templateValues.user = results.user; templateValues.userJSON = JSON.stringify(results.user);