Skip to content

Commit

Permalink
Merge pull request #546 from unitaryfund/blog-rss
Browse files Browse the repository at this point in the history
Add an RSS feed for blog posts
  • Loading branch information
cosenal authored Sep 20, 2024
2 parents e02c121 + afbc305 commit ecaee2d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@actions/github": "^5.1.1",
"@astrojs/mdx": "^0.19.7",
"@astrojs/react": "2.2.1",
"@astrojs/rss": "^4.0.7",
"@astrojs/tailwind": "3.1.3",
"@cloudinary/url-gen": "^1.10.0",
"@emotion/css": "^11.11.0",
Expand All @@ -36,6 +37,7 @@
"html-react-parser": "^3.0.16",
"iso-3166": "^4.2.0",
"lucide-react": "^0.220.0",
"markdown-it": "^14.1.0",
"markdown-to-jsx": "^7.2.1",
"merge-refs": "^1.2.1",
"nanostores": "^0.8.1",
Expand All @@ -49,6 +51,7 @@
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"remark-oembed": "^1.2.2",
"sanitize-html": "^2.13.0",
"swiper": "^9.3.2",
"tailwind-merge": "^1.12.0",
"tailwind-scrollbar-hide": "^1.1.7",
Expand Down
83 changes: 83 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ if (title) {
<link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png" />
<link rel="alternate" type="application/rss+xml" title={titleName} href={`${Astro.url.origin}/posts/feed.xml`} />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
Expand Down
35 changes: 35 additions & 0 deletions src/pages/posts/feed.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import rss from '@astrojs/rss';
import { CollectionEntry, getCollection } from 'astro:content';
import sanitizeHtml from 'sanitize-html';
import MarkdownIt from 'markdown-it';

interface SiteContext {
site: string;
}

const parser = new MarkdownIt();

function getDateFromPost(post: CollectionEntry<"blog">): Date {
return new Date(`${post.data.year}-${post.data.month}-${post.data.day}`);
}

export async function get(context: SiteContext) {
const posts = await getCollection('blog');
// Sort the posts by date in descending order
const sortedPosts = posts.sort((a, b) => {
return getDateFromPost(b).getTime() - getDateFromPost(a).getTime();
});
return rss({
title: 'Unitary Fund blog',
description: 'Unitary Fund blog',
site: context.site,
items: sortedPosts
.map((post) => ({
title: post.data.title,
link: `/posts/${post.slug}/`,
pubDate: getDateFromPost(post),
content: sanitizeHtml(parser.render(post.body)),
})),
customData: `<language>en-us</language>`,
});
}

0 comments on commit ecaee2d

Please sign in to comment.