|
| 1 | +import { Feed } from 'feed' |
| 2 | +import { posts } from '#blog/posts' |
| 3 | + |
| 4 | +export const rssPath = '/rss.xml' |
| 5 | +export const atomPath = '/atom.xml' |
| 6 | +export const jsonPath = '/feed.json' |
| 7 | + |
| 8 | +let feed: Feed | undefined = undefined |
| 9 | + |
| 10 | +export function getFeed(): Feed { |
| 11 | + if (!feed) { |
| 12 | + feed = generateFeed() |
| 13 | + } |
| 14 | + return feed |
| 15 | +} |
| 16 | + |
| 17 | +function generateFeed(): Feed { |
| 18 | + // Generate content for RSS, Atom and JSON feeds |
| 19 | + const feed = new Feed({ |
| 20 | + title: 'Blog - npmx', |
| 21 | + description: 'a fast, modern browser for the npm registry', |
| 22 | + id: 'https://npmx.dev/', |
| 23 | + link: 'https://npmx.dev/', |
| 24 | + language: 'en', |
| 25 | + image: 'https://npmx.dev/logo.svg', |
| 26 | + favicon: 'https://npmx.dev/favicon.ico', |
| 27 | + feedLinks: { |
| 28 | + rss: new URL(rssPath, 'https://npmx.dev').toString(), |
| 29 | + atom: new URL(atomPath, 'https://npmx.dev').toString(), |
| 30 | + json: new URL(jsonPath, 'https://npmx.dev').toString(), |
| 31 | + }, |
| 32 | + }) |
| 33 | + |
| 34 | + for (const post of posts.filter(post => !post.draft)) { |
| 35 | + feed.addItem({ |
| 36 | + title: post.title, |
| 37 | + id: new URL(post.path, 'https://npmx.dev').toString(), |
| 38 | + link: new URL(post.path, 'https://npmx.dev').toString(), |
| 39 | + description: post.description, |
| 40 | + author: post.authors.map(author => ({ |
| 41 | + name: author.name, |
| 42 | + link: author.profileUrl ?? undefined, |
| 43 | + // author.avatar is a relative URL - make it absolute to work in feed readers |
| 44 | + avatar: author.avatar ? new URL(author.avatar, 'https://npmx.dev').toString() : undefined, |
| 45 | + })), |
| 46 | + date: new Date(post.date), |
| 47 | + image: post.image, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + return feed |
| 52 | +} |
0 commit comments