Skip to content

Commit

Permalink
Add rss (#73)
Browse files Browse the repository at this point in the history
* Add rss

* Add rss URL to head
  • Loading branch information
zephraph authored Oct 4, 2024
1 parent e0ac9dc commit 338b5ed
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@astrojs/check": "^0.9.1",
"@astrojs/cloudflare": "^11.0.4",
"@astrojs/rss": "^4.0.7",
"@astrojs/tailwind": "^5.1.0",
"astro": "^4.13.1",
"hono": "^4.5.4",
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/layouts/base.astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const { title, description } = Astro.props;
{description && <meta name="description" content={description} />}
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<link
rel="alternate"
type="application/rss+xml"
title="Just Be"
href={new URL("rss.xml", Astro.site)}
/>
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<ViewTransitions />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/all.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import Layout from "../layouts/default.astro";
let { objects: pages } = await Astro.locals.runtime.env.R2_BUCKET.list({
const { objects: pages } = await Astro.locals.runtime.env.R2_BUCKET.list({
prefix: "0",
// @ts-expect-error This is actually supported but the types are wrong
include: ["customMetadata"],
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/[...path].ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const app = new Hono<{ Bindings: AstroContext }>()
const customMetadata: Record<string, string> = {
slug,
};
["stage", "title", "published", "updated"].forEach((key) => {
["stage", "title", "published", "updated", "tags"].forEach((key) => {
if (props[key]) {
customMetadata[key] = props[key];
}
Expand Down
35 changes: 35 additions & 0 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import rss, { type RSSFeedItem } from "@astrojs/rss";
import type { APIRoute } from "astro";

export const GET: APIRoute = async (context) => {
const { objects: pages } = await context.locals.runtime.env.R2_BUCKET.list({
prefix: "0",
// @ts-expect-error This is actually supported but the types are wrong
include: ["customMetadata"],
});

const site = context.locals.runtime.env.SITE ?? "https://just-be.dev";

const items: RSSFeedItem[] = pages
.filter(
(page) =>
page.customMetadata?.stage !== "draft" &&
page.customMetadata?.title &&
page.customMetadata?.published &&
page.customMetadata?.slug
)
.map((page) => ({
title: page.customMetadata?.title,
pubDate: page.customMetadata?.updated
? new Date(page.customMetadata?.updated)
: new Date(page.customMetadata?.published!),
link: `${site}/${page.customMetadata?.slug}`,
}));

return rss({
title: "Just Be",
description: "The personal site of Justin Bennett",
site,
items,
});
};

0 comments on commit 338b5ed

Please sign in to comment.