Adding Markdown format for To Table operation

This commit is contained in:
samgbell 2022-10-13 11:52:19 +02:00 committed by GitHub
parent c2cf535f88
commit 126debf44e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 2 deletions

View File

@ -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";
@ -43,7 +43,7 @@ class ToTable extends Operation {
{
"name": "Format",
"type": "option",
"value": ["ASCII", "HTML"]
"value": ["ASCII", "HTML", "Markdown"]
}
];
}
@ -66,6 +66,10 @@ class ToTable extends Operation {
case "ASCII":
return asciiOutput(tableData);
case "HTML":
return htmlOutput(tableData);
case "Markdown":
return markdownOutput(tableData);
default:
return htmlOutput(tableData);
}
@ -183,6 +187,53 @@ class ToTable extends Operation {
return output;
}
}
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;
}
}
}
}