Skip to content

Commit

Permalink
Improve RSS feed generation
Browse files Browse the repository at this point in the history
  • Loading branch information
meduzen committed Nov 11, 2023
1 parent 3f01c4b commit 51a40bb
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions vitepress/.vitepress/rss.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,6 @@ const baseUrl = `https://blog.mehdi.cc` // should come from .env
* @param {import('vitepress').SiteConfig} config
*/
export async function rss(config) {

// get notes

const notes = (await createContentLoader('notes/*.md', {
excerpt: true,
render: true,
}).load())
.filter(isPublished)

// get articles

const articles = (await createContentLoader('articles/*.md', {
excerpt: true,
render: true,
}).load())
.filter(isPublished)

// sort them

articles.sort(comparePublicationDate)
notes.sort(comparePublicationDate)

// combine articles and notes

const items = articles.concat(notes).toSorted(comparePublicationDate)

/**
* https://www.rssboard.org/rss-profile
* https://github.com/jpmonette/feed
Expand All @@ -51,23 +25,26 @@ export async function rss(config) {
copyright: 'Copyright © 2023-present, Mehdi Merah',
feed: `${baseUrl}/feed.rss`,
ttl: 2880, // 1 day,
})
});

items.forEach(({ url, excerpt, frontmatter, html }) =>
feed.addItem({
title: frontmatter.title,
id: `${baseUrl}${url}`,
link: `${baseUrl}${url}`,
description: frontmatter.description || excerpt,
content: html,
date: frontmatter.publishedAt,
author: [{
name: 'Mehdi Merah',
link: 'https://mehdi.cc',
email: ' ', // hack, otherwise <author> is missing in RSS
}],
})
)
(await createContentLoader(['articles/*.md', 'notes/*.md'], { excerpt: true, render: true }).load())
.filter(isPublished)
.toSorted(comparePublicationDate)
.forEach(({ url, excerpt, frontmatter, html }) =>
feed.addItem({
title: frontmatter.title,
id: `${baseUrl}${url}`,
link: `${baseUrl}${url}`,
description: frontmatter.description || excerpt,
content: html,
date: frontmatter.publishedAt,
author: [{
name: 'Mehdi Merah',
link: 'https://mehdi.cc',
email: ' ', // hack, otherwise <author> is missing in RSS
}],
})
)

writeFileSync(path.join(config.outDir, 'feed.rss'), feed.rss2())
}

0 comments on commit 51a40bb

Please sign in to comment.