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) {
var ch, lineText, pos, i, nextLine, rows, token,
var ch, lineText, pos, i, nextLine, columns, columnCount, token,
aligns, t, tableLines, tbodyLines;
// should have at least three lines
@ -75,15 +75,14 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, startLine + 1);
if (!/^[-:| ]+$/.test(lineText)) { return false; }
rows = lineText.split('|');
if (rows.length < 2) { return false; }
columns = lineText.split('|');
aligns = [];
for (i = 0; i < rows.length; i++) {
t = rows[i].trim();
for (i = 0; i < columns.length; i++) {
t = columns[i].trim();
if (!t) {
// allow empty columns before and after table, but not in between columns;
// e.g. allow ` |---| `, disallow ` ---||--- `
if (i === 0 || i === rows.length - 1) {
if (i === 0 || i === columns.length - 1) {
continue;
} else {
return false;
@ -102,8 +101,13 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, startLine).trim();
if (lineText.indexOf('|') === -1) { return false; }
rows = escapedSplit(lineText.replace(/^\||\|$/g, ''));
if (aligns.length !== rows.length) { return false; }
columns = escapedSplit(lineText.replace(/^\||\|$/g, ''));
// 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; }
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.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.map = [ startLine, startLine + 1 ];
if (aligns[i]) {
@ -123,7 +127,7 @@ module.exports = function table(state, startLine, endLine, silent) {
}
token = state.push('inline', '', 0);
token.content = rows[i].trim();
token.content = columns[i].trim();
token.map = [ startLine, startLine + 1 ];
token.children = [];
@ -141,20 +145,17 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, nextLine).trim();
if (lineText.indexOf('|') === -1) { break; }
rows = escapedSplit(lineText.replace(/^\||\|$/g, ''));
// set number of columns to number of columns in header row
rows.length = aligns.length;
columns = escapedSplit(lineText.replace(/^\||\|$/g, ''));
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);
if (aligns[i]) {
token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ];
}
token = state.push('inline', '', 0);
token.content = rows[i] ? rows[i].trim() : '';
token.content = columns[i] ? columns[i].trim() : '';
token.children = [];
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:
.
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
---|---|---
1|2
1|2|3|4
foo | bar
----- | -----
aaa | bbb
.
<table>
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td></td>
<td>aaa</td>
<td>bbb</td>
</tr>
</tbody>
</table>
.
An amount of rows might be different across the table (issue #171):
.
| 1 | 2 |
| :-----: | :-----: | :-----: |
| 3 | 4 | 5 | 6 |
.
<table>
<thead>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<th style="text-align:center">1</th>
<th style="text-align:center">2</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">4</td>
</tr>
</tbody>
</table>
.
Should be parsed before code blocks:
An amount of rows might be different across the table #2:
.
foo | bar
----- | -----
aaa | bbb
| 1 | 2 | 3 | 4 |
| :-----: | :-----: | :-----: | :-----: |
| 5 | 6 |
.
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
<th style="text-align:center">1</th>
<th style="text-align:center">2</th>
<th style="text-align:center">3</th>
<th style="text-align:center">4</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaa</td>
<td>bbb</td>
<td style="text-align:center">5</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>
</tbody>
</table>

Loading…
Cancel
Save