diff options
| author | netop://ウィビ <paul@webb.page> | 2026-04-11 14:24:49 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-04-11 14:24:49 -0700 |
| commit | 8c34d810af95fae0ef846f54370a8c88bfab7123 (patch) | |
| tree | 436beaf30f7b2b3f15741dd54a37e313964d1f7d /src/utility/feed/rss.ts | |
Diffstat (limited to 'src/utility/feed/rss.ts')
| -rw-r--r-- | src/utility/feed/rss.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/utility/feed/rss.ts b/src/utility/feed/rss.ts new file mode 100644 index 0000000..c3971bb --- /dev/null +++ b/src/utility/feed/rss.ts @@ -0,0 +1,85 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import { BaseFeed, escapeXML, type FeedOptions } from "./helper.ts"; + +interface RssItem { + content: { + body: string; + }; + description: string; + id: string; + image?: string; + link: string; + title: string; + updated?: Date; +} + +/*** EXPORT ------------------------------------------- ***/ + +export class FeedRSS extends BaseFeed<RssItem> { + constructor(options: FeedOptions) { + super(options); + } + + build(): string { + const xmlParts: string[] = [ + `<?xml version="1.0" encoding="UTF-8"?>\n`, + `<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:wfw="http://wellformedweb.org/CommentAPI/">\n`, + ` <channel>\n`, + ` <title><![CDATA[${escapeXML(this.options.title)}]]></title>\n`, + ` <description><![CDATA[${escapeXML(this.options.description)}]]></description>\n`, + ` <link>${escapeXML(this.options.link)}</link>\n`, + ` <lastBuildDate>${this.options.updated?.toUTCString()}</lastBuildDate>\n`, + ` <language>${this.options.language || "en-US"}</language>\n`, + ` <generator>${escapeXML(this.options.generator || "the webb blog")}</generator>\n`, + ` <atom:link href="${escapeXML(this.options.link)}" rel="self" type="application/rss+xml"/>\n` + ]; + + if (this.options.authors.length > 0) { + const authorXml = this.options.authors.map((author) => { + const escapedEmail = author.email ? escapeXML(author.email) : ""; + const escapedName = author.name ? escapeXML(author.name) : ""; + const emailPart = escapedEmail ? `${escapedEmail} ` : ""; + + return ( + ` <webMaster>${emailPart}(${escapedName})</webMaster>\n` + + ` <managingEditor>${emailPart}(${escapedName})</managingEditor>\n` + ); + }).join(""); + + xmlParts.push(authorXml); + } + + if (this.items.length > 0) { + const itemsXml = this.items.map((item) => { + const contentXml = item.content ? + ` <content:encoded><![CDATA[${escapeXML(item.content.body)}]]></content:encoded>\n` : + ""; + + const imageXml = item.image ? + ` <media:thumbnail url="${escapeXML(item.image)}" />\n` : + ""; + + return ( + ` <item>\n` + + ` <title>${escapeXML(item.title)}</title>\n` + + ` <link>${escapeXML(item.link)}</link>\n` + + ` <guid>${escapeXML(item.id)}</guid>\n` + + ` <pubDate>${item.updated?.toUTCString() || new Date().toUTCString()}</pubDate>\n` + + ` <description>${escapeXML(item.description)}</description>\n` + + contentXml + + imageXml + + ` </item>\n` + ); + }).join(""); + + xmlParts.push(itemsXml); + } + + xmlParts.push(` </channel>\n`, `</rss>`); + return xmlParts.join(""); + } +} |
