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
import { DOMParser, Element } from "jsr:@b-fuze/deno-dom";
const getText = async url => (await fetch(url)).text();
export default async function(req: Request): Promise<Response> {
if (req.headers.get("referer")?.includes("val.")) {
return Response.json({ error: "Cannot access from val.town" }, {
status: 400,
});
}
if (new URL(req.url).pathname === "/favicon.ico") {
return new Response(null, { status: 404 });
}
const params = new URL(req.url).searchParams;
try {
const feed = await getText("https://kagi.com/api/v1/smallweb/feed/");
const parser = new DOMParser();
const atom = parser.parseFromString(feed, "text/html");
const $feed = atom.querySelector("feed");
const entries = $feed.querySelectorAll("entry");
Array.from(entries).forEach($entry => {
$feed.removeChild($entry);
});
Array.from(entries).slice(0, parseInt(params.get("limit"), 10) || 25).forEach(($entry: Element) => {
$feed.appendChild($entry);
});
return new Response(
feed.split("\n")[0]
+ $feed.outerHTML.replace(/\s{2,}/g, "\n").replace(/<link href="(.*)" ?>/g, "<link href=\"$1\"/>").replaceAll(
"&nbsp;",
"&#160;",
),
{
headers: {
"content-type": params.get("raw") ? "text/xml" : "application/atom+xml",
},
},
);
} catch (err) {
return Response.json({ error: err.message }, {
status: 400,
});
}
}