Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(website): add rss feed #1159

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"scripts": {
"dev": "waku dev",
"build": "waku build",
"build": "VITE_EXPERIMENTAL_WAKU_ROUTER=true waku build",
"start": "waku start"
},
"dependencies": {
Expand Down
86 changes: 86 additions & 0 deletions packages/website/src/pages/api/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { compileMDX } from 'next-mdx-remote/rsc';
import { readdirSync, readFileSync } from 'node:fs';

type Item = {
title: string;
link: string;
description: string;
pubDate: string;
};

const generateRSSFeed = (items: Item[]) => {
const itemsXML = items
.map(
(item) => `
<item>
<title>${item.title}</title>
<link>${item.link}</link>
<pubDate>${item.pubDate}</pubDate>
<description>${item.description}</description>
<guid isPermaLink="true">${item.link}</guid>
</item>`,
)
.join('');

return `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="https://waku.gg/api/rss.xml" rel="self" type="application/rss+xml" />
<title>Waku</title>
<link>https://waku.gg</link>
<description>The minimal React framework</description>
${itemsXML}
</channel>
</rss>`;
};

export const GET = async () => {
const blogFileNames: Array<string> = [];
readdirSync('./private/contents').forEach((fileName) => {
if (fileName.endsWith('.mdx')) {
blogFileNames.push(fileName);
}
});

const items: Item[] = [];

for await (const fileName of blogFileNames) {
const path = `./private/contents/${fileName}`;
const source = readFileSync(path, 'utf8');
const mdx = await compileMDX({
source,
options: { parseFrontmatter: true },
});

const frontmatter = mdx.frontmatter as {
title: string;
description: string;
slug: string;
date: string;
};
const [year, month, day] = frontmatter.date.split('/').map(Number) as [
number,
number,
number,
];
items.push({
title: frontmatter.title,
link: `https://waku.gg/blog/${frontmatter.slug}`,
description: frontmatter.description || '',
pubDate: new Date(Date.UTC(year, month - 1, day)).toUTCString(),
});
}

const rssFeed = generateRSSFeed(items);
return new Response(rssFeed, {
headers: {
'Content-Type': 'application/rss+xml',
},
});
};

export const getConfig = async () => {
return {
render: 'static',
} as const;
};
Loading