-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from unitaryfund/blog-rss
Add an RSS feed for blog posts
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`, | ||
}); | ||
} |