From 126debf44e94e9fa35dc4cf72b7b48c4fc270612 Mon Sep 17 00:00:00 2001 From: samgbell <32361770+samgbell@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:52:19 +0200 Subject: [PATCH] Adding Markdown format for To Table operation --- src/core/operations/ToTable.mjs | 55 +++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/core/operations/ToTable.mjs b/src/core/operations/ToTable.mjs index 91b07771..114d5e21 100644 --- a/src/core/operations/ToTable.mjs +++ b/src/core/operations/ToTable.mjs @@ -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.

Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to \\t to support TSV (Tab Separated Values) or | for PSV (Pipe Separated Values).

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.

Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to \\t to support TSV (Tab Separated Values) or | for PSV (Pipe Separated Values).

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; + } + + } + } }