Skip to content

Commit

Permalink
Add RSS feed
Browse files Browse the repository at this point in the history
Add a link tag to the page metadata to make the RSS feed discoverable.
Add @astrojs/rss package.
  • Loading branch information
fwextensions committed Oct 11, 2024
1 parent 60da3b2 commit 4a96aaa
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
5 changes: 1 addition & 4 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { defineConfig } from "astro/config";
import icon from "astro-icon";

export default defineConfig({
site: "https://www.sfcivictech.us",
// site: "https://www.sfcivictech.org",
site: "https://www.sfcivictech.org",
base: "",
// site: "https://sfbrigade.github.io",
// base: "/astro-static-site/",
trailingSlash: "ignore",
compressHTML: false,
integrations: [
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@astrojs/check": "^0.8.3",
"@astrojs/rss": "^4.0.8",
"@iconify-json/fa": "^1.2.0",
"@picocss/pico": "^2.0.6",
"@stylistic/eslint-plugin": "^2.8.0",
Expand Down
7 changes: 7 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const blog = defineCollection({
description: z.string(),
date: z.string()
.transform((value, ctx) => {
// we treat the date as a string instead of using the built-in date parsing
// so we can force it to be in PST, rather than the default UTC
const date = parseYMD(value);

if (!date.isValid()) {
Expand All @@ -24,6 +26,11 @@ const blog = defineCollection({
message: "Invalid date",
});

// annoyingly, nothing about the file being validated here is available
// to this function. if we could get the filename, we could pull a
// missing date out of the name. given that we can't, we have to return
// undefined here and make any code that iterates over the collection
// call dateFromSlug(slug) for undefined dates, like rss.xml.js.
return z.NEVER;
}

Expand Down
6 changes: 6 additions & 0 deletions src/layouts/Page.astro
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const baseURL = import.meta.env.BASE_URL.replace(/([^/])$/, "$1/");
<link rel="mask-icon" href={base("safari-pinned-tab.svg")} color="#5bbad5" />
<meta name="msapplication-TileColor" content="#da532c" />
<meta name="theme-color" content="#ffffff" />
<link
rel="alternate"
type="application/rss+xml"
title="SF Civic Tech"
href={new URL("rss.xml", Astro.site)}
/>

<Metadata meta={meta} />

Expand Down
35 changes: 35 additions & 0 deletions src/pages/rss.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { dateFromSlug, formatRSSDate } from "@/utils";

export async function GET(context)
{
const blog = await getCollection("blog");
const items = blog.map((post) => {
const {
slug,
data: {
title,
descriptionHTML,
// pubDate is required, but some posts don't have a date specified, so
// create one from the filename
date = dateFromSlug(slug),
},
} = post;

return {
title,
description: descriptionHTML,
pubDate: formatRSSDate(date),
link: `/blog/${slug}`,
};
});

return rss({
title: "SF Civic Tech Blog",
description: "News from SF Civic Tech",
site: context.site,
trailingSlash: false,
items,
});
}
1 change: 1 addition & 0 deletions src/utils/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export default dayjs;
export const parseYMD = (date: string) => dayjs(date, "YYYY-MM-DD", true);
export const parseYMDToDate = (date: string) => parseYMD(date).toDate();
export const formatShortMDY = (date: Date) => date ? dayjs(date).format("MMM D, YYYY") : "";
export const formatRSSDate = (date: Date) => date ? dayjs(date).format("ddd, D MMM YYYY HH:mm:ss ZZ") : "";

0 comments on commit 4a96aaa

Please sign in to comment.