-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
487e44c
commit 6de7eb2
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ yarn-error.log* | |
|
||
# vercel | ||
.vercel | ||
|
||
rss.xml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |