Skip to content

Commit

Permalink
Merge pull request #12 from santhosh-chinnasamy/feat/rss-feed
Browse files Browse the repository at this point in the history
Add RSS feed for Blog
  • Loading branch information
helloanoop authored Oct 21, 2023
2 parents ac74eec + 6de7eb2 commit f80cd5b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ yarn-error.log*

# vercel
.vercel

rss.xml
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"prebuild": "node scripts/generate-rss.js",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@tabler/icons": "^1.100.0",
"@vercel/analytics": "^1.1.0",
"feed": "^4.2.2",
"gray-matter": "^4.0.3",
"marked": "^4.3.0",
"postcss": "^8.4.16",
Expand Down
60 changes: 60 additions & 0 deletions scripts/generate-rss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require("fs");
const path = require("path");
const { Feed } = require("feed");
const matter = require("gray-matter");
const { loadEnvConfig } = require("@next/env");

loadEnvConfig(process.cwd());

const getPostsSortByDate = () => {
const files = fs.readdirSync(path.join("posts"));
let posts = files.map((filename) => {
const markdownWithMeta = fs.readFileSync(
path.join("posts", filename),
"utf-8"
);

const { data } = matter(markdownWithMeta);
data.slug = filename.replace(".md", "");
return data;
});

return sortPostsInDesc(posts);
};

const sortPostsInDesc = (posts) => {
return posts?.sort(
(first, next) => new Date(next.date) - new Date(first.date)
);
};

const generateRSSFeed = () => {
const siteUrl = process.env.NEXT_PUBLIC_VERCEL_URL;
const posts = getPostsSortByDate();
const feed = new Feed({
title: "Bruno Blog | RSS Feed",
description: "RSS Feed for Bruno Blog",
id: siteUrl,
link: siteUrl,
image: `${siteUrl}/favicon-32x32.png`,
favicon: `${siteUrl}/favicon.ico`,
copyright: "Anoop M D and Contributors",
feedLinks: {
rss2: `${siteUrl}/feed.xml`,
},
});

posts.forEach((post) => {
feed.addItem({
title: post.title,
id: `${siteUrl}/blog/${post.slug}`,
link: `${siteUrl}/blog/${post.slug}`,
description: post.description,
date: new Date(post.date),
});
fs.writeFileSync("./public/rss.xml", feed.rss2());
});
console.log("RSS Feed generated!");
};

generateRSSFeed();

1 comment on commit f80cd5b

@vercel
Copy link

@vercel vercel bot commented on f80cd5b Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.