Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Nov 25, 2022
2 parents 9321b79 + 8e57354 commit e5644b5
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/core/operations/ToTable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ToTable extends Operation {

this.name = "To Table";
this.module = "Default";
this.description = "Data can be split on different characters and rendered as an HTML or ASCII table with an optional header row.<br><br>Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to <code>\\t</code> to support TSV (Tab Separated Values) or <code>|</code> for PSV (Pipe Separated Values).<br><br>You can enter as many delimiters as you like. Each character will be treat as a separate possible delimiter.";
this.description = "Data can be split on different characters and rendered as an HTML, ASCII or Markdown table with an optional header row.<br><br>Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to <code>\\t</code> to support TSV (Tab Separated Values) or <code>|</code> for PSV (Pipe Separated Values).<br><br>You can enter as many delimiters as you like. Each character will be treat as a separate possible delimiter.";
this.infoURL = "https://wikipedia.org/wiki/Comma-separated_values";
this.inputType = "string";
this.outputType = "html";
Expand All @@ -43,7 +43,7 @@ class ToTable extends Operation {
{
"name": "Format",
"type": "option",
"value": ["ASCII", "HTML"]
"value": ["ASCII", "HTML", "Markdown"]
}
];
}
Expand All @@ -66,6 +66,9 @@ class ToTable extends Operation {
case "ASCII":
return asciiOutput(tableData);
case "HTML":
return htmlOutput(tableData);
case "Markdown":
return markdownOutput(tableData);
default:
return htmlOutput(tableData);
}
Expand Down Expand Up @@ -183,6 +186,59 @@ class ToTable extends Operation {
return output;
}
}

/**
* Outputs an array of data as a Markdown table.
*
* @param {string[][]} tableData
* @returns {string}
*/
function markdownOutput(tableData) {
const headerDivider = "-";
const verticalBorder = "|";

let output = "";
const longestCells = [];

// Find longestCells value per column to pad cells equally.
tableData.forEach(function(row, index) {
row.forEach(function(cell, cellIndex) {
if (longestCells[cellIndex] === undefined || cell.length > longestCells[cellIndex]) {
longestCells[cellIndex] = cell.length;
}
});
});

// Ignoring the checkbox, as current Mardown renderer in CF doesn't handle table without headers
const row = tableData.shift();
output += outputRow(row, longestCells);
let rowOutput = verticalBorder;
row.forEach(function(cell, index) {
rowOutput += " " + headerDivider + " " + verticalBorder;
});
output += rowOutput += "\n";

// Add the rest of the table rows.
tableData.forEach(function(row, index) {
output += outputRow(row, longestCells);
});

return output;

/**
* Outputs a row of correctly padded cells.
*/
function outputRow(row, longestCells) {
let rowOutput = verticalBorder;
row.forEach(function(cell, index) {
rowOutput += " " + cell + " ".repeat(longestCells[index] - cell.length) + " " + verticalBorder;
});
rowOutput += "\n";
return rowOutput;
}

}

}

}
Expand Down

0 comments on commit e5644b5

Please sign in to comment.