feat: add rss feed with all content types
This commit is contained in:
parent
384d2d59b7
commit
7346be6687
2 changed files with 62 additions and 1 deletions
|
|
@ -11,7 +11,7 @@ const { title, showHeader = true, isHome = false } = Astro.props;
|
||||||
---
|
---
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{title}</title></head>
|
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{title}</title><link rel="alternate" type="application/rss+xml" title="wynne.rs" href="/feed.xml" /></head>
|
||||||
<body>
|
<body>
|
||||||
{showHeader && (
|
{showHeader && (
|
||||||
<header>
|
<header>
|
||||||
|
|
|
||||||
61
apps/blog/src/pages/feed.xml.ts
Normal file
61
apps/blog/src/pages/feed.xml.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import rss from '@astrojs/rss';
|
||||||
|
import { getCollection } from 'astro:content';
|
||||||
|
import yaml from 'js-yaml';
|
||||||
|
import bookmarksRaw from '../data/bookmarks.yaml?raw';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
|
import type { APIContext } from 'astro';
|
||||||
|
|
||||||
|
interface Bookmark {
|
||||||
|
date: string;
|
||||||
|
title: string;
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TxtFile {
|
||||||
|
name: string;
|
||||||
|
mtime: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(context: APIContext) {
|
||||||
|
const posts = await getCollection('posts');
|
||||||
|
const bookmarks = yaml.load(bookmarksRaw) as Bookmark[];
|
||||||
|
|
||||||
|
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,
|
||||||
|
mtime: fs.statSync(path.join(txtDir, name)).mtime,
|
||||||
|
}))
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
...posts.map(post => ({
|
||||||
|
title: post.data.title,
|
||||||
|
pubDate: post.data.date,
|
||||||
|
link: `/blog/${post.id}`,
|
||||||
|
description: post.data.title,
|
||||||
|
})),
|
||||||
|
...txtFiles.map(txt => ({
|
||||||
|
title: txt.name,
|
||||||
|
pubDate: txt.mtime,
|
||||||
|
link: `/txt/${txt.name}`,
|
||||||
|
description: txt.name,
|
||||||
|
})),
|
||||||
|
...bookmarks.map(b => ({
|
||||||
|
title: b.title,
|
||||||
|
pubDate: new Date(b.date),
|
||||||
|
link: b.url,
|
||||||
|
description: b.title,
|
||||||
|
})),
|
||||||
|
].sort((a, b) => b.pubDate.getTime() - a.pubDate.getTime());
|
||||||
|
|
||||||
|
return rss({
|
||||||
|
title: 'wynne.rs',
|
||||||
|
description: '',
|
||||||
|
site: context.site ?? 'https://wynne.rs',
|
||||||
|
items,
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue