Skip to content

Commit 30179e1

Browse files
committed
refactor: add regular and short feed
Signed-off-by: Sefa Eyeoglu <[email protected]>
1 parent c764559 commit 30179e1

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/pages/feed/feed.xml.ts renamed to src/pages/feed/[feedName].xml.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import rehypeRaw from "rehype-raw";
55
import rehypeStringify from "rehype-stringify";
66
import { getCollection, type CollectionEntry } from "astro:content";
77

8+
interface Post {
9+
title: string;
10+
url: string;
11+
date: string;
12+
description: string;
13+
content: string;
14+
}
15+
816
const FEED_CONFIG = {
917
title: "Prism Launcher",
1018
subtitle:
@@ -24,7 +32,10 @@ const escapeXml = (text: string): string =>
2432
.replace(/>/g, "&gt;")
2533
.replace(/"/g, "&quot;");
2634

27-
async function processPost(post: CollectionEntry<"news">, siteUrl: string) {
35+
async function processPost(
36+
post: CollectionEntry<"news">,
37+
siteUrl: string,
38+
): Promise<Post> {
2839
const slug = post.data.slug || post.slug;
2940

3041
try {
@@ -37,6 +48,7 @@ async function processPost(post: CollectionEntry<"news">, siteUrl: string) {
3748
title: post.data.title,
3849
url: `${siteUrl}news/${slug}/`,
3950
date: post.data.date.toISOString(),
51+
description: post.data.description,
4052
content,
4153
};
4254
} catch (error) {
@@ -45,15 +57,17 @@ async function processPost(post: CollectionEntry<"news">, siteUrl: string) {
4557
title: post.data.title,
4658
url: `${siteUrl}news/${slug}/`,
4759
date: post.data.date.toISOString(),
60+
description: post.data.description || "No description available",
4861
content: `<p>${escapeXml(post.data.description || "No description available")}</p>`,
4962
};
5063
}
5164
}
5265

5366
function generateFeed(
54-
entries: Array<{ title: string; url: string; date: string; content: string }>,
67+
entries: Post[],
5568
siteUrl: string,
5669
updated: string,
70+
short: boolean,
5771
): string {
5872
return `<?xml version="1.0" encoding="utf-8"?>
5973
<feed xmlns="http://www.w3.org/2005/Atom">
@@ -68,20 +82,28 @@ function generateFeed(
6882
<email>${FEED_CONFIG.email}</email>
6983
</author>
7084
${entries
71-
.map(
72-
(entry) => ` <entry>
85+
.map((entry) => {
86+
let content = short
87+
? `<content type="string">${escapeXml(entry.description)}</content>`
88+
: `<content type="html">${escapeXml(entry.content)}</content>`;
89+
return ` <entry>
7390
<title>${escapeXml(entry.title)}</title>
7491
<link href="${entry.url}"/>
7592
<updated>${entry.date}</updated>
7693
<id>${entry.url}</id>
77-
<content type="html">${escapeXml(entry.content)}</content>
78-
</entry>`,
79-
)
94+
${content}
95+
</entry>`;
96+
})
8097
.join("\n")}
8198
</feed>`;
8299
}
83100

84-
export const GET: APIRoute = async ({ site }) => {
101+
export const GET: APIRoute = async ({ site, params: { feedName } }) => {
102+
if (feedName !== "feed" && feedName !== "short") {
103+
return new Response(null, {
104+
status: 404,
105+
});
106+
}
85107
try {
86108
const posts = (await getCollection("news", ({ data }) => !data.draft)).sort(
87109
(a, b) => b.data.date.getTime() - a.data.date.getTime(),
@@ -95,6 +117,7 @@ export const GET: APIRoute = async ({ site }) => {
95117
await Promise.all(posts.map((post) => processPost(post, siteUrl))),
96118
siteUrl,
97119
(posts[0]?.data.date || new Date()).toISOString(),
120+
feedName === "short",
98121
),
99122
{
100123
headers: {
@@ -116,3 +139,7 @@ export const GET: APIRoute = async ({ site }) => {
116139
);
117140
}
118141
};
142+
143+
export const getStaticPaths = () => {
144+
return [{ params: { feedName: "feed" } }, { params: { feedName: "short" } }];
145+
};

0 commit comments

Comments
 (0)