Skip to content

Commit

Permalink
Improve RSS feed
Browse files Browse the repository at this point in the history
Add cover image, tags, categories, modified date, etc
  • Loading branch information
Hans5958 committed May 5, 2024
1 parent e4f39bc commit bb4fe88
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@ import relativeUrl from '../components/urlHelper.js';
import { SITE_TITLE } from '../config.js';

export async function GET(context) {
const blog = await getCollection('posts');
const blog = (await getCollection('posts')).sort((a, b) => Number(b.data.date) - Number(a.data.date))
return generateRssFromPosts(null, 'Collection and articles and texts of Hans5958.', context, blog)
}

export const generateRssFromPosts = (title, description, context, posts) => {
return rss({
title: SITE_TITLE,
description: "Collection and articles and texts of Hans5958.",
title: title ?? SITE_TITLE,
description: description,
site: context.site,
items: blog.sort((a, b) => Number(b.data.date) - Number(a.data.date)).map((post) => ({
title: post.data.title,
pubDate: post.data.date,
description: post.data.description,
link: relativeUrl('/' + post.slug),
})),
});
items: posts.map((post) => {
const customData: string[] = []

customData.push('<dc:creator><![CDATA[Hans5958]]></dc:creator>')

if (post.data.dateMod) {
customData.push(`<atom:updated>${new Date(post.data.dateMod).toISOString()}</atom:updated>`)
}

if (post.data.cover?.src) {
customData.push(`<media:thumbnail url="${post.data.cover?.src}" />`)
}

return {
title: post.data.title,
pubDate: post.data.date,
description: post.data.description,
link: relativeUrl('/' + post.slug),
categories: [post.data.category, ...post.data.tags],
customData: customData.join("")
}
}),
xmlns: {
dc: 'http://purl.org/dc/elements/1.1/',
atom: 'http://www.w3.org/2005/Atom',
media: 'http://search.yahoo.com/mrss/'
}
})
}

0 comments on commit bb4fe88

Please sign in to comment.