Skip to content

Commit

Permalink
Add RSS feed functionality and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
matttrent committed Dec 14, 2024
1 parent b6da37d commit 0688aa5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
45 changes: 42 additions & 3 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 @@ -12,6 +12,7 @@
"dependencies": {
"@astrojs/check": "^0.9.4",
"@astrojs/mdx": "^3.1.8",
"@astrojs/rss": "^4.0.10",
"@astrojs/sitemap": "^3.2.1",
"astro": "^4.16.7",
"typescript": "^5.6.3"
Expand Down
6 changes: 6 additions & 0 deletions src/components/HTMLHeader.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const { title } = Astro.props;
<title>{title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link
rel="alternate"
type="application/rss+xml"
title="matttrent.com notes RSS feed"
href="/rss.xml"
/>
<link rel="sitemap" href="/sitemap-index.xml" />
<meta name="generator" content={Astro.generator} />
<slot name="head" />
Expand Down
41 changes: 41 additions & 0 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import { getContainerRenderer as getMDXRenderer } from "@astrojs/mdx";
import { loadRenderers } from "astro:container";


type Note = CollectionEntry<"notes">;


export async function GET(context) {

const renderers = await loadRenderers([getMDXRenderer()]);
const container = await AstroContainer.create({ renderers });

const noteEntries = await getCollection("notes", ({ data }: Note) => {
return import.meta.env.PROD ? data.isDraft !== true : true;
});

const items = [];
for (const note of noteEntries) {

const { Content } = await note.render();
const content = await container.renderToString(Content);
items.push({
title: note.data.title,
pubDate: note.data.createdAt,
link: `/notes/${note.slug}`,
content: content
});
}

return rss({
title: "matttrent.com notes feed",
description: "matttrent.com notes feed",
site: context.site,
trailingSlash: false,
items: items
});
}

0 comments on commit 0688aa5

Please sign in to comment.