From d29f421927e93e88daf75f22089a3e732e195bd2 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 17 Jan 2017 20:25:55 +0300 Subject: [PATCH] Fix table indentation issues close https://github.com/markdown-it/markdown-it/issues/325 reverts https://github.com/markdown-it/markdown-it/pull/224 --- CHANGELOG.md | 6 ++ lib/rules_block/table.js | 14 ++-- test/fixtures/markdown-it/tables.txt | 119 ++++++++++++++++++--------- 3 files changed, 95 insertions(+), 44 deletions(-) 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 -. - - - - - - - - - - - - - -
foobar
aaabbb
-. - An amount of rows might be different across the table (issue #171): . @@ -542,33 +520,98 @@ Allow one-column tables (issue #171): . -Allow tables with missing values: +Allow indented tables (issue #325): . -0,0 | 0,1 | 0,2 ---- | --- | --- -1,0 | | 1,2 - | 2,1 | - + | Col1a | Col2a | + | ----- | ----- | + | Col1b | Col2b | . - - - + + - - - + + + + +
0,00,10,2Col1aCol2a
1,01,2Col1bCol2b
+. + + +Tables should not be indented more than 4 spaces (1st line): +. + | Col1a | Col2a | + | ----- | ----- | + | 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 | +. + + + + + + + +
Col1aCol2a
+
| Col1b | Col2b |
+
+. + + +Allow tables with empty body: +. + | Col1a | Col2a | + | ----- | ----- | +. + + - - - + + - + +
2,1Col1aCol2a
. + + +Align row should be at least as large as any actual rows: +. +Col1a | Col1b | Col1c +----- | ----- +Col2a | Col2b | Col2c +. +

Col1a | Col1b | Col1c +----- | ----- +Col2a | Col2b | Col2c

+.