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
import { parseCodepen } from "https://esm.town/v/g/parseCodepen";
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const id = url.pathname.slice(1);
if (!id) return new Response("go to /{id}");
if (!/^[A-Za-z0-9]+$/.test(id)) return new Response("invalid id");
try {
const pen = await parseCodepen(id);
return new Response(
`
<!DOCTYPE html>
<html>
<head>
<title>${pen.title}</title>
<style>
${pen.css}
</style>
</head>
<body>
${pen.body}
<script>
${pen.js}
</script>
</body>
</html>
`.slice(1, -1),
{
headers: {
"Content-Type": "text/html; charset=UTF-8",
},
},
);
} catch (err) {
return new Response("error: " + err.message);
}
}