feat: #7743 meta/tags.js

v1.18.x
Julian Lam 6 years ago
parent 79eed9ae60
commit 7b90863971

@ -1,142 +1,136 @@
'use strict'; 'use strict';
var nconf = require('nconf'); const nconf = require('nconf');
var async = require('async'); const winston = require('winston');
var winston = require('winston');
var plugins = require('../plugins'); const plugins = require('../plugins');
var Meta = require('../meta'); const Meta = require('../meta');
var utils = require('../utils'); const utils = require('../utils');
var Tags = module.exports; const Tags = module.exports;
Tags.parse = function (req, data, meta, link, callback) { Tags.parse = async (req, data, meta, link) => {
async.parallel({ // Meta tags
tags: function (next) { const defaultTags = [{
var defaultTags = [{ name: 'viewport',
name: 'viewport', content: 'width=device-width, initial-scale=1.0',
content: 'width=device-width, initial-scale=1.0', }, {
}, { name: 'content-type',
name: 'content-type', content: 'text/html; charset=UTF-8',
content: 'text/html; charset=UTF-8', noEscape: true,
noEscape: true, }, {
}, { name: 'apple-mobile-web-app-capable',
name: 'apple-mobile-web-app-capable', content: 'yes',
content: 'yes', }, {
}, { name: 'mobile-web-app-capable',
name: 'mobile-web-app-capable', content: 'yes',
content: 'yes', }, {
}, { property: 'og:site_name',
property: 'og:site_name', content: Meta.config.title || 'NodeBB',
content: Meta.config.title || 'NodeBB', }, {
}, { name: 'msapplication-badge',
name: 'msapplication-badge', content: 'frequency=30; polling-uri=' + nconf.get('url') + '/sitemap.xml',
content: 'frequency=30; polling-uri=' + nconf.get('url') + '/sitemap.xml', noEscape: true,
noEscape: true, }];
}];
if (Meta.config.keywords) { if (Meta.config.keywords) {
defaultTags.push({ defaultTags.push({
name: 'keywords', name: 'keywords',
content: Meta.config.keywords, content: Meta.config.keywords,
}); });
} }
if (Meta.config['brand:logo']) { if (Meta.config['brand:logo']) {
defaultTags.push({ defaultTags.push({
name: 'msapplication-square150x150logo', name: 'msapplication-square150x150logo',
content: Meta.config['brand:logo'], content: Meta.config['brand:logo'],
noEscape: true, noEscape: true,
}); });
} }
plugins.fireHook('filter:meta.getMetaTags', { req: req, data: data, tags: defaultTags }, next); // Link Tags
}, var defaultLinks = [{
links: function (next) { rel: 'icon',
var defaultLinks = [{ type: 'image/x-icon',
rel: 'icon', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/favicon.ico' + (Meta.config['cache-buster'] ? '?' + Meta.config['cache-buster'] : ''),
type: 'image/x-icon', }, {
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/favicon.ico' + (Meta.config['cache-buster'] ? '?' + Meta.config['cache-buster'] : ''), rel: 'manifest',
}, { href: nconf.get('relative_path') + '/manifest.json',
rel: 'manifest', }];
href: nconf.get('relative_path') + '/manifest.json',
}];
if (plugins.hasListeners('filter:search.query')) { if (plugins.hasListeners('filter:search.query')) {
defaultLinks.push({ defaultLinks.push({
rel: 'search', rel: 'search',
type: 'application/opensearchdescription+xml', type: 'application/opensearchdescription+xml',
title: utils.escapeHTML(String(Meta.config.title || Meta.config.browserTitle || 'NodeBB')), title: utils.escapeHTML(String(Meta.config.title || Meta.config.browserTitle || 'NodeBB')),
href: nconf.get('relative_path') + '/osd.xml', href: nconf.get('relative_path') + '/osd.xml',
}); });
} }
// Touch icons for mobile-devices // Touch icons for mobile-devices
if (Meta.config['brand:touchIcon']) { if (Meta.config['brand:touchIcon']) {
defaultLinks.push({ defaultLinks.push({
rel: 'apple-touch-icon', rel: 'apple-touch-icon',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-orig.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-orig.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '36x36', sizes: '36x36',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-36.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-36.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '48x48', sizes: '48x48',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-48.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-48.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '72x72', sizes: '72x72',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-72.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-72.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '96x96', sizes: '96x96',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-96.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-96.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '144x144', sizes: '144x144',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-144.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-144.png',
}, { }, {
rel: 'icon', rel: 'icon',
sizes: '192x192', sizes: '192x192',
href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-192.png', href: nconf.get('relative_path') + nconf.get('upload_url') + '/system/touchicon-192.png',
}); });
} }
plugins.fireHook('filter:meta.getLinkTags', { req: req, data: data, links: defaultLinks }, next);
}, const results = await utils.promiseParallel({
}, async function (err, results) { tags: plugins.fireHook('filter:meta.getMetaTags', { req: req, data: data, tags: defaultTags }),
if (err) { links: plugins.fireHook('filter:meta.getLinkTags', { req: req, data: data, links: defaultLinks }),
return callback(err); });
meta = results.tags.tags.concat(meta || []).map(function (tag) {
if (!tag || typeof tag.content !== 'string') {
winston.warn('Invalid meta tag. ', tag);
return tag;
} }
meta = results.tags.tags.concat(meta || []).map(function (tag) { if (!tag.noEscape) {
if (!tag || typeof tag.content !== 'string') { tag.content = utils.escapeHTML(String(tag.content));
winston.warn('Invalid meta tag. ', tag); }
return tag;
}
if (!tag.noEscape) { return tag;
tag.content = utils.escapeHTML(String(tag.content));
}
return tag;
});
addSiteOGImage(meta);
addIfNotExists(meta, 'property', 'og:title', Meta.config.title || 'NodeBB');
var ogUrl = nconf.get('url') + (req.originalUrl !== '/' ? stripRelativePath(req.originalUrl) : '');
addIfNotExists(meta, 'property', 'og:url', ogUrl);
addIfNotExists(meta, 'name', 'description', Meta.config.description);
addIfNotExists(meta, 'property', 'og:description', Meta.config.description);
link = results.links.links.concat(link || []);
callback(null, {
meta: meta,
link: link,
});
}); });
addSiteOGImage(meta);
addIfNotExists(meta, 'property', 'og:title', Meta.config.title || 'NodeBB');
var ogUrl = nconf.get('url') + (req.originalUrl !== '/' ? stripRelativePath(req.originalUrl) : '');
addIfNotExists(meta, 'property', 'og:url', ogUrl);
addIfNotExists(meta, 'name', 'description', Meta.config.description);
addIfNotExists(meta, 'property', 'og:description', Meta.config.description);
link = results.links.links.concat(link || []);
return {
meta: meta,
link: link,
};
}; };
function addIfNotExists(meta, keyName, tagName, value) { function addIfNotExists(meta, keyName, tagName, value) {

Loading…
Cancel
Save