-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.js
More file actions
executable file
·38 lines (32 loc) · 1015 Bytes
/
readability.js
File metadata and controls
executable file
·38 lines (32 loc) · 1015 Bytes
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
#!/usr/bin/env node
import {Readability} from "@mozilla/readability";
import {JSDOM} from "jsdom";
const filepath = process.argv[2] || ""
const encoding = process.argv[3] || "utf-8"
if (filepath) {
processFile(filepath, encoding);
} else {
let input = '';
process.stdin.on('data', (chunk) => {
input += chunk;
});
process.stdin.on('end', () => {
processInput(input, encoding);
});
}
function processFile(filepath, encoding) {
JSDOM.fromFile(filepath, encoding && {contentType: `text/html; charset=${encoding}`}).then(dom => {
processDom(dom)
});
}
function processInput(content, encoding) {
let dom = new JSDOM(content, encoding && {contentType: `text/html; charset=${encoding}`})
processDom(dom)
}
function processDom(dom) {
let article = new Readability(dom.window.document, {debug: false}).parse()
if (article) {
article['charset'] = dom.window.document.characterSet;
}
console.log(JSON.stringify(article, null, 4))
}