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

View File

@ -38,13 +38,18 @@ 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) 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 url=https%3A%2F%2Fwww.mozilla.org%2Fen-GB%2Ffirefox%2F
html=%3C!doctype%20html%3E%3Chtml%20... html=%3C!doctype%20html%3E%3Chtml%20...
Inspired by [Heck Yeah Markdown](http://heckyesmarkdown.com) Inspired by [Heck Yeah Markdown](http://heckyesmarkdown.com)
@ -76,7 +81,7 @@ javascript:(
### Safari Snippets ### Safari Snippets
Using [Safari Snippets](https://apps.apple.com/us/app/safari-snippets/id1126048257) 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(); 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 domain='';
let base_address=''; let base_address='';
if (url) { if (url) {
@ -82,7 +82,7 @@ module.exports = {
); );
// remove inline links and refs // remove inline links and refs
if (!links) { if (ignore_links) {
data = data.replaceAll(/\[\[?([^\]]+\]?)\]\([^\)]+\)/g, '$1'); data = data.replaceAll(/\[\[?([^\]]+\]?)\]\([^\)]+\)/g, '$1');
data = data.replaceAll(/[\\\[]+([0-9]+)[\\\]]+/g, '[$1]'); data = data.replaceAll(/[\\\[]+([0-9]+)[\\\]]+/g, '[$1]');
} }