-
Notifications
You must be signed in to change notification settings - Fork 29
/
url_to_markdown_readers.js
executable file
·78 lines (72 loc) · 2.12 KB
/
url_to_markdown_readers.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
const apple_dev_parser = require('./url_to_markdown_apple_dev_docs.js');
const processor = require('./url_to_markdown_processor.js');
const JSDOM = require('jsdom').JSDOM;
const https = require('https');
const failure_message = "Sorry, could not fetch and convert that URL";
const apple_dev_prefix = "https://developer.apple.com";
const stackoverflow_prefix = "https://stackoverflow.com/questions";
class html_reader {
read_url(url, res, inline_title, ignore_links) {
JSDOM.fromURL(url).then((document)=>{
let markdown = processor.process_dom(url, document, res, inline_title, ignore_links);
res.send(markdown);
}).catch((error)=> {
res.status(400).send(failure_message);
});
}
}
class apple_reader {
read_url(url, res, inline_title, ignore_links) {
let json_url = apple_dev_parser.dev_doc_url(url);
https.get(json_url,(apple_res) => {
let body = "";
apple_res.on("data", (chunk) => {
body += chunk;
});
apple_res.on("end", () => {
let json = JSON.parse(body);
let markdown = apple_dev_parser.parse_dev_doc_json(json, inline_title, ignore_links);
res.send(markdown);
});
});
}
}
class stack_reader {
read_url(url, res, inline_title, ignore_links) {
JSDOM.fromURL(url).then((document)=>{
let markdown_q = processor.process_dom(url, document, res, inline_title, ignore_links, 'question');
let markdown_a = processor.process_dom(url, document, res, false, ignore_links, 'answers');
if (markdown_a.startsWith('Your Answer')) {
res.send(markdown_q);
}
else {
res.send(markdown_q + "\n\n## Answer\n"+ markdown_a);
}
}).catch((error)=> {
res.status(400).send(failure_message);
});
}
}
module.exports = {
html_reader,
stack_reader,
apple_reader,
reader_for_url: function (url) {
if (url.startsWith(apple_dev_prefix)) {
return new apple_reader;
} else if (url.startsWith(stackoverflow_prefix)) {
return new stack_reader;
} else {
return new html_reader;
}
},
ignore_post: function(url) {
if (url) {
if (url.startsWith(stackoverflow_prefix)) {
return true;
}
} else {
return false;
}
}
}