nbbaier-readabilityhttpproxy.web.val.run
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
import { resetStyle } from "https://esm.town/v/nbbaier/resetStyle";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=6";
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
import { Readability } from "npm:@mozilla/readability";
// @ts-expect-error
import jsdom from "npm:jsdom";
type Article = {
title: string;
content: string;
textContent: string;
length: number;
excerpt: string;
byline: string;
dir: string;
siteName: string;
lang: string;
publishedTime: string;
};
const pageShell = (title: string, pageContent: string) => {
return (`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+3:ital,wght@0,200..900;1,200..900&display=swap" rel="stylesheet">
<title>${title}</title>
<style>
${resetStyle}
html {
font-family: "Source Sans 3", system-ui, sans-serif;
padding: 0.75rem;
margin: auto;
max-width: 90ch;
}
.title-container {
display: flex;
flex-direction: column;
margin-block-end: 0.25rem
}
.subhead {
margin-block-start: 0.5rem;
color: gray;
}
</style>
</head>
<body>
${pageContent}
</body>
</html>
`);
};
export default async function(req: Request): Promise<Response> {
const JSDOM = jsdom.JSDOM;
const url = new URL(req.url);
const articleUrl = url.pathname.substring(1);
if (articleUrl === "") {
return html(pageShell(
"VT Reader",
`
<h1>Val Town Reader</h1>
<p>Enter a url below to get a reader view using <a href="https://github.com/mozilla/readability">Readability.js</a></p>
<form style="display: flex; gap: .25rem; margin-block-start:1rem;" id="nameForm">
<input type="text" id="name" name="name" placeholder="Enter a url" required>
<br>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById('nameForm').onsubmit = function(event) {
event.preventDefault();
const targetURL = document.getElementById("name").value
const newURL = "${url.origin}" + "/" + targetURL
window.location = newURL
};
</script>
</body> `,
));
}
let body = await fetchText(articleUrl);
let doc = new JSDOM(body);
let reader = new Readability(doc.window.document);
let article = reader.parse();
let title = article.title;
return html(pageShell(
title,
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v125
March 29, 2024