Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module.exports = {

var sampleTable = [
"",
"Column A | Column B | Column C",
"---------|----------|---------",
" A1 | B1 | C1",
" A2 | B2 | C2",
" A3 | B3 | C3"
"| Column A | Column B | Column C |",
"| ---------|----------|--------- |",
"| A1 | B1 | C1 |",
"| A2 | B2 | C2 |",
"| A3 | B3 | C3 |"
].join("\n");

function addTable(addHeader) {
Expand All @@ -31,26 +31,27 @@ function addTable(addHeader) {

var tableColumnSeparator = /([ ]{2,}|[\t])/gi;
function convertToTableWithoutHeader(text) {
var firstRow = text.match(/.+/);
var tableContent = "| " + text.trim().replace(tableColumnSeparator, " | ").trim().replace(/\n/g, " |\n| ") + " |";
var firstRow = text.trim().match(/.+/);

var columnSeparators = firstRow == null ? null : firstRow[0].match(tableColumnSeparator);
var columnCount = columnSeparators == null ? 0 : columnSeparators.length;
var line1 = [];
for (var i = 0; i < columnCount + 1; i++) {
line1.push("column" + i);
}
var tableHeader = line1.join(" | ") + "\n";
var tableHeader = "| " + line1.join(" | ") + " |\n";
tableHeader = tableHeader + tableHeader.replace(/[a-z0-9]/gi, "-");

return tableHeader + text.replace(tableColumnSeparator, " | ");
return tableHeader + tableContent;
}

function convertToTableWithHeader(text) {
var textAsTable = text.replace(tableColumnSeparator, " | ");
var textAsTable = "| " + text.trim().replace(tableColumnSeparator, " | ").trim().replace(/\n/g, " |\n| ") + " |";

var firstRow = textAsTable.match(/.+/)[0];

var headerLine = firstRow.replace(/[^\|]/gi, "-");

return firstRow + "\n" + headerLine + textAsTable.substring(firstRow.length);
}
return firstRow + "\n" + headerLine + textAsTable.substring(firstRow.length);
}