Skip to content

Commit

Permalink
Added support for image formats
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilDahekar committed Nov 14, 2024
1 parent 6f15381 commit 4934f57
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 4934f57

Please sign in to comment.