diff --git a/CHANGELOG.md b/CHANGELOG.md index f3826f0..63163d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +8.3.0 / WIP +------------------ + +- Fix table indentation issues, #325. + + 8.2.2 / 2016-12-15 ------------------ diff --git a/lib/rules_block/table.js b/lib/rules_block/table.js index e29c12f..16ae20f 100644 --- a/lib/rules_block/table.js +++ b/lib/rules_block/table.js @@ -68,13 +68,16 @@ module.exports = function table(state, startLine, endLine, silent) { var ch, lineText, pos, i, nextLine, columns, columnCount, token, aligns, t, tableLines, tbodyLines; - // should have at least three lines + // should have at least two lines if (startLine + 2 > endLine) { return false; } nextLine = startLine + 1; if (state.sCount[nextLine] < state.blkIndent) { return false; } + // if it's indented more than 3 spaces, it should be a code block + if (state.sCount[nextLine] - state.blkIndent >= 4) { return false; } + // first character of the second line should be '|', '-', ':', // and no other characters are allowed but spaces; // basically, this is the equivalent of /^[-:|][-:|\s]*$/ regexp @@ -121,6 +124,7 @@ module.exports = function table(state, startLine, endLine, silent) { lineText = getLine(state, startLine).trim(); if (lineText.indexOf('|') === -1) { return false; } + if (state.sCount[startLine] - state.blkIndent >= 4) { return false; } columns = escapedSplit(lineText.replace(/^\||\|$/g, '')); // header row will define an amount of columns in the entire table, @@ -163,12 +167,10 @@ module.exports = function table(state, startLine, endLine, silent) { for (nextLine = startLine + 2; nextLine < endLine; nextLine++) { if (state.sCount[nextLine] < state.blkIndent) { break; } - lineText = getLine(state, nextLine); + lineText = getLine(state, nextLine).trim(); if (lineText.indexOf('|') === -1) { break; } - - // keep spaces at beginning of line to indicate an empty first cell, but - // strip trailing whitespace - columns = escapedSplit(lineText.replace(/^\||\|\s*$/g, '')); + if (state.sCount[nextLine] - state.blkIndent >= 4) { break; } + columns = escapedSplit(lineText.replace(/^\||\|$/g, '')); token = state.push('tr_open', 'tr', 1); for (i = 0; i < columnCount; i++) { diff --git a/test/fixtures/markdown-it/tables.txt b/test/fixtures/markdown-it/tables.txt index 15e26c8..a813375 100644 --- a/test/fixtures/markdown-it/tables.txt +++ b/test/fixtures/markdown-it/tables.txt @@ -448,28 +448,6 @@ x | \`\` | `x` . -Should be parsed before code blocks: -. - foo | bar - ----- | ----- - aaa | bbb -. -
foo | -bar | -
---|---|
aaa | -bbb | -
0,0 | -0,1 | -0,2 | +Col1a | +Col2a |
---|---|---|---|---|
1,0 | -- | 1,2 | +Col1b | +Col2b | +
| Col1a | Col2a |
+
+| ----- | ----- | +| Col1b | Col2b |
+. + + +Tables should not be indented more than 4 spaces (2nd line): +. + | Col1a | Col2a | + | ----- | ----- | + | Col1b | Col2b | +. +| Col1a | Col2a | +| ----- | ----- | +| Col1b | Col2b |
+. + + +Tables should not be indented more than 4 spaces (3rd line): +. + | Col1a | Col2a | + | ----- | ----- | + | Col1b | Col2b | +. +Col1a | +Col2a |
---|
| Col1b | Col2b |
+
+.
+
+
+Allow tables with empty body:
+.
+ | Col1a | Col2a |
+ | ----- | ----- |
+.
+- | 2,1 | -+ | Col1a | +Col2a |
---|
Col1a | Col1b | Col1c +----- | ----- +Col2a | Col2b | Col2c
+.