option for inline title

main
Lee Hanken 2022-02-04 12:13:41 +00:00
parent 1a552f1225
commit 05b3875319
2 changed files with 18 additions and 7 deletions

View File

@ -66,7 +66,7 @@ module.exports = {
} }
} }
// justify // pad
for (let r=0;r<n_rows;r++) { for (let r=0;r<n_rows;r++) {
for (let c=0;c<n_cols;c++) { for (let c=0;c<n_cols;c++) {
items[r][c] = justify.ljust(items[r][c], column_widths[c], " "); items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");

View File

@ -30,9 +30,13 @@ app.use(express.urlencoded({
app.get('/', (req, res) => { app.get('/', (req, res) => {
url = req.query.url; url = req.query.url;
title = req.query.title;
let inline_title = false;
if (title)
inline_title = !!JSON.parse(title);
if (url && validURL(url)) { if (url && validURL(url)) {
send_headers(res); send_headers(res);
read_url(url, res); read_url(url, res, inline_title);
} else { } else {
res.status(400).send("Please specify a valid url query parameter"); res.status(400).send("Please specify a valid url query parameter");
} }
@ -41,12 +45,16 @@ app.get('/', (req, res) => {
app.post('/', function(req, res) { app.post('/', function(req, res) {
let html = req.body.html; let html = req.body.html;
let url = req.body.url; let url = req.body.url;
let inline_title = false;
title = req.query.title;
if (title)
inline_title = !!JSON.parse(title);
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); let markdown = process_dom(url, document, res, inline_title);
send_headers(res); send_headers(res);
res.send(markdown); res.send(markdown);
} catch (error) { } catch (error) {
@ -65,7 +73,7 @@ function send_headers(res) {
res.header("Content-Type", 'text/markdown'); res.header("Content-Type", 'text/markdown');
} }
function process_dom(url, document, res) { function process_dom(url, document, res, inline_title) {
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));
@ -78,12 +86,15 @@ function process_dom(url, document, res) {
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) : markdown; let result = (url) ? common_filters.filter(url, markdown) : markdown;
if (inline_title && title) {
result = "# " + title.textContent + "\n" + result;
}
return result; return result;
} }
function read_url(url, res) { function read_url(url, res, inline_title) {
JSDOM.fromURL(url).then((document)=>{ JSDOM.fromURL(url).then((document)=>{
let markdown = process_dom(url, document, res); let markdown = process_dom(url, document, res, inline_title);
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");