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
import { basicCss } from "https://esm.town/v/easrng/basicCss";
import { upgradeExpress } from "https://esm.town/v/easrng/upgradeExpress";
import { type Component } from "npm:@easrng/elements@0.1.15";
import { stream } from "npm:@easrng/elements@0.1.15/server";
import { parseHTML } from "npm:linkedom@0.18.4";
const { document, Node } = parseHTML(`<!DOCTYPE html>`);
import { cohostSanitize } from "https://esm.town/v/easrng/cohostSanitize";
import { extractFromJson, extractFromXml } from "https://esm.town/v/easrng/patchedFeedExtractor";
import TimeAgo from "npm:javascript-time-ago";
import en from "npm:javascript-time-ago/locale/en";
TimeAgo.addDefaultLocale(en);
const timeAgo = new TimeAgo("en");
interface FeedEntry {
id: string;
link?: string;
title?: string;
htmlContent?: string;
published?: Date;
}
interface FeedData {
link?: string;
title?: string;
description?: string;
generator?: string;
language?: string;
published?: Date;
entries?: Array<FeedEntry>;
error?: true;
url: string;
}
const extract = async (url, options = {}, fetchOptions = {}) => {
const retrieve_default = async (url, options: any = {}) => {
const {
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0",
},
} = options;
const res = await fetch(url, { headers, redirect: "follow" });
const status = res.status;
if (status >= 400) {
throw new Error(`Request failed with error code ${status}`);
}
const contentType = res.headers.get("content-type");
const text = await res.text();
if (/(\+|\/)(xml|html)/.test(contentType)) {
return { type: "xml", text: text.trim(), status, contentType };
}
if (/(\+|\/)json/.test(contentType)) {
try {
const data = JSON.parse(text);
return { type: "json", json: data, status, contentType };
}
catch (err) {
throw new Error("Failed to convert data to JSON object");
}
}
throw new Error(`Invalid content type: ${contentType}`);
};
const data = await retrieve_default(url, fetchOptions);
if (!data.text && !data.json) {
throw new Error(`Failed to load content from "${url}"`);
}
const { type, json, text } = data;
const feed = type === "json"
? extractFromJson(json, options)
: extractFromXml(text, options);
return feed;
};
const Entry: Component<{ feed: FeedData; entry: FeedEntry }> = ({ feed, entry, html }) => {
return html`<li>
<article
id=${entry.id}
class="card"
...${feed.language ? { lang: feed.language } : {}}
>
<header>
<b class="author">
${
entry.link
? html`<a target="_blank" href=${entry.link}>${entry.title}</a>`
: entry.title
}
</b>
<div style="color:graytext">
${
feed.link
? html`<a
target="_blank"
style="color:inherit;text-decoration:none"
href=${feed.link}
>${feed.title}</a
>`
: feed.title
}
${entry.published && " - " + timeAgo.format(entry.published)}
</div>
</header>
<div class="content">${cohostSanitize(entry.htmlContent, document)}</div>
</article>
</li>`;