urltomarkdown/html_table_to_markdown.js

141 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-01-30 20:14:46 +00:00
const htmlEntities = require('html-entities');
2022-02-04 11:51:31 +00:00
const justify = require('justify-text');
2022-01-30 20:14:46 +00:00
module.exports = {
2022-04-02 14:25:07 +00:00
max_width: 96,
2022-01-30 20:14:46 +00:00
2022-04-02 14:25:07 +00:00
clean: function (str) {
2022-01-30 20:14:46 +00:00
str = str.replace(/<\/?[^>]+(>|$)/g, "");
str = str.replace(/(\r\n|\n|\r)/gm, "");
str = htmlEntities.decode(str);
return str;
},
2022-04-02 14:25:07 +00:00
2022-01-30 20:14:46 +00:00
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);
}
2022-04-02 14:25:07 +00:00
// is this a proper table?
if (n_rows < 2)
return "";
2022-01-30 20:14:46 +00:00
// 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;
2022-01-30 20:14:46 +00:00
}
}
}
2022-02-04 11:51:31 +00:00
2022-04-02 14:25:07 +00:00
// decide how to present
let total_width = 0;
for (let c=0;c<n_cols;c++)
total_width = total_width + column_widths[c];
if (total_width < this.max_width) {
2022-04-02 14:25:07 +00:00
// present as table
2022-04-02 14:25:07 +00:00
// pad cells
for (let r=0;r<n_rows;r++) {
for (let c=0;c<n_cols;c++) {
items[r][c] = justify.ljust(items[r][c], column_widths[c], " ");
}
2022-01-30 20:14:46 +00:00
}
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";
2022-01-30 20:14:46 +00:00
result += "|";
for (let c=0;c<n_cols;c++) {
result += "-".repeat(column_widths[c]) + "|";
}
result += "\n";
for (let r=1;r<n_rows;r++) {
2022-01-30 20:14:46 +00:00
result += "|";
for (let c=0;c<n_cols;c++) {
result += items[r][c];
result += "|";
}
result += "\n";
2022-01-30 20:14:46 +00:00
}
}
} else {
2022-04-02 14:25:07 +00:00
// present as indented list
2022-01-30 20:14:46 +00:00
result += "\n";
for (let r=1;r<n_rows;r++) {
if (items[0][0] || items[r][0])
result += "* ";
if (items[0][0]) {
result += items[0][0];
result += ": ";
}
if (items[r][0])
result += items[r][0];
if (items[0][0] || items[r][0])
result += "\n";
for (let c=1;c<n_cols;c++) {
if (items[0][c] || items[r][c])
result += " * ";
if (items[0][c]) {
result += items[0][c];
result += ": ";
}
if (items[r][c])
result += items[r][c];
if (items[0][c] || items[r][c])
result += "\n";
2022-01-30 20:14:46 +00:00
}
}
}
return result;
}
}