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 { getSlug, enrichPostsWithDates } from '../lib/md';
|
||||||
import { getTxtFiles } from '../lib/txt';
|
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) {
|
export async function GET(context: APIContext) {
|
||||||
const rawPosts = await getCollection('md');
|
const rawPosts = await getCollection('md');
|
||||||
const posts = enrichPostsWithDates(rawPosts);
|
const posts = enrichPostsWithDates(rawPosts);
|
||||||
const bookmarks = await getCollection('bookmarks');
|
|
||||||
const txtFiles = getTxtFiles();
|
const txtFiles = getTxtFiles();
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
|
|
@ -15,25 +26,19 @@ export async function GET(context: APIContext) {
|
||||||
title: post.data.title,
|
title: post.data.title,
|
||||||
pubDate: post.dates.created,
|
pubDate: post.dates.created,
|
||||||
link: `/${getSlug(post.id)}`,
|
link: `/${getSlug(post.id)}`,
|
||||||
description: post.data.title,
|
description: excerpt((post as any).body) || post.data.title,
|
||||||
})),
|
})),
|
||||||
...txtFiles.map(txt => ({
|
...txtFiles.map(txt => ({
|
||||||
title: txt.name,
|
title: txt.name,
|
||||||
pubDate: txt.date,
|
pubDate: txt.date,
|
||||||
link: `/${txt.name}`,
|
link: `/${txt.name}`,
|
||||||
description: txt.name,
|
description: txt.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());
|
].sort((a, b) => b.pubDate.getTime() - a.pubDate.getTime());
|
||||||
|
|
||||||
return rss({
|
return rss({
|
||||||
title: 'wynne.rs',
|
title: 'wynne.rs',
|
||||||
description: '',
|
description: 'personal website of lewis m.w.',
|
||||||
site: context.site ?? 'https://wynne.rs',
|
site: context.site ?? 'https://wynne.rs',
|
||||||
items,
|
items,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue