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/atom.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++ src/utility/feed/helper.ts | 67 +++++++++++++++++++++++++++++++++++ src/utility/feed/index.ts | 12 +++++++ src/utility/feed/json.ts | 41 ++++++++++++++++++++++ src/utility/feed/rss.ts | 85 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 292 insertions(+) create mode 100644 src/utility/feed/atom.ts create mode 100644 src/utility/feed/helper.ts create mode 100644 src/utility/feed/index.ts create mode 100644 src/utility/feed/json.ts create mode 100644 src/utility/feed/rss.ts (limited to 'src/utility/feed') diff --git a/src/utility/feed/atom.ts b/src/utility/feed/atom.ts new file mode 100644 index 0000000..d8827e9 --- /dev/null +++ b/src/utility/feed/atom.ts @@ -0,0 +1,87 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import { BaseFeed, escapeXML, type FeedOptions } from "./helper.ts"; + +interface AtomEntry { + content?: { + body: string; + type?: string; + }; + id: string; + image?: string; + link: string; + summary: string; + title: string; + updated?: Date; +} + +/*** EXPORT ------------------------------------------- ***/ + +export class FeedAtom extends BaseFeed { + constructor(options: FeedOptions) { + super(options); + } + + build(): string { + const xmlParts: string[] = [ + `\n`, + `\n`, + ` ${escapeXML(this.options.title)}\n`, + ` ${escapeXML(this.options.description)}\n`, + ` \n`, + ` \n`, + ` ${this.options.updated?.toISOString()}\n`, + ` ${this.options.generator || "the webb blog"}\n`, + ` ${escapeXML(this.options.link)}\n` + ]; + + for (const author of this.options.authors) { + xmlParts.push(` \n`); + + if (author.name) + xmlParts.push(` ${escapeXML(author.name)}\n`); + + if (author.email) + xmlParts.push(` ${escapeXML(author.email)}\n`); + + if (author.link) + xmlParts.push(` ${escapeXML(author.link)}\n`); + + xmlParts.push(` \n`); + } + + if (this.options.icon) { + xmlParts.push(` ${escapeXML(this.options.icon)}\n`); + xmlParts.push(` ${escapeXML(this.options.icon)}\n`); + } + + if (this.options.feed) + xmlParts.push(` \n`); + + for (const entry of this.items) { + xmlParts.push( + ` \n`, + ` ${escapeXML(entry.title)}\n`, + ` \n`, + ` ${escapeXML(entry.id)}\n`, + ` ${entry.updated?.toISOString() || new Date().toISOString()}\n`, + ` ${escapeXML(entry.summary)}\n`, + ` ${escapeXML(entry.content?.body || entry.summary)}\n`, + entry.image ? + ` \n` : + "", + ` \n` + ); + } + + for (const category of this.categories) { + xmlParts.push(` \n`); + } + + xmlParts.push(""); + return xmlParts.join(""); + } +} diff --git a/src/utility/feed/helper.ts b/src/utility/feed/helper.ts new file mode 100644 index 0000000..0177364 --- /dev/null +++ b/src/utility/feed/helper.ts @@ -0,0 +1,67 @@ + + + +/*** EXPORT ------------------------------------------- ***/ + +export interface FeedOptions { + authors: Array<{ + name: string; + email?: string; + link?: string; + }>; + copyright?: string; + description: string; + feed?: string; + feedLinks?: { + atom?: string; + }; + generator?: string; + icon?: string; + id?: string; + language?: string; + link: string; + title: string; + updated?: Date; +} + +export function escapeXML(unsafe: string): string { + const escapeMap: { [key: string]: string } = { + "<": "<", + ">": ">", + "&": "&", + "'": "'", + '"': """ + }; + + return unsafe.replace(/[<>&'"]/g, (c) => escapeMap[c] || c); +} + +export abstract class BaseFeed { + protected categories: Set; + protected items: Array; + protected options: FeedOptions; + + constructor(options: FeedOptions) { + this.categories = new Set(); + this.items = []; + + this.options = { + ...options, + updated: options.updated || new Date() + }; + } + + abstract build(): string; + + addCategory(category: string) { + this.categories.add(category); + } + + addContributor(contributor: { name: string; email: string; link?: string }) { + this.options.authors.push(contributor); + } + + addItem(item: T) { + this.items.push(item); + } +} diff --git a/src/utility/feed/index.ts b/src/utility/feed/index.ts new file mode 100644 index 0000000..d58609a --- /dev/null +++ b/src/utility/feed/index.ts @@ -0,0 +1,12 @@ + + + +/*** EXPORT ------------------------------------------- ***/ + +export { FeedAtom as ATOM } from "./atom.ts"; +export { FeedJSON as JSON } from "./json.ts"; +export { FeedRSS as RSS } from "./rss.ts"; + + + +/*** forked from https://github.com/GabsEdits/feed ***/ diff --git a/src/utility/feed/json.ts b/src/utility/feed/json.ts new file mode 100644 index 0000000..589aeae --- /dev/null +++ b/src/utility/feed/json.ts @@ -0,0 +1,41 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import { BaseFeed, type FeedOptions } from "./helper.ts"; + +interface JsonItem { + content_html?: string; + date_published?: Date; + id: string; + title: string; + url: string; +} + +/*** EXPORT ------------------------------------------- ***/ + +export class FeedJSON extends BaseFeed { + constructor(options: FeedOptions) { + super(options); + } + + build(): string { + const json: Record = { + feed_url: this.options.feed, + home_page_url: this.options.link, + icon: this.options.icon, + items: this.items.map(({ content_html, date_published, id, title, url }) => ({ + ...(content_html && { content_html }), + date_published: date_published?.toISOString() || new Date().toUTCString, + id, + title, + url + })), + title: this.options.title, + version: "https://jsonfeed.org/version/1" + }; + + return JSON.stringify(json, null, 2); + } +} 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