-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
executable file
·89 lines (80 loc) · 2.18 KB
/
index.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
const readers = require('./url_to_markdown_readers.js');
const processor = require('./url_to_markdown_processor.js');
const validURL = require('@7c/validurl');
const express = require('express');
const rateLimit = require('express-rate-limit');
const JSDOM = require('jsdom').JSDOM;
const port = process.env.PORT;
const app = express();
const rateLimiter = rateLimit({
windowMs: 30 * 1000,
max: 5,
message: 'Rate limit exceeded',
headers: true
});
app.use(rateLimiter);
app.use(express.urlencoded({
extended: true,
limit: '10mb'
}));
function send_headers(res) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Methods", 'GET, POST');
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);
}
app.get('/', (req, res) => {
const url = req.query.url;
const title = req.query.title;
const links = req.query.links;
let inline_title = false;
let ignore_links = false;
if (title) {
inline_title = (title === 'true');
}
if (links) {
ignore_links = (links === 'false');
}
if (url && validURL(url)) {
read_url(url, res, inline_title, ignore_links);
} else {
res.status(400).send("Please specify a valid url query parameter");
}
});
app.post('/', function(req, res) {
const html = req.body.html;
const url = req.body.url;
const links = req.query.links;
const title = req.query.title;
let ignore_links = false;
let inline_title = false;
if (title) {
inline_title = (title === 'true');
}
if (links) {
ignore_links = (links === 'false');
}
if (readers.ignore_post(url)) {
read_url(url, res, inline_title, ignore_links);
return;
}
if (!html) {
res.status(400).send("Please provide a POST parameter called html");
} else {
try {
let document = new JSDOM(html);
let markdown = processor.process_dom(url, document, res, inline_title, ignore_links);
send_headers(res);
res.send(markdown);
} catch (error) {
res.status(400).send("Could not parse that document");
}
}
});
app.listen(port, () => {
})