54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import rss from '@astrojs/rss';
|
|
import { getCollection } from 'astro:content';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import type { APIContext } from 'astro';
|
|
import { getGitDate } from '../utils';
|
|
|
|
interface TxtFile {
|
|
name: string;
|
|
date: Date;
|
|
}
|
|
|
|
export async function GET(context: APIContext) {
|
|
const posts = await getCollection('posts', ({ data }) => data.draft !== true);
|
|
const bookmarks = await getCollection('bookmarks');
|
|
|
|
const txtDir = path.join(process.cwd(), 'public/txt');
|
|
const txtFiles: TxtFile[] = fs.existsSync(txtDir)
|
|
? fs.readdirSync(txtDir)
|
|
.filter(file => file.endsWith('.txt'))
|
|
.map(name => ({
|
|
name,
|
|
date: getGitDate(path.join(txtDir, name)),
|
|
}))
|
|
: [];
|
|
|
|
const items = [
|
|
...posts.map(post => ({
|
|
title: post.data.title,
|
|
pubDate: post.data.date,
|
|
link: `/md/${post.id}`,
|
|
description: post.data.title,
|
|
})),
|
|
...txtFiles.map(txt => ({
|
|
title: txt.name,
|
|
pubDate: txt.date,
|
|
link: `/txt/${txt.name}`,
|
|
description: txt.name,
|
|
})),
|
|
...bookmarks.map(b => ({
|
|
title: b.data.title,
|
|
pubDate: b.data.date,
|
|
link: b.data.url,
|
|
description: b.data.title,
|
|
})),
|
|
].sort((a, b) => b.pubDate.getTime() - a.pubDate.getTime());
|
|
|
|
return rss({
|
|
title: 'wynne.rs',
|
|
description: '',
|
|
site: context.site ?? 'https://wynne.rs',
|
|
items,
|
|
});
|
|
}
|