-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
96 lines (92 loc) · 3.28 KB
/
main.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
const http = require('http');
const turndown = require('turndown');
let convert = (html) => {
let turndownService = new turndown({
'headingStyle': 'atx',
'hr': '---',
'bulletListMarker': '*',
'codeBlockStyle': 'fenced',
'fence': '```',
'emDelimiter': '*',
'strongDelimiter': '**',
'linkStyle': 'inlined',
'linkReferenceStyle': 'full',
});
turndownService.addRule('removeThumbsup', {
filter: (node, options) => {
return ('DIV' == node.nodeName
&& node.getAttribute('class')
&& node.getAttribute('class').includes('book-article-kb-star')
|| 'A' == node.nodeName
&& node.getAttribute('class')
&& node.getAttribute('class').includes('star_count'));
},
replacement: (content, node, options) => {
return '';
},
}).addRule('fixImageLink', {
filter: (node, options) => {
return ('A' == node.nodeName
&& node.getAttribute('class')
&& node.getAttribute('class').includes('fluidbox fluidbox__instance')
&& node.getAttribute('href')
&& node.getAttribute('href').includes('https://')
&& node.getAttribute('href').includes('uploads/images'));
},
replacement: (content, node, options) => {
return content;
},
}).addRule('removeCopyright', {
filter: (node, options) => {
return ('DIV' == node.nodeName
&& node.getAttribute('style')
&& node.getAttribute('style').includes('overflow: hidden;'));
},
replacement: (content, node, options) => {
return '';
},
});
return turndownService.turndown(html) + "\n";
};
http.createServer((req, res) => {
res.setHeader('Connection', 'close');
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.setHeader('Access-Control-Allow-Origin','*');
switch (req.url) {
case '':
case '/':
if ('POST' == req.method) {
let body = [];
req.on('error', (err) => {
console.err(err);
}).on('data', (chunk) => {
let maxLength = 1 << 24; // 16MB
if (chunk.length > maxLength) {
req.connection.destroy();
return;
}
body.push(chunk);
}).on('end', () => {
let html = Buffer.concat(body).toString();
console.log(`input size: ${html.length} B`);
if (0 == html.trim().length) {
res.statusCode = 204;
res.end(null);
return;
}
let md = convert(html);
console.log(`output size: ${md.length} B`);
res.statusCode = 200;
res.end(md);
});
} else {
res.statusCode = 405;
res.end('Invalid request');
}
break;
default:
res.statusCode = 404;
res.end(null);
break;
}
}).listen(9999);