Skip to content

Commit

Permalink
Merge pull request #5 from gBasil/feeds
Browse files Browse the repository at this point in the history
Add RSS, Atom, and JSON feeds
  • Loading branch information
melody-rs authored Jul 24, 2024
2 parents 9009781 + 9a02d1d commit ab7755c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@types/intrinsic-scale": "^3.0.3",
"dompurify": "^3.1.5",
"feed": "^4.2.2",
"intrinsic-scale": "^3.0.4",
"lucide-react": "^0.364.0",
"marked": "^13.0.2",
Expand Down
6 changes: 5 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata, Viewport } from "next";
import { Ubuntu } from "next/font/google";
import { WEBSITE_ROOT } from '@/consts';
import "@/styles/globals.scss";

import Background from "@/components/Background";
Expand All @@ -16,7 +17,7 @@ export const metadata: Metadata = {
description: "An RPG Maker XP-VX Ace rewrite, written in Rust with love 💕",
openGraph: {
type: "website",
images: [{ url: "https://luminol.dev/header.jpg" }],
images: [{ url: `${WEBSITE_ROOT}/header.jpg` }],
},
};

Expand All @@ -29,6 +30,9 @@ export default function RootLayout({
<html lang="en">
<head>
<link rel="shortcut icon" href="favicon.png" type="image/png" />
<link rel="alternate" type="application/rss+xml" href="/news/feed/rss" title="Luminol News (RSS)" />
<link rel="alternate" type="application/atom+xml" href="/news/feed/atom" title="Luminol News (Atom)" />
<link rel="alternate" type="application/feed+json" href="/news/feed/json" title="Luminol News (JSON)" />
</head>
<body className={font.className}>
<Background className="bg" />
Expand Down
70 changes: 70 additions & 0 deletions src/app/news/feed/[format]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Feed } from "feed";
import { marked } from "marked";
import markedAlert from "marked-alert";
import markedFootnote from "marked-footnote";
import { NEWS_ROOT, WEBSITE_ROOT } from "@/consts";

type NewsIndex = Record<string, {
title: string;
thumbnail: string;
timestamp: number;
tags: string[];
}>;

export async function GET(
_: Request,
{ params }: { params: { format: string } }
) {
const feed = new Feed({
title: "Luminol News",
description: "Luminol News",
id: `${WEBSITE_ROOT}/news`,
link: `${WEBSITE_ROOT}/news`,
language: "en",
favicon: `${WEBSITE_ROOT}/logo-icon.png`,
copyright: `Copyright ${new Date().getFullYear()} The Luminol Newswriters`,
});

const indexRes = await fetch(`${NEWS_ROOT}/index.json`);
const index: NewsIndex = await indexRes.json();

const posts = await Promise.all(
Object.entries(index)
.map(async ([file, meta]) => {
const fileRes = await fetch(`${NEWS_ROOT}/posts/${file}`);
const fileText = await fileRes.text();

const html = `<img src="${meta.thumbnail}" alt="" />` + marked
.use(markedFootnote())
.use(markedAlert())
.parse(fileText, { async: false }) as string;

return { file, html, meta };
})
);

posts.forEach(post => {
const link = `${WEBSITE_ROOT}/news/view?p=${post.file}`;

feed.addItem({
title: post.meta.title,
id: link,
link,
category: post.meta.tags.map(name => ({ name })),
content: post.html,
date: new Date(post.meta.timestamp),
});
});

if (params.format === "rss") return new Response(feed.rss2());
if (params.format === "atom") return new Response(feed.atom1());
if (params.format === "json") return new Response(feed.json1());
}

export function generateStaticParams() {
return [
{ format: "rss" },
{ format: "atom" },
{ format: "json" },
]
}
1 change: 1 addition & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const WEBSITE_ROOT = "https://luminol.dev";
export const NEWS_ROOT = "https://raw.githubusercontent.com/Astrabit-ST/luminol-news/main";

export type PostEntry = {
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,13 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.4"

feed@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/feed/-/feed-4.2.2.tgz#865783ef6ed12579e2c44bbef3c9113bc4956a7e"
integrity sha512-u5/sxGfiMfZNtJ3OvQpXcvotFpYkL0n9u9mM2vkui2nGo8b4wvDkJ8gAkYqbA8QpGyFCv3RK0Z+Iv+9veCS9bQ==
dependencies:
xml-js "^1.6.11"

file-entry-cache@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027"
Expand Down Expand Up @@ -2093,6 +2100,11 @@ sass@^1.72.0:
immutable "^4.0.0"
source-map-js ">=0.6.2 <2.0.0"

sax@^1.2.4:
version "1.4.1"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.4.1.tgz#44cc8988377f126304d3b3fc1010c733b929ef0f"
integrity sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==

scheduler@^0.23.0:
version "0.23.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.0.tgz#ba8041afc3d30eb206a487b6b384002e4e61fdfe"
Expand Down Expand Up @@ -2499,6 +2511,13 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

xml-js@^1.6.11:
version "1.6.11"
resolved "https://registry.yarnpkg.com/xml-js/-/xml-js-1.6.11.tgz#927d2f6947f7f1c19a316dd8eea3614e8b18f8e9"
integrity sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==
dependencies:
sax "^1.2.4"

yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
Expand Down

0 comments on commit ab7755c

Please sign in to comment.