forked from justjavac/deno_deploy_readme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
77 lines (67 loc) · 2.29 KB
/
mod.ts
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
import { html } from "https://deno.land/x/html/mod.ts";
import unified from "https://esm.sh/unified";
import markdown from "https://esm.sh/remark-parse";
import remark2rehype from "https://esm.sh/remark-rehype";
import externalLinks from "https://esm.sh/remark-external-links";
import stringify from "https://esm.sh/rehype-stringify";
function getUrl(fileName: string) {
if (!Deno.env.get("GITHUB_REPO")) {
return new URL(fileName, import.meta.url);
}
const repo = Deno.env.get("GITHUB_REPO");
const branch = Deno.env.get("GITHUB_BRANCH") ?? "main";
return `https://raw.githubusercontent.com/${repo}/${branch}/${fileName}`;
}
async function handleRequest(request: Request) {
const { pathname } = new URL(request.url);
if (pathname.startsWith("/assets/style.css")) {
const style = new URL("assets/style.css", import.meta.url);
const response = await fetch(style);
const headers = new Headers(response.headers);
headers.set("content-type", "text/css; charset=utf-8");
return new Response(response.body, { ...response, headers });
}
if (pathname.startsWith("/assets")) {
const favicon = new URL(pathname.substr(1), import.meta.url);
return fetch(favicon);
}
const response = await fetch(getUrl("README.md"));
const content = await response.text();
const output = await unified()
.use(markdown)
.use(externalLinks)
.use(remark2rehype)
.use(stringify)
.process(content);
return new Response(
html`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1"
/>
<title>Deno Deploy Readme</title>
<link rel="stylesheet" href="/assets/style.css" />
</head>
<body>
<div class="container-lg px-3 my-5 markdown-body">
${output.toString()}
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/anchor-js/4.2.2/anchor.min.js"></script>
<script>
anchors.add();
</script>
</body>
</html>`,
{
headers: {
"content-type": "text/html; charset=utf-8",
},
},
);
}
addEventListener("fetch", (event: FetchEvent) => {
event.respondWith(handleRequest(event.request));
});