Skip to content

Commit

Permalink
feat: added support for additional image formats in build-rss.js (#3396)
Browse files Browse the repository at this point in the history
Co-authored-by: Ansh Goyal <[email protected]>
  • Loading branch information
SahilDahekar and anshgoyalevil authored Dec 23, 2024
1 parent 3afcf6a commit 2199c3b
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions scripts/build-rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
}
Expand Down

0 comments on commit 2199c3b

Please sign in to comment.