Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Get a readable representation of an HTML page

console.log(await readableHtml("<html><body><h1>Main title</h1><p>Some content</p></body></html>")); // Main titleSome content console.log(await readableHtml(new URL("https://en.wikipedia.org/wiki/Special:Random"))); // From Wikipedia, the free encyclopedia // George Miles ChilcottUnited States Senatorfrom ColoradoIn officeApril 17, 1882 – January 27,...
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
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText";
import { Readability } from "npm:@mozilla/readability";
// @ts-expect-error
import jsdom from "npm:jsdom";
const JSDOM = jsdom.JSDOM;
export function readableHtml(urlOrContent: URL | string, options?: RequestInit): Promise<string> {
if (typeof urlOrContent === "string") {
return readableHtmlFromText(urlOrContent);
} else {
return readableHtmlFromUrl(urlOrContent);
}
}
export default readableHtml;
async function readableHtmlFromUrl(url: URL, options?: RequestInit): Promise<string> {
const body = await fetchText(url.toString(), options);
return readableHtmlFromText(body);
}
async function readableHtmlFromText(content: string): Promise<string> {
let doc = new JSDOM(content);
let reader = new Readability(doc.window.document);
let article = reader.parse();
return article.textContent;
}
August 29, 2024