Skip to content

Commit

Permalink
Merge pull request #61 from nekno/gh-pages
Browse files Browse the repository at this point in the history
Fix bug in `isColumnSeparator()` parsing logic
  • Loading branch information
ozh committed Mar 19, 2022
2 parents a551bbd + 2762be9 commit d92c1f0
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ function parseTable(table) {
for (var j = 0; j < longest.length; j++) {
if (isColumnSeparator(lines.slice(), j)) {
colIndexes.push(j);
// column separators are each padded by a space
// so skip over minimum distance between 2 columns
j += 2;
}
}

Expand Down Expand Up @@ -560,13 +563,24 @@ function isColumnSeparator(lines, column) {
} else {
var thisLine = lines[0];
var nextLine = lines[1];

if (column >= thisLine.length) {
// Column is out of range, must not be a separator
return false;
}
if (thisLine[column] == nextLine[column] && thisLine[column] != " ") {

var previousColumn = column - 1;
var thisLineThisChar = thisLine[column];
var thisLinePreviousChar = (previousColumn > 0) ? thisLine[previousColumn] : " ";
var nextLineThisChar = nextLine[column];
var nextLinePreviousChar = (previousColumn > 0) ? nextLine[previousColumn] : " ";

if (thisLineThisChar == nextLineThisChar
&& !isSpace(thisLineThisChar)
&& isSpace(thisLinePreviousChar, nextLinePreviousChar)
) {
// Rows match, check next row down
return isColumnSeparator(lines.splice(0,1), column);
return isColumnSeparator(lines.splice(1), column);
} else {
// Rows are different, this is not a separator
return false;
Expand All @@ -578,6 +592,10 @@ function isSeparatorLine(line) {
return line.trim().indexOf(" ") == -1; // must not have spaces
}

function isSpace(...chars) {
return chars.every(value => value == " ");
}

function _trim(str) {
var rgx = /^\s*(.*?)\s*$/;
var result = str.match(rgx);
Expand Down

0 comments on commit d92c1f0

Please sign in to comment.