From e39697061a45df77c659eb83eac2ae5063e13272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 22 Aug 2023 19:42:51 -0400 Subject: [PATCH] fix: closes #11921 --- src/controllers/topics.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/controllers/topics.js b/src/controllers/topics.js index fd3a3b9966..05de930053 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -206,13 +206,13 @@ async function addTags(topicData, req, res, currentPage) { const postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10)); let description = ''; if (postAtIndex && postAtIndex.content) { - description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)); + description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)).trim(); } if (description.length > 160) { description = `${description.slice(0, 157)}...`; } - description = description.replace(/\n/g, ' '); + description = description.replace(/\n/g, ' ').trim(); let mainPost = topicData.posts.find(p => parseInt(p.index, 10) === 0); if (!mainPost) { @@ -224,18 +224,10 @@ async function addTags(topicData, req, res, currentPage) { name: 'title', content: topicData.titleRaw, }, - { - name: 'description', - content: description, - }, { property: 'og:title', content: topicData.titleRaw, }, - { - property: 'og:description', - content: description, - }, { property: 'og:type', content: 'article', @@ -254,6 +246,19 @@ async function addTags(topicData, req, res, currentPage) { }, ]; + if (description && description.length) { + res.locals.metaTags.push( + { + name: 'description', + content: description, + }, + { + property: 'og:description', + content: description, + }, + ); + } + await addOGImageTags(res, topicData, postAtIndex); const page = currentPage > 1 ? `?page=${currentPage}` : '';