www/app/blog.rss/route.ts

66 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

import { allPosts, Post } from "content-collections";
import { compareDesc, parseISO, setHours } from "date-fns";
import { Feed } from "feed";
const baseUrl = "https://techaro.lol";
const createPostUrl = (url: string) => {
return url + "?utm_campaign=feed&utm_source=blog.rss";
};
const createContent = (post: Post, url: string) => `<img src="${post.imageURL}" width="1000" height="420" vspace="3" hspace="8" align="center">
<p>${post.summary}</p>
<p>Read the full article on <a href="${url}">techaro.lol</a></p>`;
const createFeed = () => {
const feed = new Feed({
title: "Techaro Blog",
description: "Insights and updates from the Techaro team.",
id: baseUrl,
link: baseUrl,
language: "en",
favicon: `${baseUrl}/favicon.ico`,
copyright: `All rights reserved ${new Date().getFullYear()}, Techaro Computing Canada`,
generator: "Techaro.lol www",
author: {
name: "Techaro Staff",
email: "staff@techaro.lol",
link: baseUrl,
},
});
allPosts
.sort((a, b) => compareDesc(new Date(a.date), new Date(b.date)))
.forEach((post) => {
const id = `${baseUrl}/${post.urlPath}`;
const url = createPostUrl(id);
feed.addItem({
title: post.title,
id: id,
link: url,
description: post.summary,
content: createContent(post, url),
author: [
{
name: post.author!.displayName!,
email: `${post.author?.name}@techaro.lol`,
link: baseUrl,
}
],
date: setHours(parseISO(post.date), 13),
image: post.imageURL,
});
});
return feed.rss2();
};
export const GET = async () => {
const feed = await createFeed();
return new Response(feed, {
status: 200,
headers: {
"Content-Type": "application/xml",
},
});
};