feat: short excerpt in rss feed
This commit is contained in:
parent
071ebdfe66
commit
e431533a39
1 changed files with 15 additions and 10 deletions
|
|
@ -4,10 +4,21 @@ import type { APIContext } from 'astro';
|
|||
import { getSlug, enrichPostsWithDates } from '../lib/md';
|
||||
import { getTxtFiles } from '../lib/txt';
|
||||
|
||||
function excerpt(markdown: string | undefined, maxLen = 160): string {
|
||||
if (!markdown) return '';
|
||||
return markdown
|
||||
.replace(/^#+\s+.*$/gm, '')
|
||||
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
|
||||
.replace(/[*_~`]/g, '')
|
||||
.replace(/:[a-z]+\[([^\]]*)\]/g, '$1')
|
||||
.replace(/\n+/g, ' ')
|
||||
.trim()
|
||||
.slice(0, maxLen);
|
||||
}
|
||||
|
||||
export async function GET(context: APIContext) {
|
||||
const rawPosts = await getCollection('md');
|
||||
const posts = enrichPostsWithDates(rawPosts);
|
||||
const bookmarks = await getCollection('bookmarks');
|
||||
const txtFiles = getTxtFiles();
|
||||
|
||||
const items = [
|
||||
|
|
@ -15,25 +26,19 @@ export async function GET(context: APIContext) {
|
|||
title: post.data.title,
|
||||
pubDate: post.dates.created,
|
||||
link: `/${getSlug(post.id)}`,
|
||||
description: post.data.title,
|
||||
description: excerpt((post as any).body) || post.data.title,
|
||||
})),
|
||||
...txtFiles.map(txt => ({
|
||||
title: txt.name,
|
||||
pubDate: txt.date,
|
||||
link: `/${txt.name}`,
|
||||
description: txt.name,
|
||||
})),
|
||||
...bookmarks.map(b => ({
|
||||
title: b.data.title,
|
||||
pubDate: b.data.date,
|
||||
link: b.data.url,
|
||||
description: b.data.title,
|
||||
description: txt.description || txt.name,
|
||||
})),
|
||||
].sort((a, b) => b.pubDate.getTime() - a.pubDate.getTime());
|
||||
|
||||
return rss({
|
||||
title: 'wynne.rs',
|
||||
description: '',
|
||||
description: 'personal website of lewis m.w.',
|
||||
site: context.site ?? 'https://wynne.rs',
|
||||
items,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue