-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create rss feed generator * create rss feed before build
- Loading branch information
1 parent
166440b
commit 0006080
Showing
4 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
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,49 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const matter = require("gray-matter"); | ||
const { format } = require("date-fns"); | ||
|
||
const xmlFormat = require("xml-formatter"); | ||
|
||
const BLOGS_DIR = path.join(process.cwd(), "_blogs"); | ||
const OUTPUT_FILE = path.join(process.cwd(), "public", "rss.xml"); | ||
|
||
function escapeXML(str) { | ||
return str | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/'/g, "'") | ||
.replace(/"/g, """); | ||
} | ||
|
||
function generateRSS() { | ||
const files = fs.readdirSync(BLOGS_DIR); | ||
|
||
const posts = files.map((file) => { | ||
const filePath = path.join(BLOGS_DIR, file); | ||
const content = fs.readFileSync(filePath, "utf-8"); | ||
const { data } = matter(content); | ||
return { | ||
title: data.title, | ||
description: data.excerpt, | ||
link: `https://umairjibran.com/blogs/${file.replace(/\.md$/, "")}`, | ||
pubDate: data.date, | ||
}; | ||
}); | ||
|
||
const rssItems = posts | ||
.map( | ||
(post) => | ||
`<item><title>${escapeXML(post.title)}</title><link>${escapeXML(post.link)}</link><description>${escapeXML(post.description)}</description><pubDate>${new Date(post.pubDate).toUTCString()}</pubDate><guid>${post.link}</guid></item>`, | ||
) | ||
.join(""); | ||
|
||
const rssFeed = | ||
`<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Umair Jibran's Blog</title><link>https://umairjibran.com/blogs</link><description>Latest articles from Umair Jibran's blog.</description><atom:link rel="self" href="https://umairjibran.com/rss.xml" /><language>en-us</language><lastBuildDate>${new Date().toUTCString()}</lastBuildDate>${rssItems}</channel></rss>`.trim(); | ||
|
||
fs.writeFileSync(OUTPUT_FILE, xmlFormat(rssFeed), "utf-8"); | ||
console.log(`RSS feed generated at ${OUTPUT_FILE}`); | ||
} | ||
|
||
generateRSS(); |
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