lunasqu.ee-nuxt/server/routes/blog/atom.xml.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-10-16 12:50:21 +00:00
import { Feed } from 'feed';
2022-10-16 16:17:55 +00:00
import { getFilteredBlogPosts } from '~~/lib/blog/read-posts';
2022-10-16 12:50:21 +00:00
const BASE_URL = 'https://lunasqu.ee/blog';
export default defineEventHandler(async (event) => {
2022-10-16 16:17:55 +00:00
const posts = await getFilteredBlogPosts(undefined, true, false);
2022-10-16 12:50:21 +00:00
const feed = new Feed({
title: "Evert's Blog",
description: 'Projects and Tutorials',
id: BASE_URL,
link: BASE_URL,
2023-09-22 15:01:40 +00:00
copyright: 'Evert "Diamond" Prants 2023',
2022-10-16 12:50:21 +00:00
language: 'en',
updated: new Date(posts[0].date),
2023-09-22 15:01:40 +00:00
generator: 'https://git.icynet.eu/evert/lunasqu.ee-nuxt',
2022-10-16 12:50:21 +00:00
feedLinks: {
atom: `${BASE_URL}/atom.xml`,
},
});
posts.forEach((post) => {
const description = post.summary
2022-10-16 12:50:21 +00:00
.replace(/<[^>]*>?/gm, '')
.replace('\n', ' ')
.substring(0, 240);
feed.addItem({
title: post.title,
id: `${BASE_URL}/${post.fullSlug}`,
link: `${BASE_URL}/${post.fullSlug}`,
description,
content: post.html,
date: new Date(post.date),
published: new Date(post.date),
category: post.tags.map((tag) => ({
term: tag,
scheme: `${BASE_URL}/tags/${tag}`,
})),
});
});
appendResponseHeader(event, 'Content-Type', 'text/xml');
return feed.atom1();
});