Skip to content

Commit

Permalink
Add RSS feed
Browse files Browse the repository at this point in the history
  • Loading branch information
FoggyMtnDrifter committed May 2, 2024
1 parent b22de65 commit 6fa2def
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
31 changes: 31 additions & 0 deletions app/feed.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import RSS from "rss";
import { getSortedPostsData } from "@/lib/news";

export async function GET() {
const feed = new RSS({
title: "Rocky Linux RSS Feed",
description: "Get all the latest news from the Rocky Linux project.",
site_url: "https://rockylinux.org",
feed_url: `https://rockylinux.org/feed.xml`,
copyright: `${new Date().getFullYear()} Rocky Enterprise Software Foundation`,
language: "en",
pubDate: new Date(),
});

const posts = await getSortedPostsData();

posts.forEach((post) => {
feed.item({
title: post.title,
description: post.excerpt,
url: `https://rockylinux.org/news/${post.slug}`,
date: new Date(post.date),
});
});

return new Response(feed.xml({ indent: true }), {
headers: {
"Content-Type": "application/atom+xml; charset=utf-8",
},
});
}
12 changes: 4 additions & 8 deletions lib/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import path from "path";
import matter from "gray-matter";
import { processMarkdownAsHTML } from "@/utils/remarkUtils";

import { Post } from "@/lib/types";

const postsDirectory = path.join(process.cwd(), "news");

export async function checkIfSlugIsValid(slug: string) {
if (!slug || typeof slug !== "string") {
return false;
}

// Check that the slug does not contain any slashes to prevent directory traversal
if (slug.includes("/") || slug.includes("\\")) {
return false;
Expand All @@ -26,7 +24,7 @@ export async function checkIfSlugIsValid(slug: string) {
}
}

export async function getSortedPostsData(numPosts?: number) {
export async function getSortedPostsData(numPosts?: number): Promise<Post[]> {
const fileNames = await fs.promises.readdir(postsDirectory);

const allPostsData = await Promise.all(
Expand All @@ -51,7 +49,7 @@ export async function getSortedPostsData(numPosts?: number) {
})
);

const sortedAndLimitedPostsData = allPostsData
return allPostsData
.sort((a, b) => {
if (a.date < b.date) {
return 1;
Expand All @@ -60,8 +58,6 @@ export async function getSortedPostsData(numPosts?: number) {
}
})
.slice(0, numPosts || allPostsData.length);

return sortedAndLimitedPostsData;
}

export async function getAllPostSlugs() {
Expand Down
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Post {
slug: string;
title: string;
date: string;
excerpt: string;
contentHtml?: string;
description?: string;
content?: string;
}
41 changes: 41 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 @@ -60,6 +60,7 @@
"remark-gfm": "^4.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0",
"rss": "^1.2.2",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7",
"typescript": "5.3.3",
Expand Down Expand Up @@ -90,6 +91,7 @@
"@testing-library/react": "^14.2.1",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.202",
"@types/rss": "^0.0.32",
"autoprefixer": "^10.4.17",
"eslint-plugin-storybook": "^0.6.15",
"husky": "^9.0.10",
Expand Down

0 comments on commit 6fa2def

Please sign in to comment.