Skip to content

Commit

Permalink
Fix table indentation issues
Browse files Browse the repository at this point in the history
close #325

reverts #224
  • Loading branch information
rlidwka committed Jan 17, 2017
1 parent 79244d1 commit d29f421
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
8.3.0 / WIP
------------------

- Fix table indentation issues, #325.


8.2.2 / 2016-12-15
------------------

Expand Down
14 changes: 8 additions & 6 deletions lib/rules_block/table.js
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand Down
119 changes: 81 additions & 38 deletions test/fixtures/markdown-it/tables.txt
Expand Up @@ -448,28 +448,6 @@ x | \`\` | `x`
</table>
.

Should be parsed before code blocks:
.
foo | bar
----- | -----
aaa | bbb
.
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaa</td>
<td>bbb</td>
</tr>
</tbody>
</table>
.


An amount of rows might be different across the table (issue #171):
.
Expand Down Expand Up @@ -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 |
.
<table>
<thead>
<tr>
<th>0,0</th>
<th>0,1</th>
<th>0,2</th>
<th>Col1a</th>
<th>Col2a</th>
</tr>
</thead>
<tbody>
<tr>
<td>1,0</td>
<td></td>
<td>1,2</td>
<td>Col1b</td>
<td>Col2b</td>
</tr>
</tbody>
</table>
.


Tables should not be indented more than 4 spaces (1st line):
.
| Col1a | Col2a |
| ----- | ----- |
| Col1b | Col2b |
.
<pre><code>| Col1a | Col2a |
</code></pre>
<p>| ----- | ----- |
| Col1b | Col2b |</p>
.


Tables should not be indented more than 4 spaces (2nd line):
.
| Col1a | Col2a |
| ----- | ----- |
| Col1b | Col2b |
.
<p>| Col1a | Col2a |
| ----- | ----- |
| Col1b | Col2b |</p>
.


Tables should not be indented more than 4 spaces (3rd line):
.
| Col1a | Col2a |
| ----- | ----- |
| Col1b | Col2b |
.
<table>
<thead>
<tr>
<th>Col1a</th>
<th>Col2a</th>
</tr>
</thead>
<tbody></tbody>
</table>
<pre><code>| Col1b | Col2b |
</code></pre>
.


Allow tables with empty body:
.
| Col1a | Col2a |
| ----- | ----- |
.
<table>
<thead>
<tr>
<td></td>
<td>2,1</td>
<td></td>
<th>Col1a</th>
<th>Col2a</th>
</tr>
</tbody>
</thead>
<tbody></tbody>
</table>
.


Align row should be at least as large as any actual rows:
.
Col1a | Col1b | Col1c
----- | -----
Col2a | Col2b | Col2c
.
<p>Col1a | Col1b | Col1c
----- | -----
Col2a | Col2b | Col2c</p>
.

0 comments on commit d29f421

Please sign in to comment.