2022-12-21 22:28:59 +00:00
|
|
|
const readers = require('./url_to_markdown_readers.js');
|
|
|
|
const processor = require('./url_to_markdown_processor.js');
|
2022-01-18 16:39:42 +00:00
|
|
|
const validURL = require('@7c/validurl');
|
2022-01-29 19:11:43 +00:00
|
|
|
const express = require('express');
|
2022-01-09 15:20:59 +00:00
|
|
|
const rateLimit = require('express-rate-limit');
|
2022-12-21 22:28:59 +00:00
|
|
|
const JSDOM = require('jsdom').JSDOM;
|
2022-01-29 19:11:43 +00:00
|
|
|
const port = process.env.PORT;
|
2022-01-30 11:50:17 +00:00
|
|
|
const app = express();
|
|
|
|
|
2022-01-09 15:20:59 +00:00
|
|
|
const rateLimiter = rateLimit({
|
|
|
|
windowMs: 30 * 1000,
|
|
|
|
max: 5,
|
|
|
|
message: 'Rate limit exceeded',
|
|
|
|
headers: true
|
|
|
|
});
|
|
|
|
|
2022-01-29 19:11:43 +00:00
|
|
|
app.use(rateLimiter);
|
2022-01-08 17:05:05 +00:00
|
|
|
|
2022-01-29 19:11:43 +00:00
|
|
|
app.use(express.urlencoded({
|
2022-01-30 11:50:17 +00:00
|
|
|
extended: true,
|
|
|
|
limit: '10mb'
|
2022-01-29 19:11:43 +00:00
|
|
|
}));
|
2022-01-09 15:20:59 +00:00
|
|
|
|
2022-12-21 22:28:59 +00:00
|
|
|
function send_headers(res) {
|
|
|
|
res.header("Access-Control-Allow-Origin", '*');
|
|
|
|
res.header("Access-Control-Expose-Headers", 'X-Title');
|
|
|
|
res.header("Content-Type", 'text/markdown');
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_url(url, res, inline_title, ignore_links) {
|
|
|
|
reader = readers.reader_for_url(url);
|
|
|
|
send_headers(res);
|
|
|
|
reader.read_url(url, res, inline_title, ignore_links);
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
app.get('/', (req, res) => {
|
2022-04-08 10:16:54 +00:00
|
|
|
const url = req.query.url;
|
|
|
|
const title = req.query.title;
|
|
|
|
const links = req.query.links;
|
2022-02-04 12:13:41 +00:00
|
|
|
let inline_title = false;
|
2022-04-08 10:16:54 +00:00
|
|
|
let ignore_links = false;
|
|
|
|
if (title) {
|
|
|
|
inline_title = (title === 'true');
|
|
|
|
}
|
|
|
|
if (links) {
|
|
|
|
ignore_links = (links === 'false');
|
|
|
|
}
|
2022-01-18 16:39:42 +00:00
|
|
|
if (url && validURL(url)) {
|
2022-12-21 22:28:59 +00:00
|
|
|
read_url(url, res, inline_title, ignore_links);
|
2022-01-08 17:35:43 +00:00
|
|
|
} else {
|
2022-01-18 16:39:42 +00:00
|
|
|
res.status(400).send("Please specify a valid url query parameter");
|
2022-01-08 17:35:43 +00:00
|
|
|
}
|
2022-01-08 17:05:05 +00:00
|
|
|
});
|
|
|
|
|
2022-01-29 19:11:43 +00:00
|
|
|
app.post('/', function(req, res) {
|
2022-04-08 10:16:54 +00:00
|
|
|
const html = req.body.html;
|
|
|
|
const url = req.body.url;
|
|
|
|
const links = req.query.links;
|
|
|
|
const title = req.query.title;
|
|
|
|
let ignore_links = false;
|
2022-02-04 12:13:41 +00:00
|
|
|
let inline_title = false;
|
2022-04-08 10:16:54 +00:00
|
|
|
if (title) {
|
|
|
|
inline_title = (title === 'true');
|
|
|
|
}
|
|
|
|
if (links) {
|
|
|
|
ignore_links = (links === 'false');
|
|
|
|
}
|
2022-12-21 22:28:59 +00:00
|
|
|
if (readers.ignore_post(url)) {
|
|
|
|
read_url(url, res, inline_title, ignore_links);
|
2022-11-05 02:49:07 +00:00
|
|
|
}
|
2022-01-30 11:50:17 +00:00
|
|
|
if (!html) {
|
|
|
|
res.status(400).send("Please provide a POST parameter called html");
|
|
|
|
} else {
|
2022-12-21 22:28:59 +00:00
|
|
|
//try {
|
2022-05-12 14:09:43 +00:00
|
|
|
let document = new JSDOM(html);
|
2022-12-21 22:28:59 +00:00
|
|
|
let markdown = processor.process_dom(url, document, res, inline_title, ignore_links);
|
2022-05-12 14:09:43 +00:00
|
|
|
send_headers(res);
|
|
|
|
res.send(markdown);
|
2022-12-21 22:28:59 +00:00
|
|
|
//} catch (error) {
|
|
|
|
//res.status(400).send("Could not parse that document");
|
|
|
|
//}
|
2022-01-29 19:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
})
|