From d55f55313c5e603df69fb90c369a5ad4911245b0 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Mon, 7 Oct 2024 15:56:22 +0530 Subject: [PATCH] suggested changes applied --- scripts/build-rss.js | 136 +++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 62 deletions(-) diff --git a/scripts/build-rss.js b/scripts/build-rss.js index 05162769d72f..47ba14f4ffeb 100644 --- a/scripts/build-rss.js +++ b/scripts/build-rss.js @@ -1,4 +1,4 @@ -const fs = require('fs') +const fs = require('fs').promises const json2xml = require('jgexml/json2xml') function getAllPosts() { @@ -15,73 +15,85 @@ function clean(s) { return s } -module.exports = function rssFeed(type, title, desc, outputPath) { - return new Promise((resolve, reject) => { - try { +module.exports = async function rssFeed(type, title, desc, outputPath) { + try { - const posts = getAllPosts()[`${type}`] - .sort((i1, i2) => { - if (!i1.date || !i2.date) { - throw new Error('Missing date in post data'); - } - const i1Date = new Date(i1.date) - const i2Date = new Date(i2.date) - if (i1.featured && !i2.featured) return -1 - if (!i1.featured && i2.featured) return 1 - return i2Date - i1Date - }) + let posts = getAllPosts()[`${type}`] + posts = posts.filter(post => { + if (!post.date) { + return false; + } + return true; + }); + posts.sort((i1, i2) => { + const i1Date = new Date(i1.date) + const i2Date = new Date(i2.date) + if (i1.featured && !i2.featured) return -1 + if (!i1.featured && i2.featured) return 1 + return i2Date - i1Date + }) - const base = 'https://www.asyncapi.com' - const tracking = '?utm_source=rss'; + const base = 'https://www.asyncapi.com' + const tracking = '?utm_source=rss'; - const feed = {} - const rss = {} - rss['@version'] = '2.0' - rss["@xmlns:atom"] = 'http://www.w3.org/2005/Atom' - rss.channel = {} - rss.channel.title = title - rss.channel.link = `${base}/${outputPath}` - rss.channel["atom:link"] = {} - rss.channel["atom:link"]["@rel"] = 'self' - rss.channel["atom:link"]["@href"] = rss.channel.link - rss.channel["atom:link"]["@type"] = 'application/rss+xml' - rss.channel.description = desc - rss.channel.language = 'en-gb'; - rss.channel.copyright = 'Made with :love: by the AsyncAPI Initiative.'; - rss.channel.webMaster = 'info@asyncapi.io (AsyncAPI Initiative)' - rss.channel.pubDate = new Date().toUTCString() - rss.channel.generator = 'next.js' - rss.channel.item = [] + const feed = {} + const rss = {} + rss['@version'] = '2.0' + rss["@xmlns:atom"] = 'http://www.w3.org/2005/Atom' + rss.channel = {} + rss.channel.title = title + rss.channel.link = `${base}/${outputPath}` + rss.channel["atom:link"] = {} + rss.channel["atom:link"]["@rel"] = 'self' + rss.channel["atom:link"]["@href"] = rss.channel.link + rss.channel["atom:link"]["@type"] = 'application/rss+xml' + rss.channel.description = desc + rss.channel.language = 'en-gb'; + rss.channel.copyright = 'Made with :love: by the AsyncAPI Initiative.'; + rss.channel.webMaster = 'info@asyncapi.io (AsyncAPI Initiative)' + rss.channel.pubDate = new Date().toUTCString() + rss.channel.generator = 'next.js' + rss.channel.item = [] - for (let post of posts) { - if (!post.title || !post.slug || !post.excerpt || !post.date) { - throw new Error('Missing required fields in post data'); - } - 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'; - } - item.enclosure = enclosure; + for (let post of posts) { + if (!post.title || !post.slug || !post.excerpt || !post.date) { + throw new Error('Missing required fields in post data'); + } + const link = `${base}${post.slug}${tracking}`; + const { title, excerpt, date } = post; + const pubDate = new Date(date).toUTCString(); + const description = clean(excerpt); + const guid = { '@isPermaLink': true, '': link }; + const item = { + title, + description, + link, + category: type, + guid, + 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'; } - rss.channel.item.push(item) + item.enclosure = enclosure; } + rss.channel.item.push(item) + } - feed.rss = rss + feed.rss = rss - const xml = json2xml.getXml(feed, '@', '', 2); - fs.writeFileSync(`./public/${outputPath}`, xml, 'utf8') - resolve(`RSS feed generated successfully at ${outputPath}`); - } catch (err) { - reject(new Error(`Failed to generate RSS feed: ${err.message}`)); - } - }); + const xml = json2xml.getXml(feed, '@', '', 2); + await fs.writeFile(`./public/${outputPath}`, xml, 'utf8'); + return `RSS feed generated successfully at ${outputPath}`; + } catch (err) { + throw new Error(`Failed to generate RSS feed: ${err.message}`); + } };