increase minimum table cell size
parent
a084fa23f5
commit
2567ae65c7
|
@ -0,0 +1,95 @@
|
||||||
|
const htmlEntities = require('html-entities');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
|
||||||
|
clean(str) {
|
||||||
|
str = str.replace(/<\/?[^>]+(>|$)/g, "");
|
||||||
|
str = str.replace(/(\r\n|\n|\r)/gm, "");
|
||||||
|
str = htmlEntities.decode(str);
|
||||||
|
return str;
|
||||||
|
},
|
||||||
|
convert: function (table) {
|
||||||
|
let result = "\n";
|
||||||
|
|
||||||
|
let caption = table.match(/<caption[^>]*>((?:.|\n)*)<\/caption>/i);
|
||||||
|
if (caption)
|
||||||
|
result += this.clean(caption[1]) + "\n\n";
|
||||||
|
|
||||||
|
let items = [];
|
||||||
|
|
||||||
|
// collect data
|
||||||
|
let rows = table.match(/(<tr[^>]*>(?:.|\n)*?<\/tr>)/gi);
|
||||||
|
let n_rows = rows.length;
|
||||||
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
let item_cols = [];
|
||||||
|
let cols = rows[r].match(/<t[h|d][^>]*>(?:.|\n)*?<\/t[h|d]>/gi);
|
||||||
|
for (let c=0;c<cols.length;c++)
|
||||||
|
item_cols.push(this.clean(cols[c]));
|
||||||
|
items.push(item_cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
// find number of columns
|
||||||
|
let n_cols=0;
|
||||||
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
if (items[r].length > n_cols) {
|
||||||
|
n_cols = items[r].length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalise columns
|
||||||
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
if (typeof items[r][c] === 'undefined') {
|
||||||
|
items[r].push("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// correct widths
|
||||||
|
let column_widths = [];
|
||||||
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
column_widths.push(3);
|
||||||
|
}
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
let l = items[r][c].length;
|
||||||
|
if (l>column_widths[c]) {
|
||||||
|
column_widths[c]=l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let r=0;r<n_rows;r++) {
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
items[r][c] = items[r][c].padEnd(column_widths[c], " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// output table
|
||||||
|
if (n_rows >0 && n_cols > 0) {
|
||||||
|
if (n_rows > 1) {
|
||||||
|
result += "|";
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
result += items[0][c];
|
||||||
|
result += "|";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += "\n";
|
||||||
|
result += "|";
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
result += "-".repeat(column_widths[c]) + "|";
|
||||||
|
}
|
||||||
|
result += "\n";
|
||||||
|
for (let r=1;r<n_rows;r++) {
|
||||||
|
result += "|";
|
||||||
|
for (let c=0;c<n_cols;c++) {
|
||||||
|
result += items[r][c];
|
||||||
|
result += "|";
|
||||||
|
}
|
||||||
|
result += "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
95
index.js
95
index.js
|
@ -3,10 +3,10 @@ const turndown = require('turndown');
|
||||||
const { Readability } = require('@mozilla/readability');
|
const { Readability } = require('@mozilla/readability');
|
||||||
const JSDOM = require('jsdom').JSDOM;
|
const JSDOM = require('jsdom').JSDOM;
|
||||||
const common_filters = require('./url_to_markdown_common_filters');
|
const common_filters = require('./url_to_markdown_common_filters');
|
||||||
|
const table_to_markdown = require('./html_table_to_markdown.js');
|
||||||
const validURL = require('@7c/validurl');
|
const validURL = require('@7c/validurl');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const rateLimit = require('express-rate-limit');
|
const rateLimit = require('express-rate-limit');
|
||||||
const htmlEntities = require('html-entities');
|
|
||||||
|
|
||||||
const port = process.env.PORT;
|
const port = process.env.PORT;
|
||||||
|
|
||||||
|
@ -90,102 +90,11 @@ function read_url(url, res) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function clean(str) {
|
|
||||||
str = str.replace(/<\/?[^>]+(>|$)/g, "");
|
|
||||||
str = str.replace(/(\r\n|\n|\r)/gm, "");
|
|
||||||
str = htmlEntities.decode(str);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
function format_table(table) {
|
|
||||||
let result = "\n";
|
|
||||||
|
|
||||||
let caption = table.match(/<caption[^>]*>((?:.|\n)*)<\/caption>/i);
|
|
||||||
if (caption)
|
|
||||||
result += clean(caption[1]) + "\n\n";
|
|
||||||
|
|
||||||
let items = [];
|
|
||||||
|
|
||||||
// collect data
|
|
||||||
let rows = table.match(/(<tr[^>]*>(?:.|\n)*?<\/tr>)/gi);
|
|
||||||
let n_rows = rows.length;
|
|
||||||
for (let r=0;r<n_rows;r++) {
|
|
||||||
let item_cols = [];
|
|
||||||
let cols = rows[r].match(/<t[h|d][^>]*>(?:.|\n)*?<\/t[h|d]>/gi);
|
|
||||||
for (let c=0;c<cols.length;c++)
|
|
||||||
item_cols.push(clean(cols[c]));
|
|
||||||
items.push(item_cols);
|
|
||||||
}
|
|
||||||
|
|
||||||
// find number of columns
|
|
||||||
let n_cols=0;
|
|
||||||
for (let r=0;r<n_rows;r++) {
|
|
||||||
if (items[r].length > n_cols) {
|
|
||||||
n_cols = items[r].length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// normalise columns
|
|
||||||
for (let r=0;r<n_rows;r++) {
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
if (typeof items[r][c] === 'undefined') {
|
|
||||||
items[r].push("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// correct widths
|
|
||||||
let column_widths = [];
|
|
||||||
for (let r=0;r<n_rows;r++) {
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
column_widths.push(0);
|
|
||||||
}
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
let l = items[r][c].length;
|
|
||||||
if (l>column_widths[c]) {
|
|
||||||
column_widths[c]=l;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (let r=0;r<n_rows;r++) {
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
items[r][c] = items[r][c].padEnd(column_widths[c], " ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// output table
|
|
||||||
if (n_rows >0 && n_cols > 0) {
|
|
||||||
if (n_rows > 1) {
|
|
||||||
result += "|";
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
result += items[0][c];
|
|
||||||
result += "|";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result += "\n";
|
|
||||||
result += "|";
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
result += "-".repeat(column_widths[c]) + "|";
|
|
||||||
}
|
|
||||||
result += "\n";
|
|
||||||
for (let r=1;r<n_rows;r++) {
|
|
||||||
result += "|";
|
|
||||||
for (let c=0;c<n_cols;c++) {
|
|
||||||
result += items[r][c];
|
|
||||||
result += "|";
|
|
||||||
}
|
|
||||||
result += "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function format_tables(html, replacements) {
|
function format_tables(html, replacements) {
|
||||||
const tables = html.match(/(<table[^>]*>(?:.|\n)*?<\/table>)/gi);
|
const tables = html.match(/(<table[^>]*>(?:.|\n)*?<\/table>)/gi);
|
||||||
for (let t=0;t<tables.length;t++) {
|
for (let t=0;t<tables.length;t++) {
|
||||||
let table = tables[t];
|
let table = tables[t];
|
||||||
let markdown = format_table(table);
|
let markdown = table_to_markdown.convert(table);
|
||||||
let placeholder = "urltomarkdowntableplaceholder"+t+Math.random();
|
let placeholder = "urltomarkdowntableplaceholder"+t+Math.random();
|
||||||
replacements.placeholders[t] = placeholder;
|
replacements.placeholders[t] = placeholder;
|
||||||
replacements.tables[t] = markdown;
|
replacements.tables[t] = markdown;
|
||||||
|
|
Loading…
Reference in New Issue