-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss-parser.js
More file actions
296 lines (264 loc) · 8.61 KB
/
Copy pathrss-parser.js
File metadata and controls
296 lines (264 loc) · 8.61 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import https from 'https';
import http from 'http';
/**
* Simple RSS/Atom feed parser that converts XML to JSON
* No external dependencies required
*/
/**
* Fetch RSS feed from URL
* @param {string} url - The feed URL
* @returns {Promise<string>} - The feed XML content
*/
async function fetchFeed(url, maxRedirects = 5) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
const options = {
headers: {
'User-Agent': 'Newsflash/1.0 RSS Reader',
'Accept': 'application/rss+xml, application/atom+xml, application/xml, text/xml, */*',
},
};
const req = client.get(url, options, (res) => {
if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
if (maxRedirects <= 0) {
reject(new Error('Too many redirects'));
return;
}
const redirectUrl = new URL(res.headers.location, url).href;
fetchFeed(redirectUrl, maxRedirects - 1).then(resolve, reject);
return;
}
if (res.statusCode !== 200) {
reject(new Error(`Failed to fetch feed: ${res.statusCode}`));
return;
}
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => resolve(data));
}).on('error', reject);
// Set timeout
req.setTimeout(10000, () => {
req.destroy();
reject(new Error('Request timeout'));
});
});
}
/**
* Extract text content from XML tag
* @param {string} xml - The XML string
* @param {string} tag - The tag name
* @returns {string|null} - The text content or null
*/
function extractTag(xml, tag) {
const regex = new RegExp(`<${tag}[^>]*>([\\s\\S]*?)<\/${tag}>`, 'i');
const match = xml.match(regex);
if (!match) return null;
// Handle CDATA
let content = match[1];
const cdataMatch = content.match(/<!\[CDATA\[([\s\S]*?)\]\]>/);
if (cdataMatch) {
content = cdataMatch[1];
}
// Decode HTML entities
return decodeEntities(content.trim());
}
/**
* Extract attribute from XML tag
* @param {string} xml - The XML string
* @param {string} tag - The tag name
* @param {string} attr - The attribute name
* @returns {string|null} - The attribute value or null
*/
function extractAttribute(xml, tag, attr) {
const regex = new RegExp(`<${tag}[^>]*${attr}=["']([^"']+)["']`, 'i');
const match = xml.match(regex);
return match ? match[1] : null;
}
/**
* Decode HTML entities
* @param {string} text - The text with entities
* @returns {string} - The decoded text
*/
function decodeEntities(text) {
return text
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/'/g, "'")
.replace(/&/g, '&');
}
/**
* Zero-width characters used by Sanity.io Content Source Maps (stega) to embed
* click-to-edit pointers inside string fields. Some feeds (e.g. Stack Overflow's
* blog, which runs on Sanity) leak these into their RSS output, polluting titles
* and summaries with invisible metadata. This strips them out.
* Chars: U+200B (ZWSP), U+200C (ZWNJ), U+200D (ZWJ), U+FEFF (ZWNBSP/BOM).
*/
const STEGA_REGEX = /[]/g;
/**
* Recursively strip zero-width stega characters from all string values in a value.
* Handles strings, arrays and plain objects; other types are returned unchanged.
* @param {*} value - The value to sanitise
* @returns {*} - The sanitised value
*/
function stripStega(value) {
if (typeof value === 'string') {
return value.replace(STEGA_REGEX, '');
}
if (Array.isArray(value)) {
return value.map(stripStega);
}
if (value && typeof value === 'object') {
for (const key of Object.keys(value)) {
value[key] = stripStega(value[key]);
}
return value;
}
return value;
}
/**
* Parse date string to timestamp
* @param {string} dateStr - The date string
* @returns {number} - The timestamp
*/
function parseDate(dateStr) {
if (!dateStr) return null;
const timestamp = Date.parse(dateStr);
return isNaN(timestamp) ? null : timestamp;
}
/**
* Parse RSS item
* @param {string} itemXml - The item XML
* @returns {Object} - The parsed item
*/
function parseRSSItem(itemXml) {
const title = extractTag(itemXml, 'title');
const description = extractTag(itemXml, 'description') || extractTag(itemXml, 'summary');
const link = extractTag(itemXml, 'link');
const pubDate = extractTag(itemXml, 'pubDate') || extractTag(itemXml, 'published');
const creator = extractTag(itemXml, 'dc:creator') || extractTag(itemXml, 'author');
const guid = extractTag(itemXml, 'guid');
const content = extractTag(itemXml, 'content:encoded') || extractTag(itemXml, 'content');
const published = parseDate(pubDate);
if (published === null) {
const dateTags = {
pubDate: extractTag(itemXml, 'pubDate'),
published: extractTag(itemXml, 'published'),
date: extractTag(itemXml, 'date'),
'dc:date': extractTag(itemXml, 'dc:date'),
updated: extractTag(itemXml, 'updated'),
};
console.warn(`RSS item missing published date: "${title || guid || link}"`, dateTags);
}
return {
id: guid || link,
title: title || '',
description: description || '',
link: link || '',
author: creator || '',
published,
created: Date.now(),
category: [],
content: content || '',
enclosures: [],
media: {}
};
}
/**
* Parse Atom entry
* @param {string} entryXml - The entry XML
* @returns {Object} - The parsed entry
*/
function parseAtomEntry(entryXml) {
const title = extractTag(entryXml, 'title');
const summary = extractTag(entryXml, 'summary');
const content = extractTag(entryXml, 'content');
const linkHref = extractAttribute(entryXml, 'link', 'href');
const id = extractTag(entryXml, 'id');
const published = extractTag(entryXml, 'published');
const updated = extractTag(entryXml, 'updated');
// Extract author name
const authorMatch = entryXml.match(/<author>([\s\S]*?)<\/author>/i);
let author = '';
if (authorMatch) {
author = extractTag(authorMatch[1], 'name') || '';
}
const publishedDate = parseDate(published);
if (publishedDate === null) {
const dateTags = {
published,
updated,
issued: extractTag(entryXml, 'issued'),
created: extractTag(entryXml, 'created'),
'dc:date': extractTag(entryXml, 'dc:date'),
};
console.warn(`Atom entry missing published date: "${title || id || linkHref}"`, dateTags);
}
return {
id: id || linkHref,
title: title || '',
description: summary || content || '',
link: linkHref || '',
author: author,
published: publishedDate,
created: parseDate(updated || published),
category: [],
content: content || '',
enclosures: [],
media: {}
};
}
/**
* Parse RSS/Atom feed to JSON
* @param {string} url - The feed URL
* @returns {Promise<Object>} - The parsed feed data
*/
export async function parse(url) {
const xml = await fetchFeed(url);
// Determine feed type
const isAtom = xml.includes('<feed') && xml.includes('xmlns="http://www.w3.org/2005/Atom"');
if (isAtom) {
// Parse Atom feed
const feedMatch = xml.match(/<feed[^>]*>([\s\S]*)<\/feed>/i);
if (!feedMatch) throw new Error('Invalid Atom feed');
const feedContent = feedMatch[1];
const title = extractTag(feedContent, 'title');
const subtitle = extractTag(feedContent, 'subtitle');
const linkHref = extractAttribute(feedContent, 'link', 'href');
// Extract entries
const entryMatches = feedContent.match(/<entry>([\s\S]*?)<\/entry>/gi) || [];
const items = entryMatches.map(parseAtomEntry);
return stripStega({
title: title || '',
description: subtitle || '',
link: linkHref || '',
image: '',
category: [],
items: items
});
} else {
// Parse RSS feed
const channelMatch = xml.match(/<channel>([\s\S]*)<\/channel>/i);
if (!channelMatch) throw new Error('Invalid RSS feed');
const channelContent = channelMatch[1];
const title = extractTag(channelContent, 'title');
const description = extractTag(channelContent, 'description');
const link = extractTag(channelContent, 'link');
const imageTag = extractTag(channelContent, 'image');
const imageUrl = imageTag ? extractTag(imageTag, 'url') : null;
// Extract items
const itemMatches = channelContent.match(/<item>([\s\S]*?)<\/item>/gi) || [];
const items = itemMatches.map(parseRSSItem);
return stripStega({
title: title || '',
description: description || '',
link: link || '',
image: imageUrl || '',
category: [],
items: items
});
}
}
export default { parse };