2022-01-08 17:05:05 +00:00
|
|
|
const https = require('https');
|
|
|
|
const turndown = require('turndown');
|
|
|
|
const { Readability } = require('@mozilla/readability');
|
|
|
|
const JSDOM = require('jsdom').JSDOM;
|
2022-01-13 14:40:21 +00:00
|
|
|
const common_filters = require('./url_to_markdown_common_filters');
|
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');
|
|
|
|
const app = express();
|
2022-01-09 15:20:59 +00:00
|
|
|
const rateLimit = require('express-rate-limit');
|
2022-01-29 19:11:43 +00:00
|
|
|
const service = new turndown();
|
|
|
|
|
|
|
|
const port = process.env.PORT;
|
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({
|
|
|
|
extended: true
|
|
|
|
}));
|
2022-01-09 15:20:59 +00:00
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
app.get('/', (req, res) => {
|
|
|
|
url = req.query.url;
|
2022-01-18 16:39:42 +00:00
|
|
|
if (url && validURL(url)) {
|
2022-01-29 19:11:43 +00:00
|
|
|
send_headers(res);
|
2022-01-29 19:52:39 +00:00
|
|
|
read_url(url, res);
|
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) {
|
|
|
|
let html;
|
|
|
|
|
|
|
|
if (req.body) {
|
|
|
|
html = req.body.html;
|
|
|
|
if (!html) {
|
|
|
|
res.status(400).send("Please provide a POST parameter called html");
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
let document = new JSDOM(html);
|
|
|
|
let markdown = process_dom(document, res);
|
|
|
|
res.send(markdown);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(400).send("Could not parse that document");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
})
|
|
|
|
|
2022-01-29 19:11:43 +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 process_dom(document, res) {
|
|
|
|
let title = document.window.document.querySelector('title');
|
|
|
|
if (title)
|
|
|
|
res.header("X-Title", encodeURIComponent(title.textContent));
|
|
|
|
let reader = new Readability(document.window.document);
|
|
|
|
let article = reader.parse();
|
|
|
|
let markdown = service.turndown(article.content);
|
|
|
|
return markdown;
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
function read_url(url, res) {
|
|
|
|
JSDOM.fromURL(url).then((document)=>{
|
2022-01-29 19:52:39 +00:00
|
|
|
let markdown = process_dom(document, res);
|
|
|
|
let result = common_filters.filter(url, markdown);
|
|
|
|
res.send(result);
|
2022-01-18 16:39:42 +00:00
|
|
|
}).catch((error)=> {
|
|
|
|
res.status(400).send("Sorry, could not fetch and convert that URL");
|
2022-01-08 17:05:05 +00:00
|
|
|
});
|
|
|
|
}
|