Skip to content

Commit

Permalink
rss: add simple rss feed
Browse files Browse the repository at this point in the history
rss: add tests to check against generated XML
  • Loading branch information
samfry13 authored and ADKaster committed Jul 24, 2024
1 parent 44aae87 commit 0e59e86
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.13",
"@types/bun": "latest",
"fast-xml-parser": "^4.4.0",
"kleur": "^4.1.5",
"prettier": "^3.3.3",
"prettier-plugin-astro": "^0.14.1"
Expand All @@ -25,6 +26,7 @@
},
"dependencies": {
"@astrojs/mdx": "3.1.3",
"@astrojs/rss": "^4.0.7",
"@astrojs/sitemap": "^3.1.6",
"@astrojs/tailwind": "^5.1.0",
"@astrojs/ts-plugin": "1.9.0",
Expand Down
7 changes: 7 additions & 0 deletions src/components/global/head.astro
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ const { title, description, author, image } = Astro.props;
<title>{title !== undefined ? title : "Ladybird"}</title>
<link rel="stylesheet" href="/assets/fonts/stylesheet.css" />
<link rel="icon" type="image/x-icon" href="/assets/img/ladybird_favicon.png" />

<link
rel="alternate"
type="application/rss+xml"
title="Ladybird Browser Posts"
href={new URL("posts.xml", Astro.site)}
/>
20 changes: 20 additions & 0 deletions src/pages/posts.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import type { APIContext } from "astro";

export async function GET(context: APIContext) {
const posts = await getCollection("posts");
return rss({
title: "Ladybird Browser Posts",
description: "Ladybird is a brand-new browser &amp; web engine",
site: context.site!,
items: posts.map((post) => ({
title: post.data.title,
description: post.data.description,
author: post.data.author,
pubDate: post.data.date,
link: `/posts/${post.slug}`,
})),
trailingSlash: false,
});
}
45 changes: 45 additions & 0 deletions tests/rss.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { test, expect, describe } from "bun:test";
import { XMLParser } from "fast-xml-parser";
import fs from "fs";
import path from "path";

const rootDir: string = path.join(__dirname, "../");

describe("RSS Feeds", () => {
const srcDir = path.join(rootDir, "/src/content/posts");
const distDir = path.join(rootDir, "/dist/");

const mdFiles = fs
.readdirSync(srcDir)
.filter((file) => file.endsWith(".mdx"));
const xmlFile = fs.readFileSync(path.join(distDir, "posts.xml"));

const parser = new XMLParser();
const parsedXML = parser.parse(xmlFile);

test("RSS Channel includes overall metadata", () => {
expect(parsedXML.rss.channel).toEqual(
expect.objectContaining({
title: "Ladybird Browser Posts",
description: "Ladybird is a brand-new browser &amp; web engine",
link: "https://ladybird.org",
})
);
});

test("XML should contain entries for every post", async () => {
expect(parsedXML.rss.channel.item).toBeArrayOfSize(mdFiles.length);
parsedXML.rss.channel.item.forEach((item: any) => {
const itemAttributes = Object.keys(item);
expect(itemAttributes).toEqual(
expect.arrayContaining([
"title",
"link",
"description",
"pubDate",
"author",
])
);
});
});
});

0 comments on commit 0e59e86

Please sign in to comment.