Browse Source

Loosen table rules

1. allow one-column tables
2. allow mismatched column count

close https://github.com/markdown-it/markdown-it/issues/171
pull/186/head
Alex Kocharin 9 years ago
parent
commit
a18ddc0223
  1. 33
      lib/rules_block/table.js
  2. 96
      test/fixtures/markdown-it/tables.txt

33
lib/rules_block/table.js

@ -54,7 +54,7 @@ function escapedSplit(str) {
module.exports = function table(state, startLine, endLine, silent) { module.exports = function table(state, startLine, endLine, silent) {
var ch, lineText, pos, i, nextLine, rows, token, var ch, lineText, pos, i, nextLine, columns, columnCount, token,
aligns, t, tableLines, tbodyLines; aligns, t, tableLines, tbodyLines;
// should have at least three lines // should have at least three lines
@ -75,15 +75,14 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, startLine + 1); lineText = getLine(state, startLine + 1);
if (!/^[-:| ]+$/.test(lineText)) { return false; } if (!/^[-:| ]+$/.test(lineText)) { return false; }
rows = lineText.split('|'); columns = lineText.split('|');
if (rows.length < 2) { return false; }
aligns = []; aligns = [];
for (i = 0; i < rows.length; i++) { for (i = 0; i < columns.length; i++) {
t = rows[i].trim(); t = columns[i].trim();
if (!t) { if (!t) {
// allow empty columns before and after table, but not in between columns; // allow empty columns before and after table, but not in between columns;
// e.g. allow ` |---| `, disallow ` ---||--- ` // e.g. allow ` |---| `, disallow ` ---||--- `
if (i === 0 || i === rows.length - 1) { if (i === 0 || i === columns.length - 1) {
continue; continue;
} else { } else {
return false; return false;
@ -102,8 +101,13 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, startLine).trim(); lineText = getLine(state, startLine).trim();
if (lineText.indexOf('|') === -1) { return false; } if (lineText.indexOf('|') === -1) { return false; }
rows = escapedSplit(lineText.replace(/^\||\|$/g, '')); columns = escapedSplit(lineText.replace(/^\||\|$/g, ''));
if (aligns.length !== rows.length) { return false; }
// header row will define an amount of columns in the entire table,
// and align row shouldn't be smaller than that (the rest of the rows can)
columnCount = columns.length;
if (columnCount > aligns.length) { return false; }
if (silent) { return true; } if (silent) { return true; }
token = state.push('table_open', 'table', 1); token = state.push('table_open', 'table', 1);
@ -115,7 +119,7 @@ module.exports = function table(state, startLine, endLine, silent) {
token = state.push('tr_open', 'tr', 1); token = state.push('tr_open', 'tr', 1);
token.map = [ startLine, startLine + 1 ]; token.map = [ startLine, startLine + 1 ];
for (i = 0; i < rows.length; i++) { for (i = 0; i < columns.length; i++) {
token = state.push('th_open', 'th', 1); token = state.push('th_open', 'th', 1);
token.map = [ startLine, startLine + 1 ]; token.map = [ startLine, startLine + 1 ];
if (aligns[i]) { if (aligns[i]) {
@ -123,7 +127,7 @@ module.exports = function table(state, startLine, endLine, silent) {
} }
token = state.push('inline', '', 0); token = state.push('inline', '', 0);
token.content = rows[i].trim(); token.content = columns[i].trim();
token.map = [ startLine, startLine + 1 ]; token.map = [ startLine, startLine + 1 ];
token.children = []; token.children = [];
@ -141,20 +145,17 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, nextLine).trim(); lineText = getLine(state, nextLine).trim();
if (lineText.indexOf('|') === -1) { break; } if (lineText.indexOf('|') === -1) { break; }
rows = escapedSplit(lineText.replace(/^\||\|$/g, '')); columns = escapedSplit(lineText.replace(/^\||\|$/g, ''));
// set number of columns to number of columns in header row
rows.length = aligns.length;
token = state.push('tr_open', 'tr', 1); token = state.push('tr_open', 'tr', 1);
for (i = 0; i < rows.length; i++) { for (i = 0; i < columnCount; i++) {
token = state.push('td_open', 'td', 1); token = state.push('td_open', 'td', 1);
if (aligns[i]) { if (aligns[i]) {
token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ]; token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ];
} }
token = state.push('inline', '', 0); token = state.push('inline', '', 0);
token.content = rows[i] ? rows[i].trim() : ''; token.content = columns[i] ? columns[i].trim() : '';
token.children = []; token.children = [];
token = state.push('td_close', 'td', -1); token = state.push('td_close', 'td', -1);

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

@ -225,18 +225,6 @@ bar|bar</p>
. .
Title line should have correct column count:
.
foo|foo
---|---|---
bar|bar
.
<p>foo|foo
—|---|—
bar|bar</p>
.
Should terminate paragraph: Should terminate paragraph:
. .
paragraph paragraph
@ -390,54 +378,94 @@ Another complicated backticks case
. .
Add/remove columns to match the first row: Should be parsed before code blocks:
. .
Heading 1|Heading 2|Heading 3 foo | bar
---|---|--- ----- | -----
1|2 aaa | bbb
1|2|3|4
. .
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Heading 1</th> <th>foo</th>
<th>Heading 2</th> <th>bar</th>
<th>Heading 3</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>1</td> <td>aaa</td>
<td>2</td> <td>bbb</td>
<td></td>
</tr> </tr>
</tbody>
</table>
.
An amount of rows might be different across the table (issue #171):
.
| 1 | 2 |
| :-----: | :-----: | :-----: |
| 3 | 4 | 5 | 6 |
.
<table>
<thead>
<tr> <tr>
<td>1</td> <th style="text-align:center">1</th>
<td>2</td> <th style="text-align:center">2</th>
<td>3</td> </tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">4</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
. .
Should be parsed before code blocks: An amount of rows might be different across the table #2:
. .
foo | bar | 1 | 2 | 3 | 4 |
----- | ----- | :-----: | :-----: | :-----: | :-----: |
aaa | bbb | 5 | 6 |
. .
<table> <table>
<thead> <thead>
<tr> <tr>
<th>foo</th> <th style="text-align:center">1</th>
<th>bar</th> <th style="text-align:center">2</th>
<th style="text-align:center">3</th>
<th style="text-align:center">4</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>aaa</td> <td style="text-align:center">5</td>
<td>bbb</td> <td style="text-align:center">6</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
.
Allow one-column tables (issue #171):
.
| foo |
:-----:
| bar |
.
<table>
<thead>
<tr>
<th style="text-align:center">foo</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">bar</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

Loading…
Cancel
Save