Skip to content

Commit

Permalink
feat: improved-image-detection in build-rss.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshaydewan committed Nov 1, 2024
1 parent 551c47a commit b839277
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions scripts/build-rss.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const fs = require('fs')
const json2xml = require('jgexml/json2xml')

const MIME_TYPES = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.webp': 'image/webp',
'.gif': 'image/gif',
'.bmp': 'image/bmp',
'.tiff': 'image/tiff',
'.tif': 'image/tiff'
};

function getMimeType(url) {
const ext = path.extname(url).toLowerCase();
return MIME_TYPES[ext] || 'application/octet-stream'; // Fallback MIME type

Check warning on line 18 in scripts/build-rss.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-rss.js#L17-L18

Added lines #L17 - L18 were not covered by tests
}

function getAllPosts() {
return require('../config/posts.json')
}
Expand Down Expand Up @@ -53,16 +70,11 @@ module.exports = function rssFeed(type, title, desc, outputPath) {
const link = `${base}${post.slug}${tracking}`;
const item = { title: post.title, description: clean(post.excerpt), link, category: type, guid: { '@isPermaLink': true, '': link }, pubDate: new Date(post.date).toUTCString() }
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';
}
const enclosure = {

Check warning on line 73 in scripts/build-rss.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-rss.js#L73

Added line #L73 was not covered by tests
"@url": `${base}${post.cover}`,
"@length": 15026, // Dummy value, replace with actual size if available
"@type": getMimeType(`${base}${post.cover}`) // Pass the full URL to getMimeType
};
item.enclosure = enclosure;
}
rss.channel.item.push(item)
Expand Down

0 comments on commit b839277

Please sign in to comment.