From 4934f57dfbbb7bdb029c7045ea3ce1e1bf2d5fd7 Mon Sep 17 00:00:00 2001 From: SahilDahekar Date: Thu, 14 Nov 2024 21:08:38 +0530 Subject: [PATCH] Added support for image formats --- scripts/build-rss.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/scripts/build-rss.js b/scripts/build-rss.js index 673da1398fe0..64da620fdb9b 100644 --- a/scripts/build-rss.js +++ b/scripts/build-rss.js @@ -63,6 +63,18 @@ module.exports = async function rssFeed(type, title, desc, outputPath) { throw new Error(`Missing required fields in posts: ${invalidPosts.map(p => p.title || p.slug).join(', ')}`); } + const mimeTypes = { + '.jpeg': 'image/jpeg', + '.jpg': 'image/jpeg', + '.png': 'image/png', + '.svg': 'image/svg+xml', + '.webp': 'image/webp', + '.gif': 'image/gif', + '.bmp': 'image/bmp', + '.tiff': 'image/tiff', + '.ico': 'image/x-icon' + }; + for (let post of posts) { const link = `${base}${post.slug}${tracking}`; const { title, excerpt, date } = post; @@ -78,17 +90,13 @@ module.exports = async function rssFeed(type, title, desc, outputPath) { pubDate }; if (post.cover) { - const enclosure = {}; - enclosure["@url"] = base + post.cover; - enclosure["@length"] = 15026; // dummy value, anything works - enclosure["@type"] = 'image/jpeg'; - if (typeof enclosure["@url"] === 'string') { - let tmp = enclosure["@url"].toLowerCase(); - if (tmp.indexOf('.png') >= 0) enclosure["@type"] = 'image/png'; - if (tmp.indexOf('.svg') >= 0) enclosure["@type"] = 'image/svg+xml'; - if (tmp.indexOf('.webp') >= 0) enclosure["@type"] = 'image/webp'; - } - item.enclosure = enclosure; + const fileExtension = post.cover.substring(post.cover.lastIndexOf('.')).toLowerCase(); + const mimeType = mimeTypes[fileExtension] || 'image/jpeg'; + item.enclosure = { + "@url": base + post.cover, + "@length": 15026, // dummy value, anything works + "@type": mimeType + }; } rss.channel.item.push(item) }