cleaned up handling of parameters

main
Lee Hanken 2022-04-08 11:16:54 +01:00
parent cb6689e175
commit 195afdd469
3 changed files with 39 additions and 25 deletions

View File

@ -29,35 +29,44 @@ app.use(express.urlencoded({
}));
app.get('/', (req, res) => {
url = req.query.url;
title = req.query.title;
links = req.query.links;
const url = req.query.url;
const title = req.query.title;
const links = req.query.links;
let inline_title = false;
if (title)
inline_title = !!JSON.parse(title);
if (links)
links = !JSON.parse(links);
let ignore_links = false;
if (title) {
inline_title = (title === 'true');
}
if (links) {
ignore_links = (links === 'false');
}
if (url && validURL(url)) {
send_headers(res);
read_url(url, res, inline_title, links);
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) {
let html = req.body.html;
let url = req.body.url;
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;
title = req.query.title;
if (title)
inline_title = !!JSON.parse(title);
if (title) {
inline_title = (title === 'true');
}
if (links) {
ignore_links = (links === 'false');
}
if (!html) {
res.status(400).send("Please provide a POST parameter called html");
} else {
try {
let document = new JSDOM(html);
let markdown = process_dom(url, document, res, inline_title);
let markdown = process_dom(url, document, res, inline_title, ignore_links);
send_headers(res);
res.send(markdown);
} catch (error) {
@ -76,7 +85,7 @@ function send_headers(res) {
res.header("Content-Type", 'text/markdown');
}
function process_dom(url, document, res, inline_title) {
function process_dom(url, document, res, inline_title, ignore_links) {
let title = document.window.document.querySelector('title');
if (title)
res.header("X-Title", encodeURIComponent(title.textContent));
@ -88,16 +97,16 @@ function process_dom(url, document, res, inline_title) {
for (let i=0;i<replacement.placeholders.length;i++) {
markdown = markdown.replace(replacement.placeholders[i], replacement.tables[i]);
}
let result = (url) ? common_filters.filter(url, markdown, links) : markdown;
let result = (url) ? common_filters.filter(url, markdown, ignore_links) : markdown;
if (inline_title && title) {
result = "# " + title.textContent + "\n" + result;
}
return result;
}
function read_url(url, res, inline_title, links) {
function read_url(url, res, inline_title, ignore_links) {
JSDOM.fromURL(url).then((document)=>{
let markdown = process_dom(url, document, res, inline_title, links);
let markdown = process_dom(url, document, res, inline_title, ignore_links);
res.send(markdown);
}).catch((error)=> {
res.status(400).send("Sorry, could not fetch and convert that URL");

View File

@ -38,14 +38,19 @@ Title is also returned in HTTP header.
X-Title: Firefox%20-%20Protect%20your%20life%20online%20with%20privacy-first%20products%20%E2%80%94%20Mozilla%20(UK)
```
Alternative request, supply url and html:
Optionally suppress links:
POST https://urltomarkdown.herokuapp.com/
GET https://urltomarkdown.herokuapp.com/?url=https%3A%2F%2Fwww.mozilla.org%2Fen-GB%2Ffirefox%2F&links=false
Alternative POST request, supply url and html in POST body:
POST https://urltomarkdown.herokuapp.com/?title=true&links=false
url=https%3A%2F%2Fwww.mozilla.org%2Fen-GB%2Ffirefox%2F
html=%3C!doctype%20html%3E%3Chtml%20...
Inspired by [Heck Yeah Markdown](http://heckyesmarkdown.com)
## Also of interest:
@ -76,7 +81,7 @@ javascript:(
### Safari Snippets
Using [Safari Snippets](https://apps.apple.com/us/app/safari-snippets/id1126048257)
with the following code solves the issue that some sites prevent javascript bookmarklets accessing a resource on a different domain
to inject the following code solves the issue that some sites prevent javascript bookmarklets accessing a resource on a different domain
```
var request=new XMLHttpRequest();

View File

@ -47,7 +47,7 @@ module.exports = {
}
],
filter: function (url, data, links=true) {
filter: function (url, data, ignore_links=false) {
let domain='';
let base_address='';
if (url) {
@ -82,7 +82,7 @@ module.exports = {
);
// remove inline links and refs
if (!links) {
if (ignore_links) {
data = data.replaceAll(/\[\[?([^\]]+\]?)\]\([^\)]+\)/g, '$1');
data = data.replaceAll(/[\\\[]+([0-9]+)[\\\]]+/g, '[$1]');
}