|
| 1 | +import { joinURL } from 'ufo' |
| 2 | +import { Feed } from 'feed' |
| 3 | +import { queryCollection } from '@nuxt/content/server' |
| 4 | +import type { EventHandlerRequest, H3Event } from 'h3' |
| 5 | + |
| 6 | +export async function generateFeed(event: H3Event<EventHandlerRequest>, tags: string[] = []): Promise<Feed> { |
| 7 | + const baseUrl = 'https://techwatching.dev' |
| 8 | + const siteUrl = joinURL(baseUrl, 'posts') |
| 9 | + const feed = new Feed({ |
| 10 | + title: 'Alexandre Nedelec\'s blog', |
| 11 | + description: 'Alexandre Nédélec\'s blog', |
| 12 | + id: siteUrl, |
| 13 | + link: siteUrl, |
| 14 | + language: 'en', |
| 15 | + favicon: joinURL(baseUrl, 'favicon.ico'), |
| 16 | + copyright: `Copyright © 2019-${new Date().getFullYear()} Alexandre Nedelec All Rights Reserved`, |
| 17 | + feedLinks: { |
| 18 | + rss: `${baseUrl}/feed.rss`, |
| 19 | + atom: `${baseUrl}/feed.atom` |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + const articles = await queryCollection(event, 'posts') |
| 24 | + .order('date', 'DESC') |
| 25 | + .all() |
| 26 | + |
| 27 | + for (const article of articles.filter(article => tags.length === 0 || article.tags?.some((tag: string) => tags.includes(tag)))) { |
| 28 | + feed.addItem({ |
| 29 | + link: joinURL(baseUrl, article.path ?? ''), |
| 30 | + image: joinURL(baseUrl, article.image?.src ?? ''), |
| 31 | + title: article.title!, |
| 32 | + date: new Date(article.date), |
| 33 | + description: article.description, |
| 34 | + author: article.authors?.map(a => ({ name: a.name, link: a.to })), |
| 35 | + category: article.tags?.map(tag => ({ name: tag })) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + return feed |
| 40 | +} |
0 commit comments