aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/vendor/rss/rss.ts
blob: f44a6d22563df138d679bc810f2f1140a9f12d66 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { XMLElement, XMLRootElement, XMLTagType, XMLText } from './xml';

class XMLElementWithRSSUtil extends XMLElement {
  protected replaceChildByTagName(tag: string) {
    const existingEl = this.findElementChild(v => v.tagName === tag);
    if (existingEl) {
      existingEl.children.length = 0;
      existingEl.attributes.clear();
    }
    const el = existingEl ?? new XMLElement(tag);
    if (!existingEl)
      this.child(el)
    return el
  }
}
export class RSSItemElement extends XMLElementWithRSSUtil {
  public constructor(title: string | null, description: string | null, link: string | URL | null) {
    super("item");
    if (title !== null)
      this.title(title);
    if (description !== null)
      this.description(description)
    if (link !== null)
      this.link(link)
  }
  /** Replaces the title of the item */
  public title(title: string) {
    this.replaceChildByTagName('title').child(new XMLText(title))
    return this;
  }
  /** Replaces the link to the item */
  public link(link: string | URL) {
    this.replaceChildByTagName('link').child(new XMLText(typeof link === 'string' ? link : link.href))
    return this;
  }
  /** Sets the description of the item, replacing it if it already exists */
  public description(description: string) {
    this.replaceChildByTagName('description').child(new XMLText(description))
    return this;
  }
  /** Sets the URL for the comment section of an item, replacing it if it already exists */
  public comments(comments: string | URL) {
    return this.child(new XMLElement('comments').child(new XMLText(typeof comments === 'string' ? comments : comments.href)));
  }
  /** Sets the timestamp where the most recent item in the channel was published, replacing it if it already exists */
  public pubDate(pubDate: Date) {
    this.replaceChildByTagName('pubDate').child(new XMLText(pubDate.toUTCString()))
    return this;
  }
  /** Specifies a third-party source for the item. */
  public source(sourceName: string, sourceUrl: string | URL) {
    return this.child(new XMLElement('source').attribute('url', typeof sourceUrl === 'string' ? sourceUrl : sourceUrl.href).child(new XMLText(sourceName)))
  }
  /** Adds a category to the post */
  public category(
    category: string,
  ) {
    return this.child(new XMLElement('category').child(new XMLText(category)));
  }
  /** Adds a author to the post */
  public author(
    author: string,
  ) {
    return this.child(new XMLElement('author').child(new XMLText(author)));
  }
  /** Adds a unique ID to the post. This ID must be globally unique, but has no format specifications. */
  public guid(
    id: string,
    /** If this is a permanent link to the post. If true, this must be a URL that permanently points to this item. */
    isPermaLink: boolean
  ) {
    return this.child(new XMLElement('guid').child(new XMLText(id)).attribute('isPermaLink', isPermaLink ? 'true' : 'false'));
  }
  /** Adds a media file to be included with an item */
  public enclosure(
    url: string,
    mimeType: string,
    /** in bytes */
    length: number
  ) {
    return this.child(
      new XMLElement('enclosure')
        .attribute('url', url)
        .attribute('length', length.toString())
        .attribute('type', mimeType)
    )
  }
}
export class RSSChannelElement extends XMLElementWithRSSUtil {
  public constructor(title: string | null, description: string | null, link: URL | string | null) {
    super("channel");
    if (title !== null)
      this.title(title);
    if (description !== null)
      this.description(description)
    if (link !== null)
      this.link(link)
  }
  /** Replaces the title of the channel */
  public title(title: string) {
    this.replaceChildByTagName('title').child(new XMLText(title))
    return this;
  }
  /** Replaces the link to the channel */
  public link(link: string | URL) {
    this.replaceChildByTagName('link').child(new XMLText(typeof link === 'string' ? link : link.href))
    return this;
  }
  /** Sets the description of the channel, replacing it if it already exists */
  public description(description: string) {
    this.replaceChildByTagName('description').child(new XMLText(description))
    return this;
  }
  /** Sets the timestamp where the most recent item in the channel was published, replacing it if it already exists */
  public pubDate(pubDate: Date) {
    this.replaceChildByTagName('pubDate').child(new XMLText(pubDate.toUTCString()))
    return this;
  }
  /** Sets the timestamp where the RSS file was last modified in any way, replacing it if it already exists */
  public lastBuildDate(lastBuildDate: Date) {
    this.replaceChildByTagName('lastBuildDate').child(new XMLText(lastBuildDate.toUTCString()))
    return this;
  }
  /** Sets the time to live for the channel - the maximum time until clients should re-fetch it, replacing it if it already exists */
  public ttl(ttl: number) {
    this.replaceChildByTagName('ttl').child(new XMLText(ttl.toString()))
    return this;
  }
  /** Adds the copyright information of the channel */
  public copyright(copyright: string) {
    return this.child(new XMLElement('copyright').child(new XMLText(copyright)))
  }
  /** Adds the generator used for this RSS feed - we recommend calling this and keeping it at .rsstrogen() - Can be called multiple times */
  public generator(generator: string = 'rsstrogen') {
    return this.child(new XMLElement('generator').child(