|
| 1 | +import type { Content, ContentType } from "$lib/types"; |
| 2 | +import { Feed } from 'feed'; |
| 3 | + |
| 4 | +const site = 'https://codingcat.dev'; |
| 5 | + |
| 6 | + |
| 7 | +export const buildFeed = ({ |
| 8 | + contents, |
| 9 | + contentType, |
| 10 | +}: { |
| 11 | + contents: Content[]; |
| 12 | + contentType: ContentType; |
| 13 | +}) => { |
| 14 | + const feed = new Feed({ |
| 15 | + title: `${site} - ${contentType} feed`, |
| 16 | + description: `${site} - ${contentType} feed`, |
| 17 | + id: `${site}`, |
| 18 | + link: `${site}`, |
| 19 | + language: 'en', // optional, used only in RSS 2.0, possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes |
| 20 | + image: `https://media.codingcat.dev/image/upload/f_png,c_thumb,g_face,w_1200,h_630/dev-codingcatdev-photo/v60h88eohd7ufghkspgo.png`, |
| 21 | + favicon: `${site}/favicon.ico`, |
| 22 | + copyright: `All rights reserved 2021, ${site}`, |
| 23 | + updated: new Date(), |
| 24 | + feedLinks: { |
| 25 | + rss2: `${site}/blog/rss.xml`, |
| 26 | + }, |
| 27 | + author: { |
| 28 | + name: 'Alex Patterson', |
| 29 | + email: 'alex@codingcat.dev', |
| 30 | + link: `${site}`, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + for (const content of contents) { |
| 35 | + feed.addItem({ |
| 36 | + title: content.title || '', |
| 37 | + link: `${site}/${contentType}/${content.slug}`, |
| 38 | + description: `${content.excerpt}`, |
| 39 | + image: content.cover || feed.items.at(0)?.image, |
| 40 | + date: content.start || new Date(), |
| 41 | + author: content?.authors ? content.authors?.map((author) => { |
| 42 | + return { |
| 43 | + name: author.name, |
| 44 | + link: `${site}/authors/${author.slug}`, |
| 45 | + }; |
| 46 | + }) |
| 47 | + : [{ |
| 48 | + name: 'Alex Patterson', |
| 49 | + email: 'alex@codingcat.dev', |
| 50 | + link: `${site}`, |
| 51 | + }], |
| 52 | + }); |
| 53 | + } |
| 54 | + return feed; |
| 55 | +}; |
0 commit comments