-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathWritableStream.js
46 lines (39 loc) · 1.18 KB
/
WritableStream.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
const Readability = require("../readabilitySAX");
const { Parser, CollectingHandler } = require("htmlparser2");
const { Writable } = require("readable-stream");
const parserOptions = {
lowerCaseTags: true,
};
module.exports = class WritableStream extends Writable {
constructor(settings, callback) {
super();
if (typeof settings === "function") {
callback = settings;
settings = null;
}
this._cb = callback;
this._readability = new Readability(settings);
this._handler = new CollectingHandler(this._readability);
this._parser = new Parser(this._handler, parserOptions);
}
_write(chunk, encoding, cb) {
this._parser.write(chunk);
cb();
}
end(chunk) {
this._parser.end(chunk);
super.end();
for (
let skipLevel = 1;
this._readability._getCandidateNode().info.textLength < 250 &&
skipLevel < 4;
skipLevel++
) {
this._readability.setSkipLevel(skipLevel);
this._handler.restart();
}
if (this._cb) {
this._cb(this._readability.getArticle());
}
}
};