-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-rss.js
106 lines (78 loc) · 3.03 KB
/
create-rss.js
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
// RSS stands for Really Simple Syndication
module.exports = class CreateRSS {
constructor() {
// Create local variables
this.channel = {};
this.items = [];
// Fill the options with default values
this.setOptions({});
}
setChannel(title, link, description, language) {
this.setTitle(title);
this.setLink(link);
this.setDescription(description);
this.setLanguage(language);
return this;
}
setOptions(options) {
if (!options) throw new Error("Options are required");
this.channel.title = options.title ?? "RSS Feed";
this.channel.link = options.link ?? "https://example.com";
this.channel.description = options.description ?? "RSS Feed";
this.channel.language = options.language ?? "en-us";
return this;
}
setTitle(title) {
if (!title) throw new Error("Title is required");
this.channel.title = title;
return this;
}
setLink(link) {
if (!link) throw new Error("Link is required");
this.channel.link = link;
return this;
}
setDescription(description) {
if (!description) throw new Error("Description is required");
this.channel.description = description;
return this;
}
setLanguage(language) {
if (!language) throw new Error("Language is required");
this.channel.language = language;
return this;
}
addItem(title, link, description) {
if (!title) throw new Error("Title is required");
if (!link) link = "";
this.items.push({
title: title,
link: link ?? "",
description: description ?? ""
});
return this;
}
addItems(items) {
if (!items) throw new Error("Items are required");
for (let i = 0; i < items.length; i++)
this.addItem(items[i].title, items[i].link, items[i].description);
return this;
}
generateItems() {
let temp = "";
for (let i = 0; i < this.items.length; i++)
temp += "\t\t<item>\n\t\t\t<title>" + this.items[i].title + "</title>\n\t\t\t<link>" + this.items[i].link + "</link>\n\t\t\t<description>" + this.items[i].description + "</description>\n\t\t</item>\n";
return temp;
}
generate() {
// Create the RSS header
let temp = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\">";
// Open the channel tag
temp += "\n\t<channel>\n\t\t<title>" + this.channel.title + "</title>\n\t\t<link>" + this.channel.link + "</link>\n\t\t<description>" + this.channel.description + "</description>\n\t\t<language>" + this.channel.language + "</language>\n";
// Loop through the items and add them to the RSS feed
temp += this.generateItems();
// Close the channel and rss tags
temp += "\t</channel>\n</rss>";
return temp;
}
};