diff options
Diffstat (limited to 'src/utility/feed/helper.ts')
| -rw-r--r-- | src/utility/feed/helper.ts | 67 |
1 files changed, 67 insertions, 0 deletions
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<T> { + protected categories: Set<string>; + protected items: Array<T>; + 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); + } +} |
