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