Skip to content

Commit

Permalink
Add rss feed (#119)
Browse files Browse the repository at this point in the history
* create rss feed generator

* create rss feed before build
  • Loading branch information
UmairJibran authored Jan 7, 2025
1 parent 166440b commit 0006080
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
22 changes: 21 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"prebuild": "node scripts/generate-rss.js",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand All @@ -23,7 +24,8 @@
"remark-html": "^16.0.1",
"sass": "^1.77.8",
"sharp": "^0.33.4",
"swr": "^2.2.5"
"swr": "^2.2.5",
"xml-formatter": "^3.6.3"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
49 changes: 49 additions & 0 deletions scripts/generate-rss.js
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, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&apos;")
.replace(/"/g, "&quot;");
}

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();
5 changes: 5 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const inconsolata = Inconsolata({
export const metadata: Metadata = {
...meta,
metadataBase: new URL(meta.metadataBase),
alternates: {
types: {
"application/rss+xml": "/rss.xml",
},
},
};

export default function RootLayout({
Expand Down

0 comments on commit 0006080

Please sign in to comment.