Browse Source

allow tables with tbody rows longer than thead

The way that GitHub.com does this (truncating all rows that are longer
than the thead row) is stupid. It hides content that could be important
to readers, without so much as a parser error. And that can be a pain
when you're not carefully checking what your markdown looks like after
compilation. For example, an extra cell jutting out of an otherwise
rectangular table, or a missing header is an easy error to spot, while a
column missing still leaves the table looking correct and orderly.

Even if you are looking at the output in a graphical/rendered form while
you're editing, it's annoying to start a table cell in a new column and
not be able to see what you are typing until you add a header for that
column.
pull/223/head
Sean Lang 9 years ago
parent
commit
977a456e2c
  1. 5
      lib/rules_block/table.js
  2. 4
      test/fixtures/markdown-it/tables.txt

5
lib/rules_block/table.js

@ -55,7 +55,7 @@ function escapedSplit(str) {
module.exports = function table(state, startLine, endLine, silent) {
var ch, lineText, pos, i, nextLine, columns, columnCount, token,
aligns, t, tableLines, tbodyLines;
aligns, t, tableLines, tbodyLines, len;
// should have at least three lines
if (startLine + 2 > endLine) { return false; }
@ -151,7 +151,8 @@ module.exports = function table(state, startLine, endLine, silent) {
columns = escapedSplit(lineText.replace(/^\||\|\s*$/g, ''));
token = state.push('tr_open', 'tr', 1);
for (i = 0; i < columnCount; i++) {
len = Math.max(columns.length, columnCount);
for (i = 0; i < len; i++) {
token = state.push('td_open', 'td', 1);
if (aligns[i]) {
token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ];

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

@ -401,7 +401,7 @@ Should be parsed before code blocks:
.
An amount of rows might be different across the table (issue #171):
Allow tables with extra cells in a row outside of thead:
.
| 1 | 2 |
| :-----: | :-----: | :-----: |
@ -418,6 +418,8 @@ An amount of rows might be different across the table (issue #171):
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">4</td>
<td style="text-align:center">5</td>
<td>6</td>
</tr>
</tbody>
</table>

Loading…
Cancel
Save