-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.tsx
executable file
·328 lines (315 loc) · 9.24 KB
/
app.tsx
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env -S deno run --allow-net --watch --unstable-kv app.tsx
/** @jsx jsx */
/** @jsxFrag Fragment */
import { Hono } from "https://deno.land/x/hono@v3.11.7/mod.ts";
import { jsx } from "https://deno.land/x/hono@v3.7.2/middleware.ts";
import {
DOMParser,
HTMLDocument,
} from "https://deno.land/x/deno_dom@v0.1.43/deno-dom-wasm.ts";
import LRU from "https://deno.land/x/lru@1.0.2/mod.ts";
const CACHE = "CACHE";
const expireIn = 1000 * 60 * 60 * 24 * 365; // 1 year
const kv = await Deno.openKv();
const lru = new LRU<Map<string, string>>(100);
async function get(url: string, useCache: boolean) {
const map = new Map<string, string>();
if (url.indexOf(":ogp.deno.dev") > 0) {
return map;
}
try {
if (useCache) {
const memoryCache = lru.get(url);
if (memoryCache) {
console.log("memory");
return memoryCache;
}
const kvCache = await kv.get<Map<string, string>>([CACHE, url]);
if (kvCache.value) {
lru.set(url, kvCache.value);
console.log("kv");
return kvCache.value;
}
}
console.log("fetch");
const response: Response = await fetch(url);
const parser: DOMParser = new DOMParser();
const document: HTMLDocument | null = parser.parseFromString(
await response.text(),
"text/html",
);
if (!document) {
return map;
}
document.getElementsByTagName("meta").forEach((a) => {
const name = a.attributes.getNamedItem("name");
if (name && name.value?.startsWith("twitter:")) {
const content = a.attributes.getNamedItem("content");
if (content) {
map.set(name.value, content.value);
}
}
const property = a.attributes.getNamedItem("property");
if (property && property.value?.startsWith("og:")) {
const content = a.attributes.getNamedItem("content");
if (content) {
map.set(property.value, content.value);
}
}
});
document.getElementsByTagName("title").forEach((e) => {
map.set("title", e.textContent);
});
let favicon = document.querySelector("link[rel='icon']")?.getAttribute(
"href",
);
if (!favicon) {
// Deprecated
favicon = document.querySelector("link[rel='shortcut icon']")?.getAttribute(
"href",
);
}
if (!favicon) {
const u = new URL(url);
u.pathname = "/favicon.ico";
u.search = "";
u.hash = "";
favicon = u.toString();
} else if (favicon.startsWith("/")) {
const u = new URL(url);
const original = new URL("https://example.com" + favicon);
u.pathname = original.pathname;
u.search = original.search;
u.hash = "";
favicon = u.toString();
}
map.set("favicon", favicon);
lru.set(url, map);
kv.set([CACHE, url], map, { expireIn });
return map;
} catch (error) {
return map;
}
}
type Attr = {
title: string;
description: string;
url: string;
image: string;
site: string;
favicon?: string;
};
function Large({ title, url, image, site }: Attr) {
return (
<div class="card" style="max-width: 500px; height: 350px">
{image &&
(
<img
src={image}
class="card-img-top"
alt={site}
style="max-width: 100%"
/>
)}
<div
class="card-body"
style="overflow: hidden;;"
>
<p class="card-text">
<small class="text-muted">
<a
href={encodeURI(url)}
class="stretched-link"
style="text-decoration: none;color: black;"
target="_blank"
>
{new URL(url).hostname}
</a>
</small>
</p>
{title &&
(
<div
class="card-title"
style="max-width: 500px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
>
{title}
</div>
)}
{
/* {description &&
<p class="card-text">{description}</p>
} */
}
</div>
</div>
);
}
function Small({ title, description, url, favicon }: Attr) {
return (
<div class="card" style="height: 150px; width: 100%">
<div class="card-body" style="overflow: hidden; ">
<h5
class="card-title"
style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
>
<img src={favicon} style="height: 25px; margin: 5px" />
{title}
</h5>
<p
class="card-text"
style="max-width: 100%;white-space: nowrap; overflow: hidden; text-overflow: ellipsis;"
>
{description}
</p>
<p class="card-text">
<small class="text-muted">
<a href={encodeURI(url)} class="stretched-link" target="_blank">
{new URL(url).hostname}
</a>
</small>
</p>
</div>
</div>
);
}
function App(
{ map, url, size }: {
map: Map<string, string>;
url: string;
size: string | null;
},
) {
let image = map.get("og:image") || map.get("twitter:image:src") || "";
if (image.startsWith("/")) {
const u = new URL(url);
image = u.protocol + "//" + u.host + image;
}
const title = map.get("og:title") || map.get("twitter:title") || map.get("title") || "";
const description = map.get("og:description") ||
map.get("twitter:description") || "";
const site = map.get("og:site_name") || map.get("twitter:site") || "";
const favicon = map.get("favicon");
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossOrigin="anonymous"
>
</link>
</head>
<body>
{size === "small"
? (
<Small
title={title}
description={description}
url={url}
site={site}
image={image}
favicon={favicon}
/>
)
: (
<Large
title={title}
description={description}
url={url}
site={site}
image={image}
/>
)}
</body>
</html>
);
}
function Default({ baseUrl }: { baseUrl: string }) {
const exampleUrlLarge = baseUrl + "/?size=large&url=https://github.com";
const iframeLarge =
`<iframe src="${exampleUrlLarge}" height="350" width="500"></iframe>`;
const exampleUrlSmall = baseUrl + "/?size=small&url=https://github.com";
const iframeSmall =
`<iframe src="${exampleUrlSmall}" height="150" style="width: 100%;"></iframe>`;
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Open Graph Preview</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossOrigin="anonymous"
>
</link>
</head>
<body>
<div class="container">
<h1>Open Graph Preview</h1>
<div class="alert alert-primary" role="alert">
Specify "url" Parameter
</div>
<h2>Large Example</h2>
<h3>URL</h3>
<div>
<a href={exampleUrlLarge} target="_blank">{exampleUrlLarge}</a>
</div>
<h3>Embed Code</h3>
<div class="card">
<div class="card-body" style="padding-bottom: 0px">
<pre><code>{iframeLarge}</code></pre>
</div>
</div>
<h3>Preview</h3>
<div dangerouslySetInnerHTML={{ __html: iframeLarge }}></div>
<h2>Small Example</h2>
<h3>URL</h3>
<div>
<a href={exampleUrlSmall} target="_blank">{exampleUrlSmall}</a>
</div>
<h3>Embed Code</h3>
<div class="card">
<div class="card-body" style="padding-bottom: 0px">
<pre><code>{iframeSmall}</code></pre>
</div>
</div>
<h3>Preview</h3>
<div dangerouslySetInnerHTML={{ __html: iframeSmall }}></div>
</div>
<br />
</body>
</html>
);
}
const NotFound = () => (
<div>
<h1>Page not found</h1>
</div>
);
const app = new Hono();
app.get("/", async (c) => {
const requestUrl = new URL(c.req.url);
const url = requestUrl.searchParams.get("url");
const size = requestUrl.searchParams.get("size");
if (!url) {
return c.html(<Default baseUrl={requestUrl.origin} />);
}
let map: Map<string, string>;
if (new URL(url).hostname === requestUrl.hostname) {
map = new Map<string, string>();
} else {
const useCache = c.req.header("Cache-Control") !== "no-cache";
map = await get(url, useCache);
}
return c.html(<App map={map} url={url} size={size} />, {
headers: { "Cache-Control": "max-age=60" },
});
})
.notFound((c) => c.html(<NotFound />));
Deno.serve(app.fetch);