summaryrefslogtreecommitdiff
path: root/src/utility/feed/helper.ts
blob: 0177364a5896ff9a44bd941758da02bf0792c38a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 } = {
    "<": "&lt;",
    ">": "&gt;",
    "&": "&amp;",
    "'": "&apos;",
    '"': "&quot;"
  };

  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);
  }
}