From 8c34d810af95fae0ef846f54370a8c88bfab7123 Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 14:24:49 -0700 Subject: initial commit --- src/utility/feed/rss.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/utility/feed/rss.ts (limited to 'src/utility/feed/rss.ts') 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 { + constructor(options: FeedOptions) { + super(options); + } + + build(): string { + const xmlParts: string[] = [ + `\n`, + `\n`, + ` \n`, + ` <![CDATA[${escapeXML(this.options.title)}]]>\n`, + ` \n`, + ` ${escapeXML(this.options.link)}\n`, + ` ${this.options.updated?.toUTCString()}\n`, + ` ${this.options.language || "en-US"}\n`, + ` ${escapeXML(this.options.generator || "the webb blog")}\n`, + ` \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 ( + ` ${emailPart}(${escapedName})\n` + + ` ${emailPart}(${escapedName})\n` + ); + }).join(""); + + xmlParts.push(authorXml); + } + + if (this.items.length > 0) { + const itemsXml = this.items.map((item) => { + const contentXml = item.content ? + ` \n` : + ""; + + const imageXml = item.image ? + ` \n` : + ""; + + return ( + ` \n` + + ` ${escapeXML(item.title)}\n` + + ` ${escapeXML(item.link)}\n` + + ` ${escapeXML(item.id)}\n` + + ` ${item.updated?.toUTCString() || new Date().toUTCString()}\n` + + ` ${escapeXML(item.description)}\n` + + contentXml + + imageXml + + ` \n` + ); + }).join(""); + + xmlParts.push(itemsXml); + } + + xmlParts.push(` \n`, ``); + return xmlParts.join(""); + } +} -- cgit v1.2.3