-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f7e66b
commit 90b24e1
Showing
6 changed files
with
102 additions
and
49 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
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
|
||
import type { APIRoute } from "astro"; | ||
import { getCollection } from "astro:content"; | ||
|
||
import rss, { type RSSFeedItem } from "@astrojs/rss"; | ||
import sanitizeHtml from "sanitize-html"; | ||
import MarkdownIt from "markdown-it"; | ||
const parser = new MarkdownIt(); | ||
|
||
import { | ||
BLOG_TITLE, | ||
BLOG_DESCRIPTION, | ||
EMAIL_ADDRESS, | ||
FULL_NAME, | ||
} from "lib/consts"; | ||
import { parseDateFromFilePath } from "lib/blog/utils"; | ||
|
||
const BLOG_CUSTOM_DATA = ` | ||
<atom:link href="${import.meta.env.SITE}/rss.xml" rel="self" type="application/rss+xml" /> | ||
<category>Technology</category> | ||
<copyright>Copyright ${new Date().getUTCFullYear()} Rayhan Noufal Arayilakath</copyright> | ||
<docs>https://www.rssboard.org/rss-specification</docs> | ||
<generator>@astrojs/rss</generator> | ||
<image> | ||
<link>https://www.rayhanadev.com/</link> | ||
<title>RAYHANADEV</title> | ||
<url>https://www.rayhanadev.com/favicon-32x32.png</url> | ||
<description>Various pieces written and composed by Ray, related to software engineering and life.</description> | ||
<height>32</height> | ||
<width>32</width> | ||
</image> | ||
<language>en-US</language> | ||
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate> | ||
<managingEditor>${EMAIL_ADDRESS} (${FULL_NAME})</managingEditor> | ||
<pubDate>${new Date().toUTCString()}</pubDate> | ||
<ttl>60</ttl> | ||
<webMaster>${EMAIL_ADDRESS}</webMaster> | ||
`; | ||
|
||
export const GET: APIRoute = async () => { | ||
const blog = await getCollection("blog"); | ||
return rss({ | ||
xmlns: { | ||
atom: "http://www.w3.org/2005/Atom", | ||
}, | ||
title: BLOG_TITLE, | ||
description: BLOG_DESCRIPTION, | ||
site: import.meta.env.SITE, | ||
customData: BLOG_CUSTOM_DATA, | ||
items: await Promise.all( | ||
blog.map(async (post): Promise<RSSFeedItem> => { | ||
const date = parseDateFromFilePath(post.filePath!); | ||
|
||
const ogImagePath = path.resolve( | ||
process.cwd(), | ||
`./dist/open-graph/${post.id}.png`, | ||
); | ||
const ogImageFile = await fs | ||
.readFile(ogImagePath) | ||
.catch(() => undefined); | ||
|
||
return { | ||
author: `${EMAIL_ADDRESS} (${FULL_NAME})`, | ||
categories: post.data.tags, | ||
description: post.data.description, | ||
enclosure: ogImageFile && { | ||
length: ogImageFile.byteLength, | ||
type: "image/png", | ||
url: `${import.meta.env.SITE}/open-graph/${post.id}.png`, | ||
}, | ||
link: `${import.meta.env.SITE}/thoughts/${post.id}/`, | ||
pubDate: date, | ||
title: post.data.title, | ||
content: | ||
post.body && | ||
sanitizeHtml(parser.render(post.body), { | ||
allowedTags: | ||
sanitizeHtml.defaults.allowedTags.concat([ | ||
"img", | ||
"figure", | ||
]), | ||
}), | ||
customData: `<guid>${import.meta.env.SITE}/thoughts/${post.id}/</guid>`, | ||
}; | ||
}), | ||
).then((items) => | ||
items.sort( | ||
(a, b) => | ||
new Date(b.pubDate!).getTime() - | ||
new Date(a.pubDate!).getTime(), | ||
), | ||
), | ||
}); | ||
}; |