-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrss-feed-parser.ts
More file actions
192 lines (166 loc) · 5.52 KB
/
rss-feed-parser.ts
File metadata and controls
192 lines (166 loc) · 5.52 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
import { sanitizeExternalUrl, sanitizeImageUrl } from '@/app/utils/url-security';
import type { RSSItem } from './types';
const stripHtml = (value: string): string =>
value
.replace(/<[^>]+>/g, ' ')
.replace(/\s+/g, ' ')
.trim();
const readNodeText = (element: Element | null | undefined): string | undefined => {
const value = element?.textContent?.trim();
return value ? value : undefined;
};
const readFirstText = (root: Element, selectors: string[]): string | undefined => {
for (const selector of selectors) {
const match = root.querySelector(selector);
const value = readNodeText(match);
if (value) {
return value;
}
}
return undefined;
};
function formatRelativeFromNow(
publishedAt: Date,
formatRelativeTime: (value: number, unit: Intl.RelativeTimeFormatUnit) => string
) {
const diffMs = publishedAt.getTime() - Date.now();
const diffMinutes = Math.round(diffMs / (1000 * 60));
if (Math.abs(diffMinutes) < 60) {
return formatRelativeTime(diffMinutes, 'minute');
}
const diffHours = Math.round(diffMinutes / 60);
if (Math.abs(diffHours) < 24) {
return formatRelativeTime(diffHours, 'hour');
}
const diffDays = Math.round(diffHours / 24);
if (Math.abs(diffDays) < 30) {
return formatRelativeTime(diffDays, 'day');
}
const diffMonths = Math.round(diffDays / 30);
if (Math.abs(diffMonths) < 12) {
return formatRelativeTime(diffMonths, 'month');
}
const diffYears = Math.round(diffMonths / 12);
return formatRelativeTime(diffYears, 'year');
}
export function toRSSItem({
id,
title,
providerName,
link,
fallbackRecentLabel,
publishedAtRaw,
excerpt,
imageUrl,
formatRelativeTime,
}: {
id: string;
title: string;
providerName: string;
link: string;
fallbackRecentLabel: string;
publishedAtRaw?: string;
excerpt?: string;
imageUrl?: string;
formatRelativeTime: (value: number, unit: Intl.RelativeTimeFormatUnit) => string;
}): RSSItem {
const safeLink = sanitizeExternalUrl(link) ?? '#';
const safeImageUrl = sanitizeImageUrl(imageUrl);
const publishedAt = publishedAtRaw ? new Date(publishedAtRaw) : null;
const publishedAtMs = publishedAt?.getTime();
const hasValidPublishedAt = typeof publishedAtMs === 'number' && !Number.isNaN(publishedAtMs);
let timeAgo = fallbackRecentLabel;
if (hasValidPublishedAt && publishedAt) {
timeAgo = formatRelativeFromNow(publishedAt, formatRelativeTime);
}
return {
id,
title,
source: providerName,
timeAgo,
url: safeLink,
excerpt: excerpt ? stripHtml(excerpt) : undefined,
imageUrl: safeImageUrl ?? undefined,
publishedAtMs: hasValidPublishedAt ? publishedAtMs : undefined,
};
}
export function parseRSSDocument(
xml: string,
providerName: string,
fallbackRecentLabel: string,
formatRelativeTime: (value: number, unit: Intl.RelativeTimeFormatUnit) => string
): RSSItem[] {
const parser = new DOMParser();
const document = parser.parseFromString(xml, 'application/xml');
const parserError = document.querySelector('parsererror');
if (parserError) {
throw new Error('invalid-feed-format');
}
const rssItems = Array.from(document.querySelectorAll('rss > channel > item'));
if (rssItems.length > 0) {
return rssItems
.map((item) => {
const link = readFirstText(item, ['link']) ?? readFirstText(item, ['guid']);
const title = readFirstText(item, ['title']);
if (!link || !title) {
return null;
}
const mediaContent = item.querySelector('media\\:content, content');
const mediaThumbnail = item.querySelector('media\\:thumbnail, thumbnail');
return toRSSItem({
id: readFirstText(item, ['guid']) ?? link,
title,
providerName,
link,
fallbackRecentLabel,
formatRelativeTime,
publishedAtRaw: readFirstText(item, ['pubDate', 'dc\\:date']),
excerpt: readFirstText(item, ['description', 'content\\:encoded']),
imageUrl:
mediaContent?.getAttribute('url') ?? mediaThumbnail?.getAttribute('url') ?? undefined,
});
})
.filter((item): item is RSSItem => item !== null);
}
const atomEntries = Array.from(document.querySelectorAll('feed > entry'));
return atomEntries
.map((entry) => {
const link =
entry.querySelector('link[rel="alternate"]')?.getAttribute('href') ??
entry.querySelector('link')?.getAttribute('href') ??
undefined;
const title = readFirstText(entry, ['title']);
if (!link || !title) {
return null;
}
return toRSSItem({
id: readFirstText(entry, ['id']) ?? link,
title,
providerName,
link,
fallbackRecentLabel,
formatRelativeTime,
publishedAtRaw: readFirstText(entry, ['published', 'updated']),
excerpt: readFirstText(entry, ['summary', 'content']),
});
})
.filter((item): item is RSSItem => item !== null);
}
export const dedupeItems = (items: RSSItem[]): RSSItem[] =>
items.filter(
(item, index, allItems) =>
allItems.findIndex((candidate) => candidate.url === item.url) === index
);
export const sortItemsByPublishedAt = (items: RSSItem[]): RSSItem[] =>
[...items].sort((left, right) => {
if (typeof left.publishedAtMs === 'number' && typeof right.publishedAtMs === 'number') {
return right.publishedAtMs - left.publishedAtMs;
}
if (typeof left.publishedAtMs === 'number') {
return -1;
}
if (typeof right.publishedAtMs === 'number') {
return 1;
}
return 0;
});