-
-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improved-image-Type-detection in build-rss.js #3359
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} | ||
|
||
function getAllPosts() { | ||
return require('../config/posts.json') | ||
} | ||
|
@@ -53,16 +70,11 @@ | |
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 = { | ||
"@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 | ||
}; | ||
Comment on lines
+73
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve enclosure object creation. Several improvements can be made to this section:
Consider this improvement: + const fullUrl = `${base}${post.cover}`;
const enclosure = {
- "@url": `${base}${post.cover}`,
+ "@url": fullUrl,
"@length": 15026, // Dummy value, replace with actual size if available
- "@type": getMimeType(`${base}${post.cover}`) // Pass the full URL to getMimeType
+ "@type": getMimeType(fullUrl)
}; Would you like help implementing a function to determine the actual file size of the images? This could involve:
🧰 Tools🪛 GitHub Check: codecov/patch[warning] 73-73: scripts/build-rss.js#L73 |
||
item.enclosure = enclosure; | ||
} | ||
rss.channel.item.push(item) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing 'path' module import.
The function uses
path.extname()
but thepath
module is not imported.Add this import at the top of the file:
const fs = require('fs') const json2xml = require('jgexml/json2xml') +const path = require('path')
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 17-18: scripts/build-rss.js#L17-L18
Added lines #L17 - L18 were not covered by tests
💡 Codebase verification
Missing path module import and validation needed in getMimeType function
path
module is used but not imported inscripts/build-rss.js
Recommended changes:
const path = require('path')
at the top of the filetests/build-rss.test.js
covering:🔗 Analysis chain
Add input validation and tests for getMimeType function.
The function needs defensive programming and test coverage:
Would you like me to help with:
Let's verify the current usage patterns:
Let me gather more context about the usage and implementation to provide a comprehensive verification.
Let me check the test configuration and examine if there are any existing test utilities that could be helpful for testing the getMimeType function.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 189
Script:
Length of output: 3157
Script:
Length of output: 1365
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 17-18: scripts/build-rss.js#L17-L18
Added lines #L17 - L18 were not covered by tests