|
| 1 | +import { LoaderFunction } from 'react-router'; |
| 2 | +import blogPosts from '@pages/blog/blogposts'; |
| 3 | +import type { Dayjs } from 'dayjs'; |
| 4 | +import dayjs from 'dayjs'; |
| 5 | +import advancedFormat from 'dayjs/plugin/advancedFormat'; |
| 6 | +import timezone from 'dayjs/plugin/timezone'; |
| 7 | +import utc from 'dayjs/plugin/utc'; |
| 8 | + |
| 9 | +const BASE_URL = 'https://elanora.lol'; |
| 10 | + |
| 11 | +dayjs.extend(advancedFormat); |
| 12 | +dayjs.extend(timezone); |
| 13 | +dayjs.extend(utc); |
| 14 | + |
| 15 | +const rssDateFormatString = 'ddd, DD MMMM YYYY HH:mm:ss z'; |
| 16 | + |
| 17 | +export interface RssItem { |
| 18 | + title: string; |
| 19 | + link: URL | string; |
| 20 | + description: string; |
| 21 | + pubDate: Dayjs | string; |
| 22 | + author: string; |
| 23 | + guid: string; |
| 24 | +} |
| 25 | + |
| 26 | +export interface RssImage { |
| 27 | + url: URL | string; |
| 28 | + title: string; |
| 29 | + link: URL | string; |
| 30 | + width?: number; |
| 31 | + height?: number; |
| 32 | + description?: string; |
| 33 | +} |
| 34 | + |
| 35 | +export interface RssChannelOptionals { |
| 36 | + language?: string; |
| 37 | + copyright?: string; |
| 38 | + managingEditor?: string; |
| 39 | + webMaster?: string; |
| 40 | + pubDate?: string; |
| 41 | + lastBuildDate?: string; |
| 42 | + category?: string; |
| 43 | + generator?: string; |
| 44 | + docs?: URL | string; |
| 45 | + cloud?: string; |
| 46 | + ttl?: number; |
| 47 | + image?: RssImage; |
| 48 | + rating?: string; |
| 49 | + textInput?: string; |
| 50 | + skiphours?: string; |
| 51 | + skipDays?: string; |
| 52 | +} |
| 53 | + |
| 54 | +export interface RssChannelObj { |
| 55 | + title: string; |
| 56 | + link: URL | string; |
| 57 | + description: string; |
| 58 | + atomLink: URL | string; |
| 59 | + itemArray?: RssItem[]; |
| 60 | + metaOptionals?: RssChannelOptionals; |
| 61 | +} |
| 62 | + |
| 63 | +export const rssMetaDefaults: RssChannelOptionals = { |
| 64 | + language: 'en-us', |
| 65 | + generator: |
| 66 | + 'https://github.com/elanora96/elanora96.github.io/blob/main/app/[rss.xml].tsx', |
| 67 | + docs: new URL('https://www.rssboard.org/rss-specification'), |
| 68 | + ttl: 60, |
| 69 | +}; |
| 70 | + |
| 71 | +const rssChannel: RssChannelObj = { |
| 72 | + title: 'elanora.lol blog posts', |
| 73 | + link: new URL(`${BASE_URL}/blog`), |
| 74 | + atomLink: new URL(`${BASE_URL}/blog/rss.xml`), |
| 75 | + description: 'The collected writings of elanora96', |
| 76 | + itemArray: blogPosts.map((post) => ({ |
| 77 | + description: post.description ? post.description : '', |
| 78 | + author: 'rss@elanora.lol (Elanora Manson)', |
| 79 | + pubDate: post.date.format(rssDateFormatString), |
| 80 | + title: post.postName, |
| 81 | + link: new URL(`${BASE_URL}/blog/${post.postName}`), |
| 82 | + guid: `${BASE_URL}/blog/${post.postName}`, |
| 83 | + })), |
| 84 | + metaOptionals: { |
| 85 | + copyright: 'Copyright 2023 - ∞, Elanora Manson', |
| 86 | + managingEditor: 'rss@elanora.lol (Elanora Manson)', |
| 87 | + webMaster: 'rss@elanora.lol (Elanora Manson)', |
| 88 | + pubDate: dayjs().tz('UTC').format(rssDateFormatString), |
| 89 | + ...rssMetaDefaults, |
| 90 | + }, |
| 91 | +}; |
| 92 | + |
| 93 | +export const generateRssChannel = (props: RssChannelObj): string => { |
| 94 | + const { title, description, link, atomLink, itemArray, metaOptionals } = |
| 95 | + props; |
| 96 | + |
| 97 | + return `<?xml version="1.0" encoding="UTF-8"?> |
| 98 | +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> |
| 99 | + <channel> |
| 100 | + <title>${title}</title> |
| 101 | + <description>${description}</description> |
| 102 | + <link>${link}</link> |
| 103 | + ${ |
| 104 | + metaOptionals |
| 105 | + ? Object.entries(metaOptionals) |
| 106 | + .map(([key, value]) => `<${key}>${value}</${key}>`) |
| 107 | + .join('') |
| 108 | + : '' |
| 109 | + } |
| 110 | + <atom:link href="${atomLink}" rel="self" type="application/rss+xml" /> |
| 111 | + ${ |
| 112 | + itemArray |
| 113 | + ? itemArray |
| 114 | + .map( |
| 115 | + (item) => ` |
| 116 | + <item> |
| 117 | + <title><![CDATA[${item.title}]]></title> |
| 118 | + <description><![CDATA[${item.description}]]></description> |
| 119 | + <pubDate>${item.pubDate}</pubDate> |
| 120 | + <link>${item.link}</link> |
| 121 | + ${item.guid ? `<guid isPermaLink="false">${item.guid}</guid>` : ''} |
| 122 | + </item>`, |
| 123 | + ) |
| 124 | + .join('') |
| 125 | + : '' |
| 126 | + } |
| 127 | + </channel> |
| 128 | +</rss>`; |
| 129 | +}; |
| 130 | + |
| 131 | +export const loader: LoaderFunction = () => { |
| 132 | + const feed = generateRssChannel(rssChannel); |
| 133 | + |
| 134 | + const headersObj = { |
| 135 | + headers: { |
| 136 | + 'Content-Type': 'application/xml', |
| 137 | + 'Cache-Control': 'public, max-age=2419200', |
| 138 | + }, |
| 139 | + }; |
| 140 | + |
| 141 | + return new Response(feed, headersObj); |
| 142 | +}; |
0 commit comments