From 27b65a439ab56002d6932e73b10bc947483bb044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 8 Dec 2017 12:17:16 -0500 Subject: [PATCH] refactor of og:image code --- src/controllers/topics.js | 125 ++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 65 deletions(-) diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 053e1aa300..4ecaf7486e 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -205,16 +205,11 @@ function buildBreadcrumbs(topicData, callback) { } function addTags(topicData, req, res) { - function findPost(index) { - for (var i = 0; i < topicData.posts.length; i += 1) { - if (parseInt(topicData.posts[i].index, 10) === parseInt(index, 10)) { - return topicData.posts[i]; - } - } - } - var description = ''; - var postAtIndex = findPost(Math.max(0, req.params.post_index - 1)); + var postAtIndex = topicData.posts.find(function (postData) { + return parseInt(postData.index, 10) === parseInt(Math.max(0, req.params.post_index - 1), 10); + }); + var description = ''; if (postAtIndex && postAtIndex.content) { description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)); } @@ -222,27 +217,8 @@ function addTags(topicData, req, res) { if (description.length > 255) { description = description.substr(0, 255) + '...'; } - - var ogImageUrl = ''; - if (topicData.thumb) { - ogImageUrl = topicData.thumb; - } else if (topicData.category.backgroundImage && (!postAtIndex || !postAtIndex.index)) { - ogImageUrl = topicData.category.backgroundImage; - } else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) { - ogImageUrl = postAtIndex.user.picture; - } else if (meta.config['og:image']) { - ogImageUrl = meta.config['og:image']; - } else if (meta.config['brand:logo']) { - ogImageUrl = meta.config['brand:logo']; - } else { - ogImageUrl = '/logo.png'; - } - - if (typeof ogImageUrl === 'string' && ogImageUrl.indexOf('http') === -1) { - ogImageUrl = nconf.get('url') + ogImageUrl; - } - description = description.replace(/\n/g, ' '); + res.locals.metaTags = [ { name: 'title', @@ -264,16 +240,6 @@ function addTags(topicData, req, res) { property: 'og:type', content: 'article', }, - { - property: 'og:image', - content: ogImageUrl, - noEscape: true, - }, - { - property: 'og:image:url', - content: ogImageUrl, - noEscape: true, - }, { property: 'article:published_time', content: utils.toISOString(topicData.timestamp), @@ -288,32 +254,7 @@ function addTags(topicData, req, res) { }, ]; - topicData.posts.forEach(function (postData) { - var regex = /src\s*=\s*"(.+?)"/g; - var match = regex.exec(postData.content); - while (match !== null) { - var image = match[1]; - - if (image.startsWith(nconf.get('url') + '/plugins')) { - return; - } - if (!image.startsWith('http')) { - image = nconf.get('url') + image; - } - - res.locals.metaTags.push({ - property: 'og:image', - content: image, - noEscape: true, - }); - res.locals.metaTags.push({ - property: 'og:image:url', - content: image, - noEscape: true, - }); - match = regex.exec(postData.content); - } - }); + addOGImageTags(res, topicData, postAtIndex); res.locals.linkTags = [ { @@ -335,6 +276,60 @@ function addTags(topicData, req, res) { } } +function addOGImageTags(res, topicData, postAtIndex) { + var ogImageUrl = ''; + if (topicData.thumb) { + ogImageUrl = topicData.thumb; + } else if (topicData.category.backgroundImage && (!postAtIndex || !postAtIndex.index)) { + ogImageUrl = topicData.category.backgroundImage; + } else if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) { + ogImageUrl = postAtIndex.user.picture; + } else if (meta.config['og:image']) { + ogImageUrl = meta.config['og:image']; + } else if (meta.config['brand:logo']) { + ogImageUrl = meta.config['brand:logo']; + } else { + ogImageUrl = '/logo.png'; + } + + addOGImageTag(res, ogImageUrl); + addOGImageTagsForPosts(res, topicData.posts); +} + +function addOGImageTagsForPosts(res, posts) { + posts.forEach(function (postData) { + var regex = /src\s*=\s*"(.+?)"/g; + var match = regex.exec(postData.content); + while (match !== null) { + var image = match[1]; + + if (image.startsWith(nconf.get('url') + '/plugins')) { + return; + } + + addOGImageTag(res, image); + + match = regex.exec(postData.content); + } + }); +} + +function addOGImageTag(res, imageUrl) { + if (typeof imageUrl === 'string' && !imageUrl.startsWith('http')) { + imageUrl = nconf.get('url') + imageUrl; + } + res.locals.metaTags.push({ + property: 'og:image', + content: imageUrl, + noEscape: true, + }); + res.locals.metaTags.push({ + property: 'og:image:url', + content: imageUrl, + noEscape: true, + }); +} + topicsController.teaser = function (req, res, next) { var tid = req.params.topic_id;