Alex Kocharin 8 years ago
parent
commit
d29f421927
  1. 6
      CHANGELOG.md
  2. 14
      lib/rules_block/table.js
  3. 119
      test/fixtures/markdown-it/tables.txt

6
CHANGELOG.md

@ -1,3 +1,9 @@
8.3.0 / WIP
------------------
- Fix table indentation issues, #325.
8.2.2 / 2016-12-15
------------------

14
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++) {

119
test/fixtures/markdown-it/tables.txt

@ -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):
.
@ -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>
<td></td>
<td>2,1</td>
<td></td>
<th>Col1a</th>
<th>Col2a</th>
</tr>
</tbody>
</thead>
<tbody></tbody>
</table>
<pre><code>| Col1b | Col2b |
</code></pre>
.
Allow tables with empty body:
.
| Col1a | Col2a |
| ----- | ----- |
.
<table>
<thead>
<tr>
<th>Col1a</th>
<th>Col2a</th>
</tr>
</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>
.

Loading…
Cancel
Save