Skip to content

Commit

Permalink
fix(tables): tables are properly rendered when followed by a single l…
Browse files Browse the repository at this point in the history
…inebreak and a list

Closes #443
  • Loading branch information
tivie committed Oct 6, 2017
1 parent a207da1 commit d88b095
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 9 deletions.
19 changes: 16 additions & 3 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/subParsers/tables.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down
@@ -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>
@@ -0,0 +1,7 @@
| Tables |
| ------------- |
| **col 3 is** |
| col 2 is |
| zebra stripes |

1. test
@@ -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>
@@ -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

0 comments on commit d88b095

Please sign in to comment.