-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
183 lines (144 loc) · 3.83 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
DOMParser,
Element,
type HTMLDocument,
} from "https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts";
import { NodeHtmlMarkdown } from "https://esm.sh/node-html-markdown@1.3.0";
import { Readability } from "https://esm.sh/@mozilla/readability@0.4.4";
import { serve } from "https://deno.land/std@0.182.0/http/server.ts";
import { type Options, renderMarkdown } from "./markdown.ts";
const port = 8080;
const base = Deno.env.get("BASE") ?? `http://localhost:${port}/`;
const mainPage = (await Deno.readTextFile("./assets/index.md")).replaceAll(
"{{base}}",
base,
);
function status(code: number, message: string): Response {
return new Response(JSON.stringify({ message }), {
status: code,
headers: {
"content-type": "application/json; charset=utf-8",
},
});
}
function markdown(markdown: string, options?: Options): Response {
return new Response(
renderMarkdown(markdown, options),
{
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
},
},
);
}
function safeURL(url: string, base?: URL): URL | null {
try {
return new URL(url, base);
} catch {
return null;
}
}
function rebaseLinks(document: HTMLDocument, url: URL) {
const links = document.querySelectorAll("a");
for (const link of links) {
if (!(link instanceof Element)) {
continue;
}
const l = link as Element;
const href = l.getAttribute("href");
if (!href) {
continue;
}
const url = safeURL(href);
if (!url) {
continue;
}
if (url.origin === base) {
continue;
}
l.setAttribute("href", `${base}${url}`);
}
const images = document.querySelectorAll("img");
for (const image of images) {
// set the src of image's URL's base to the base of the requested URL
if (!(image instanceof Element)) {
continue;
}
const i = image as Element;
const src = i.getAttribute("src");
if (!src) {
continue;
}
if (src.startsWith("http")) {
continue;
}
const newURL = safeURL(src, url);
if (!newURL) {
continue;
}
i.setAttribute("src", newURL.href);
}
}
const handler = async (request: Request): Promise<Response> => {
const url = new URL(request.url);
// main page
if (url.pathname === "/") {
return markdown(mainPage, {
disableHtmlSanitization: true,
title: "MDSite",
description: "Convert any webpage to markdown",
});
}
const requestedURL = safeURL(url.pathname.slice(1));
if (!requestedURL) {
return status(400, "Invalid URL");
}
const response = await fetch(requestedURL);
if (!response.ok) {
return status(response.status, response.statusText);
}
const html = await response.text();
const doc = new DOMParser().parseFromString(html, "text/html");
if (!doc) {
return status(500, "Unable to parse HTML");
}
if (!url.searchParams.has("no-rebase")) {
rebaseLinks(doc, requestedURL);
}
const reader = new Readability(doc as unknown as Document);
const article = reader.parse();
if (!article) {
return status(500, "Unable to parse article");
}
if (url.searchParams.has("html")) {
return new Response(article.content, {
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
},
});
}
const renderedMarkdown = NodeHtmlMarkdown.translate(article.content);
if (url.searchParams.has("raw")) {
return new Response(
`# ${article.title}\n
${renderedMarkdown}`,
{
status: 200,
headers: {
"content-type": "text/plain; charset=utf-8",
},
},
);
}
return markdown(
`# ${article.title} ([original](${requestedURL})) ([raw](?raw))\n\n
${renderedMarkdown}`,
{
title: article.title,
description: article.excerpt,
},
);
};
await serve(handler, { port });