From 819917daf2590a1c2f3352076527c54cf1f006a7 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 17 Sep 2021 09:57:25 -0400 Subject: [PATCH] refactor: allow plugins to replace og:image, or specify additional og:image /cc @antosik --- src/meta/tags.js | 86 +++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/src/meta/tags.js b/src/meta/tags.js index 1f2c68e13f..4e5aeb6815 100644 --- a/src/meta/tags.js +++ b/src/meta/tags.js @@ -164,7 +164,7 @@ Tags.parse = async (req, data, meta, link) => { return tag; }); - addSiteOGImage(meta); + await addSiteOGImage(meta); addIfNotExists(meta, 'property', 'og:title', Meta.config.title || 'NodeBB'); const ogUrl = url + (req.originalUrl !== '/' ? stripRelativePath(req.originalUrl) : ''); @@ -211,49 +211,59 @@ function stripRelativePath(url) { return url; } -function addSiteOGImage(meta) { +async function addSiteOGImage(meta) { const key = Meta.config['og:image'] ? 'og:image' : 'brand:logo'; let ogImage = stripRelativePath(Meta.config[key] || ''); if (ogImage && !ogImage.startsWith('http')) { ogImage = url + ogImage; } - if (ogImage) { - meta.push({ - property: 'og:image', - content: ogImage, - noEscape: true, - }, { - property: 'og:image:url', - content: ogImage, - noEscape: true, - }); + const { images } = await plugins.hooks.fire('filter:meta.addSiteOGImage', { + images: [{ + url: ogImage || `${url}/assets/images/logo@3x.png`, + width: ogImage ? Meta.config[`${key}:width`] : 963, + height: ogImage ? Meta.config[`${key}:height`] : 225, + }], + }); - if (Meta.config[`${key}:width`] && Meta.config[`${key}:height`]) { - meta.push({ - property: 'og:image:width', - content: String(Meta.config[`${key}:width`]), - }, { - property: 'og:image:height', - content: String(Meta.config[`${key}:height`]), - }); + const properties = ['url', 'secure_url', 'type', 'width', 'height', 'alt']; + images.forEach((image) => { + for (const property of properties) { + if (image.hasOwnProperty(property)) { + switch (property) { + case 'url': { + meta.push({ + property: 'og:image', + content: image.url, + noEscape: true, + }, { + property: 'og:image:url', + content: image.url, + noEscape: true, + }); + break; + } + + case 'secure_url': { + meta.push({ + property: `og:${property}`, + content: image[property], + noEscape: true, + }); + break; + } + + case 'type': + case 'alt': + case 'width': + case 'height': { + meta.push({ + property: `og:image:${property}`, + content: String(image[property]), + }); + } + } + } } - } else { - // Push fallback logo - meta.push({ - property: 'og:image', - content: `${url}/assets/images/logo@3x.png`, - noEscape: true, - }, { - property: 'og:image:url', - content: `${url}/assets/images/logo@3x.png`, - noEscape: true, - }, { - property: 'og:image:width', - content: '963', - }, { - property: 'og:image:height', - content: '225', - }); - } + }); }