47 lines
No EOL
1.3 KiB
TypeScript
47 lines
No EOL
1.3 KiB
TypeScript
import { BlogCollectionItem } from '@nuxt/content'
|
|
|
|
const clean = (value: string) => value
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/ä/g, 'ä')
|
|
.replace(/ö/g, 'ö')
|
|
.replace(/ü/g, 'ü')
|
|
.replace(/ß/g, 'ß')
|
|
|
|
const makeItem = (article: BlogCollectionItem, base: string) => `
|
|
<item>
|
|
<title>${clean(article.title)}</title>
|
|
<description>${clean(article.description)}</description>
|
|
<link>${base}${article.path}</link>
|
|
</item>
|
|
`
|
|
|
|
const simpleDate = (date: Date) => {
|
|
date.setDate(date.getDate() + 1)
|
|
return `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(2, '0')}-${`${date.getDate()}`.padStart(2, '0')}`
|
|
}
|
|
|
|
export default defineEventHandler(async event => {
|
|
const baseUrl = getRequestURL(event)
|
|
const blogUrl = `${baseUrl.protocol}//${baseUrl.host}`
|
|
|
|
const articles = await queryCollection(event, 'blog')
|
|
.where('date', '<', simpleDate(new Date()))
|
|
.order('date', 'DESC')
|
|
.all()
|
|
|
|
const xmlItems = articles.map(article => makeItem(article, blogUrl)).join('')
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0">
|
|
<channel>
|
|
<title>Blogfussel</title>
|
|
<link>${baseUrl}/blog</link>
|
|
<description>für mehr Fussel im Blog</description>
|
|
<language>de</language>
|
|
${xmlItems}
|
|
</channel>
|
|
</rss>
|
|
`
|
|
}) |