wf4/server/routes/blog/posts.json.ts
2025-07-11 18:01:06 +02:00

36 lines
No EOL
1.1 KiB
TypeScript

const simpleDate = (date: Date) => {
date.setDate(date.getDate() + 1)
return `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(2, '0')}-${`${date.getDate()}`.padStart(2, '0')}`
}
const author = (baseUrl: string) => ({
name: 'webfussel',
img: `${baseUrl}/img/blog/authors/webfussel@3x.webp`,
img1x: `${baseUrl}/img/blog/authors/webfussel@1x.webp`,
img2x: `${baseUrl}/img/blog/authors/webfussel@2x.webp`,
img3x: `${baseUrl}/img/blog/authors/webfussel@3x.webp`,
})
export default defineEventHandler(async event => {
const url = getRequestURL(event)
const baseUrl = `${url.protocol}//${url.host}`
const articles = await queryCollection(event, 'blog')
.where('date', '<', simpleDate(new Date()))
.order('date', 'DESC')
.all()
const entries = articles.map(article => ({
url: `${baseUrl}${article.path}`,
title: article.title,
excerpt: article.description,
date: article.date,
thumbnail: `${baseUrl}${article.thumbnail}`,
category: article.category,
}))
return {
author: author(baseUrl),
posts: entries,
}
})