-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip.js
executable file
·149 lines (128 loc) · 4.29 KB
/
clip.js
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
#!/usr/bin/env node
'use strict';
const { Readability } = require('@mozilla/readability');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const TurndownService = require('turndown');
const url = require('url');
const turndownPluginGfm = require('turndown-plugin-gfm');
const gfm = turndownPluginGfm.gfm;
const turndownService = new TurndownService();
turndownService.use(gfm);
let args = {};
process.argv.forEach((arg, index) => {
switch (arg) {
case "-h":
help();
break;
case "--help":
help();
break;
case "--html":
args.format = "html";
break;
default:
args.page = arg;
}
});
function help() {
console.error(`clippy <URL> [--html]
Usage:
URL - the url to be clipped
--html - format output as html (default is markdown)
--help - this help
`)
process.exit();
}
// TODO: understand why the tables in http://www.lebaneserecipes.com/Tabouleh.htm don't parse correctly in turndown
// custom PRE tag parser, since turndown only parses <pre><code> blocks
turndownService.addRule(
'indented-pre-block',
{
filter: function (node, options) {
return (
options.codeBlockStyle === 'indented' &&
node.nodeName === 'PRE'
);
},
replacement: function (content, node, options) {
return (
'\n\n ' +
node.firstChild.textContent.replace(/\n/g, '\n ') +
'\n\n'
);
}
}
);
async function main(args) {
const dom = await JSDOM.fromURL(args.page);
let imgs = dom.window.document.querySelectorAll("img");
for (let i=0; i < imgs.length; i++) {
let imgSrc = imgs[i].getAttribute("src");
if (imgSrc) {
imgs[i].setAttribute("src", url.resolve(args.page, imgs[i].getAttribute("src")));
}
}
if (args.format == "html"){
for (let i=0; i < imgs.length; i++) {
// replace img src with base64 encoded data block
let dom_node = imgs[i];
const imgSrc = dom_node.getAttribute("src");
dom_node.setAttribute("src", await inlineBase64Img(url.resolve(args.page, imgSrc)));
}
// If you're going to use Readability with untrusted input (whether in HTML or DOM
// form), we strongly recommend you use a sanitizer library like DOMPurify to avoid
// script injection when you use the output of Readability. We would also recommend
// using CSP to add further defense-in-depth restrictions to what you allow the
// resulting content to do. The Firefox integration of reader mode uses both of
// these techniques itself. Sanitizing unsafe content out of the input is explicitly
// not something we aim to do as part of Readability itself - there are other good
// sanitizer libraries out there, use them!
let article = new Readability(dom.window.document).parse();
console.log('<body>');
console.log(`<h3>${article.title}</h3>`);
console.log(`<p>Excerpt: ${article.exceprt} </p>`);
console.log(`<p>Length: ${article.length} </p>`);
console.log(`<p>Byline: ${article.byline} </p>`);
console.log(article.content);
console.log('</body>');
} else {
let article = new Readability(dom.window.document).parse();
console.log(`---\ntitle: ${article.title}\nurl: ${args.page}\n\n---\n`);
console.log(turndownService.turndown(article.content));
}
}
// Return the image specified by page in base64, so it can inlined
function inlineBase64Img(page)
{
return new Promise((resolve, reject) => {
var http;
if (page.startsWith("https")) {
http = require('https');
} else {
http = require('http');
}
var body = [];
// const req = http.request(new URL(page), , (res) => {
const req = http.get(new URL(page), (res) => {
res.setEncoding('binary');
// console.error(`STATUS: ${res.statusCode}`);
// console.error(`HEADERS: ${JSON.stringify(res.headers)}`);
res.on('data', (chunk) => {
body.push(chunk);
});
res.on('end', () => {
const imgStr = Buffer.from(body.join(''), 'binary').toString('base64');
const contentType = res.headers['content-type'];
resolve(`data:${contentType};base64,${imgStr}`);
});
res.on('abort', () => {
reject("aborted");
});
});
req.on('error', (e) => {
reject(`problem with request: ${e.message}`);
});
});
}
main(args);