fix(tables): tables are properly rendered when followed by a single linebreak and a list

Closes #443
This commit is contained in:
Estevao Soares dos Santos 2017-10-06 12:38:01 +01:00
parent a207da1d5f
commit d88b095f05
9 changed files with 79 additions and 2 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -5,9 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
return text;
}
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
//singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|¨0)/gm;
singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|<ol|<ul|¨0)/gm;
function parseStyles (sLine) {
if (/^:[ \t]*--*$/.test(sLine)) {
@ -123,11 +123,24 @@ showdown.subParser('tables', function (text, options, globals) {
return buildTable(headers, cells);
}
function hackFixTableFollowedByList (rawTable) {
var lastChars = rawTable.slice(-3);
if (lastChars === '<ol' || lastChars === '<ul') {
rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
}
return rawTable;
}
text = globals.converter._dispatch('tables.before', text, options, globals);
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
// tables not being parsed when followed by a list
text = text.replace(tableRgx, hackFixTableFollowedByList);
text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
// parse multi column tables
text = text.replace(tableRgx, parseTable);

View File

@ -0,0 +1,21 @@
<table>
<thead>
<tr>
<th>Tables</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>col 3 is</strong></td>
</tr>
<tr>
<td>col 2 is</td>
</tr>
<tr>
<td>zebra stripes</td>
</tr>
</tbody>
</table>
<ol>
<li>test</li>
</ol>

View File

@ -0,0 +1,7 @@
| Tables |
| ------------- |
| **col 3 is** |
| col 2 is |
| zebra stripes |
1. test

View File

@ -0,0 +1,29 @@
<table>
<thead>
<tr>
<th>Tables</th>
<th style="text-align:center;">Are</th>
<th style="text-align:right;">Cool</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>col 3 is</strong></td>
<td style="text-align:center;">right-aligned</td>
<td style="text-align:right;">$1600</td>
</tr>
<tr>
<td>col 2 is</td>
<td style="text-align:center;"><em>centered</em></td>
<td style="text-align:right;">$12</td>
</tr>
<tr>
<td>zebra stripes</td>
<td style="text-align:center;">are neat</td>
<td style="text-align:right;">$1</td>
</tr>
</tbody>
</table>
<ol>
<li>test</li>
</ol>

View File

@ -0,0 +1,7 @@
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| **col 3 is** | right-aligned | $1600 |
| col 2 is | *centered* | $12 |
| zebra stripes | are neat | $1 |
1. test