urltomarkdown/index.js

90 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-12-21 22:28:59 +00:00
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');
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;
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
});
app.use(rateLimiter);
2022-01-08 17:05:05 +00:00
app.use(express.urlencoded({
2022-01-30 11:50:17 +00:00
extended: true,
limit: '10mb'
}));
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-Allow-Methods", 'GET, POST');
2022-12-21 22:28:59 +00:00
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');
}
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 {
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
});
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);
return;
}
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:50:16 +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:50:16 +00:00
} catch (error) {
res.status(400).send("Could not parse that document");
}
}
});
2022-01-08 17:05:05 +00:00
app.listen(port, () => {
})