Skip to content

Commit 35767d3

Browse files
authored
fix(rss): ensure posts are sorted by date (#327)
* fix(rss): ensure posts are sorted by date * fix(formatting): oops
1 parent 64780b1 commit 35767d3

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/pages/news/rss.xml.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ import type { APIRoute } from "astro";
22
import rss from "@astrojs/rss";
33
import { getCollection, type CollectionEntry } from "astro:content";
44

5+
const getPostDate = (post: CollectionEntry<"posts">) => post.data.pubDate ?? post.data.date;
6+
57
export const GET: APIRoute = async ({ site }) => {
68
const posts = await getCollection("posts");
79

8-
const items = posts.map((post: CollectionEntry<"posts">) => {
9-
const data = post.data;
10-
const pubDate = data.pubDate ?? data.date ?? undefined;
10+
const items = posts
11+
.sort((a, b) => getPostDate(b).valueOf() - getPostDate(a).valueOf())
12+
.map((post) => {
13+
const data = post.data;
14+
const pubDate = getPostDate(post);
1115

12-
return {
13-
title: data.title,
14-
pubDate,
15-
link: `/news/${post.id}`,
16-
author: data.author,
17-
};
18-
});
16+
return {
17+
title: data.title,
18+
pubDate,
19+
link: `/news/${post.id}`,
20+
author: data.author,
21+
};
22+
});
1923

2024
return rss({
2125
title: "PaperMC News",

0 commit comments

Comments
 (0)