urltomarkdown/url_to_markdown_readers.js

78 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2022-12-21 22:28:59 +00:00
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);
2022-12-21 22:50:16 +00:00
}).catch((error)=> {
res.status(400).send(failure_message);
2022-12-21 22:28:59 +00:00
});
}
}
class apple_reader {
read_url(url, res, inline_title, ignore_links) {
2022-12-21 23:42:01 +00:00
let json_url = apple_dev_parser.dev_doc_url(url);
2022-12-21 22:28:59 +00:00
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);
}
2022-12-21 22:50:16 +00:00
}).catch((error)=> {
res.status(400).send(failure_message);
2022-12-21 22:28:59 +00:00
});
}
}
module.exports = {
2022-12-23 12:03:25 +00:00
html_reader,
stack_reader,
apple_reader,
2022-12-21 23:09:51 +00:00
reader_for_url: function (url) {
if (url.startsWith(apple_dev_prefix)) {
2022-12-21 22:28:59 +00:00
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;
}
}
}