|
| 1 | +import { portableTextToHtml } from "./portable-text-html"; |
| 2 | +import type { SanityRequestContext } from "./sanity/context"; |
| 3 | +import { SITE_NAME } from "./site"; |
| 4 | +import { cdata, escapeXml, imageMimeType, rfc822 } from "./xml"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Feed serialization. |
| 8 | + * |
| 9 | + * The output reproduces what the `feed` npm package emitted, element order and |
| 10 | + * all, because these documents are already subscribed to. In particular: |
| 11 | + * |
| 12 | + * - `<guid isPermaLink="false">` stays the Sanity `_id`. Changing a podcast |
| 13 | + * GUID makes Apple and Spotify re-publish the entire back catalogue as new |
| 14 | + * episodes; this is the single most destructive thing this file could get |
| 15 | + * wrong. |
| 16 | + * - Enclosure MIME types keep the library's `image/jpg` spelling. |
| 17 | + * |
| 18 | + * The library itself is not ported: it reaches for Node built-ins that are |
| 19 | + * awkward on workerd, and this is a fixed document shape. |
| 20 | + */ |
| 21 | + |
| 22 | +/** Feeds only ever carry the most recent entries. */ |
| 23 | +export const FEED_LIMIT = 50; |
| 24 | + |
| 25 | +const FEED_AUTHOR = { |
| 26 | + name: "Alex Patterson", |
| 27 | + email: "alex@codingcat.dev", |
| 28 | + slug: "alex-patterson", |
| 29 | +}; |
| 30 | + |
| 31 | +/** Sanity `_type` to the URL segment its listing lives under. */ |
| 32 | +export function typePath(type: string): string { |
| 33 | + switch (type) { |
| 34 | + case "post": |
| 35 | + return "blog"; |
| 36 | + case "podcast": |
| 37 | + return "podcasts"; |
| 38 | + default: |
| 39 | + return `${type}s`; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export interface FeedItemSource { |
| 44 | + _id: string; |
| 45 | + _type: string; |
| 46 | + title: string; |
| 47 | + slug: string | null; |
| 48 | + excerpt?: string | null; |
| 49 | + coverImage?: unknown; |
| 50 | + date?: string | null; |
| 51 | + content?: unknown; |
| 52 | + author?: Array<{ title: string; slug: string | null }> | null; |
| 53 | + season?: number | null; |
| 54 | + episode?: number | null; |
| 55 | + spotify?: { |
| 56 | + link?: string; |
| 57 | + enclosures?: Array<{ url?: string; length?: number; type?: string }>; |
| 58 | + itunes?: { |
| 59 | + duration?: string; |
| 60 | + episodeType?: string; |
| 61 | + explicit?: string; |
| 62 | + summary?: string; |
| 63 | + image?: { href?: string }; |
| 64 | + }; |
| 65 | + } | null; |
| 66 | +} |
| 67 | + |
| 68 | +interface FeedContext { |
| 69 | + origin: string; |
| 70 | + sanity: SanityRequestContext; |
| 71 | + type: "post" | "podcast"; |
| 72 | + items: FeedItemSource[]; |
| 73 | + now: Date; |
| 74 | +} |
| 75 | + |
| 76 | +function itemUrl(origin: string, item: FeedItemSource) { |
| 77 | + return `${origin}/${item._type}/${item.slug}`; |
| 78 | +} |
| 79 | + |
| 80 | +function itemImage( |
| 81 | + ctx: FeedContext, |
| 82 | + item: FeedItemSource, |
| 83 | + width: number, |
| 84 | + height: number, |
| 85 | +) { |
| 86 | + return ctx.sanity |
| 87 | + .urlForImage(item.coverImage as never) |
| 88 | + ?.width(width) |
| 89 | + .height(height) |
| 90 | + .url(); |
| 91 | +} |
| 92 | + |
| 93 | +function itemHtml(ctx: FeedContext, item: FeedItemSource) { |
| 94 | + return portableTextToHtml(item.content, { |
| 95 | + origin: ctx.origin, |
| 96 | + imageUrl: (source) => |
| 97 | + ctx.sanity |
| 98 | + .urlForImage(source as never) |
| 99 | + ?.width(1200) |
| 100 | + .url(), |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +function itemAuthors(item: FeedItemSource) { |
| 105 | + return item.author?.length ? item.author : null; |
| 106 | +} |
| 107 | + |
| 108 | +// --- RSS 2.0 (blog) --- |
| 109 | + |
| 110 | +export function buildRss2(ctx: FeedContext): string { |
| 111 | + const { origin, type, items, now } = ctx; |
| 112 | + const feedPath = typePath(type); |
| 113 | + const title = `${SITE_NAME} - ${type} feed`; |
| 114 | + const home = `${origin}/${feedPath}`; |
| 115 | + |
| 116 | + const body = items |
| 117 | + .map((item) => { |
| 118 | + const image = itemImage(ctx, item, 1200, 630); |
| 119 | + const authors = itemAuthors(item) ?? [{ title: FEED_AUTHOR.name }]; |
| 120 | + |
| 121 | + // One element per author, and description omitted entirely when the |
| 122 | + // excerpt is empty — both reproduce the `feed` library exactly, so |
| 123 | + // existing subscribers see no change. |
| 124 | + const authorTags = authors |
| 125 | + .map( |
| 126 | + (author) => |
| 127 | + `\n <author>${escapeXml(author.title)}</author>`, |
| 128 | + ) |
| 129 | + .join(""); |
| 130 | + |
| 131 | + const description = item.excerpt |
| 132 | + ? `\n <description>${cdata(item.excerpt)}</description>` |
| 133 | + : ""; |
| 134 | + |
| 135 | + const enclosure = image |
| 136 | + ? `\n <enclosure url="${escapeXml(image)}" length="0" type="${imageMimeType(image)}"/>` |
| 137 | + : ""; |
| 138 | + |
| 139 | + return ` <item> |
| 140 | + <title>${cdata(item.title)}</title> |
| 141 | + <link>${escapeXml(itemUrl(origin, item))}</link> |
| 142 | + <guid isPermaLink="false">${escapeXml(item._id)}</guid> |
| 143 | + <pubDate>${rfc822(item.date ? new Date(item.date) : now)}</pubDate>${description} |
| 144 | + <content:encoded>${cdata(itemHtml(ctx, item))}</content:encoded>${authorTags}${enclosure} |
| 145 | + </item>`; |
| 146 | + }) |
| 147 | + .join("\n"); |
| 148 | + |
| 149 | + return `<?xml version="1.0" encoding="utf-8"?> |
| 150 | +<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"> |
| 151 | + <channel> |
| 152 | + <title>${escapeXml(title)}</title> |
| 153 | + <link>${escapeXml(home)}</link> |
| 154 | + <description>${escapeXml(title)}</description> |
| 155 | + <lastBuildDate>${rfc822(now)}</lastBuildDate> |
| 156 | + <docs>https://validator.w3.org/feed/docs/rss2.html</docs> |
| 157 | + <generator>${escapeXml(origin)}</generator> |
| 158 | + <language>en</language> |
| 159 | + <image> |
| 160 | + <title>${escapeXml(title)}</title> |
| 161 | + <url>${escapeXml(origin)}/icon.svg</url> |
| 162 | + <link>${escapeXml(home)}</link> |
| 163 | + </image> |
| 164 | + <copyright>All rights reserved ${now.getUTCFullYear()}, ${SITE_NAME}</copyright> |
| 165 | +${body} |
| 166 | + </channel> |
| 167 | +</rss>`; |
| 168 | +} |
| 169 | + |
| 170 | +// --- JSON Feed 1.0 --- |
| 171 | + |
| 172 | +export function buildJsonFeed(ctx: FeedContext): string { |
| 173 | + const { origin, type, items } = ctx; |
| 174 | + const feedPath = typePath(type); |
| 175 | + const title = `${SITE_NAME} - ${type} feed`; |
| 176 | + |
| 177 | + return JSON.stringify( |
| 178 | + { |
| 179 | + version: "https://jsonfeed.org/version/1", |
| 180 | + title, |
| 181 | + home_page_url: `${origin}/${feedPath}`, |
| 182 | + feed_url: `${origin}/${feedPath}/rss.json`, |
| 183 | + description: title, |
| 184 | + icon: `${origin}/icon.svg`, |
| 185 | + author: { |
| 186 | + name: FEED_AUTHOR.name, |
| 187 | + url: origin, |
| 188 | + }, |
| 189 | + items: items.map((item) => { |
| 190 | + const authors = itemAuthors(item); |
| 191 | + const primary = authors?.[0]; |
| 192 | + return { |
| 193 | + id: item._id, |
| 194 | + content_html: itemHtml(ctx, item), |
| 195 | + url: itemUrl(origin, item), |
| 196 | + title: item.title, |
| 197 | + summary: item.excerpt ?? undefined, |
| 198 | + image: itemImage(ctx, item, 1200, 630), |
| 199 | + date_modified: item.date |
| 200 | + ? new Date(item.date).toISOString() |
| 201 | + : undefined, |
| 202 | + author: primary |
| 203 | + ? { |
| 204 | + name: primary.title, |
| 205 | + url: `${origin}/author/${primary.slug}`, |
| 206 | + } |
| 207 | + : { |
| 208 | + name: FEED_AUTHOR.name, |
| 209 | + url: `${origin}/author/${FEED_AUTHOR.slug}`, |
| 210 | + }, |
| 211 | + }; |
| 212 | + }), |
| 213 | + }, |
| 214 | + null, |
| 215 | + 2, |
| 216 | + ); |
| 217 | +} |
| 218 | + |
| 219 | +// --- Podcast RSS with the iTunes namespace --- |
| 220 | + |
| 221 | +export function buildPodcastRss(ctx: FeedContext): string { |
| 222 | + const { origin, items, now } = ctx; |
| 223 | + const feedUrl = `${origin}/podcasts/rss.xml`; |
| 224 | + const feedImage = `${origin}/icon.svg`; |
| 225 | + |
| 226 | + const body = items |
| 227 | + .map((item) => { |
| 228 | + const image = itemImage(ctx, item, 1400, 1400) ?? feedImage; |
| 229 | + const title = escapeXml(item.title); |
| 230 | + const authors = itemAuthors(item); |
| 231 | + const authorName = authors |
| 232 | + ? authors.map((a) => a.title).join(", ") |
| 233 | + : FEED_AUTHOR.name; |
| 234 | + |
| 235 | + const spotify = item.spotify; |
| 236 | + const enclosure = spotify?.enclosures?.[0]; |
| 237 | + const enclosureXml = enclosure?.url |
| 238 | + ? `<enclosure url="${escapeXml(enclosure.url)}" length="${enclosure.length ?? 0}" type="${escapeXml(enclosure.type ?? "audio/mpeg")}" />\n ` |
| 239 | + : ""; |
| 240 | + |
| 241 | + const itunes = spotify?.itunes; |
| 242 | + let itunesXml = ""; |
| 243 | + if (itunes?.explicit) { |
| 244 | + itunesXml += `\n <itunes:explicit>${escapeXml(itunes.explicit)}</itunes:explicit>`; |
| 245 | + } |
| 246 | + if (itunes?.summary) { |
| 247 | + itunesXml += `\n <itunes:summary>${escapeXml(itunes.summary)}</itunes:summary>`; |
| 248 | + } |
| 249 | + if (itunes?.image?.href) { |
| 250 | + itunesXml += `\n <itunes:image href="${escapeXml(itunes.image.href)}" />`; |
| 251 | + } |
| 252 | + |
| 253 | + const season = item.season |
| 254 | + ? `\n <itunes:season>${item.season}</itunes:season>` |
| 255 | + : ""; |
| 256 | + const episode = item.episode |
| 257 | + ? `\n <itunes:episode>${item.episode}</itunes:episode>` |
| 258 | + : ""; |
| 259 | + const duration = itunes?.duration |
| 260 | + ? `\n <itunes:duration>${escapeXml(itunes.duration)}</itunes:duration>` |
| 261 | + : ""; |
| 262 | + |
| 263 | + return ` <item> |
| 264 | + <title>${title}</title> |
| 265 | + <link>${escapeXml(itemUrl(origin, item))}</link> |
| 266 | + <guid isPermaLink="false">${escapeXml(item._id)}</guid> |
| 267 | + <pubDate>${rfc822(item.date ? new Date(item.date) : now)}</pubDate> |
| 268 | + <description>${cdata(item.excerpt ?? "")}</description> |
| 269 | + <author>${escapeXml(authorName)}</author> |
| 270 | + ${enclosureXml}<itunes:title>${title}</itunes:title> |
| 271 | + <itunes:author>${escapeXml(authorName)}</itunes:author> |
| 272 | + <itunes:image href="${escapeXml(image)}" />${season}${episode} |
| 273 | + <itunes:episodeType>${escapeXml(itunes?.episodeType ?? "full")}</itunes:episodeType>${duration}${itunesXml} |
| 274 | + </item>`; |
| 275 | + }) |
| 276 | + .join("\n"); |
| 277 | + |
| 278 | + return `<?xml version="1.0" encoding="utf-8"?> |
| 279 | +<rss version="2.0" |
| 280 | + xmlns:atom="http://www.w3.org/2005/Atom" |
| 281 | + xmlns:itunes="http://www.itunes.apple.com/dtds/podcast-1.0.dtd" |
| 282 | + xmlns:content="http://purl.org/rss/1.0/modules/content/" |
| 283 | + xmlns:podcast="https://podcastindex.org/namespace/1.0"> |
| 284 | + <channel> |
| 285 | + <title>${SITE_NAME} Podcast</title> |
| 286 | + <link>${escapeXml(origin)}/podcasts</link> |
| 287 | + <description>The ${SITE_NAME} Podcast features conversations about web development, design, and technology with industry experts and community members.</description> |
| 288 | + <language>en</language> |
| 289 | + <lastBuildDate>${rfc822(now)}</lastBuildDate> |
| 290 | + <atom:link href="${escapeXml(feedUrl)}" rel="self" type="application/rss+xml" /> |
| 291 | + <copyright>All rights reserved ${now.getUTCFullYear()}, ${SITE_NAME}</copyright> |
| 292 | + <itunes:author>${FEED_AUTHOR.name}</itunes:author> |
| 293 | + <itunes:owner> |
| 294 | + <itunes:name>${FEED_AUTHOR.name}</itunes:name> |
| 295 | + <itunes:email>${FEED_AUTHOR.email}</itunes:email> |
| 296 | + </itunes:owner> |
| 297 | + <itunes:image href="${escapeXml(feedImage)}" /> |
| 298 | + <itunes:category text="Technology" /> |
| 299 | + <itunes:explicit>false</itunes:explicit> |
| 300 | + <itunes:type>episodic</itunes:type> |
| 301 | + <image> |
| 302 | + <url>${escapeXml(feedImage)}</url> |
| 303 | + <title>${SITE_NAME} Podcast</title> |
| 304 | + <link>${escapeXml(origin)}/podcasts</link> |
| 305 | + </image> |
| 306 | +${body} |
| 307 | + </channel> |
| 308 | +</rss>`; |
| 309 | +} |
| 310 | + |
| 311 | +export type { FeedContext }; |
0 commit comments