Skip to content

Commit d88b095

Browse files
committed
fix(tables): tables are properly rendered when followed by a single linebreak and a list
Closes #443
1 parent a207da1 commit d88b095

9 files changed

Lines changed: 99 additions & 9 deletions

dist/showdown.js

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/tables.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
55
return text;
66
}
77

8-
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
8+
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
99
//singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
10-
singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|¨0)/gm;
10+
singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|<ol|<ul|¨0)/gm;
1111

1212
function parseStyles (sLine) {
1313
if (/^:[ \t]*--*$/.test(sLine)) {
@@ -123,11 +123,24 @@ showdown.subParser('tables', function (text, options, globals) {
123123
return buildTable(headers, cells);
124124
}
125125

126+
function hackFixTableFollowedByList (rawTable) {
127+
var lastChars = rawTable.slice(-3);
128+
if (lastChars === '<ol' || lastChars === '<ul') {
129+
rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
130+
}
131+
return rawTable;
132+
}
133+
126134
text = globals.converter._dispatch('tables.before', text, options, globals);
127135

128136
// find escaped pipe characters
129137
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
130138

139+
// hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
140+
// tables not being parsed when followed by a list
141+
text = text.replace(tableRgx, hackFixTableFollowedByList);
142+
text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
143+
131144
// parse multi column tables
132145
text = text.replace(tableRgx, parseTable);
133146

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>Tables</th>
5+
</tr>
6+
</thead>
7+
<tbody>
8+
<tr>
9+
<td><strong>col 3 is</strong></td>
10+
</tr>
11+
<tr>
12+
<td>col 2 is</td>
13+
</tr>
14+
<tr>
15+
<td>zebra stripes</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<ol>
20+
<li>test</li>
21+
</ol>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| Tables |
2+
| ------------- |
3+
| **col 3 is** |
4+
| col 2 is |
5+
| zebra stripes |
6+
7+
1. test
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>Tables</th>
5+
<th style="text-align:center;">Are</th>
6+
<th style="text-align:right;">Cool</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<td><strong>col 3 is</strong></td>
12+
<td style="text-align:center;">right-aligned</td>
13+
<td style="text-align:right;">$1600</td>
14+
</tr>
15+
<tr>
16+
<td>col 2 is</td>
17+
<td style="text-align:center;"><em>centered</em></td>
18+
<td style="text-align:right;">$12</td>
19+
</tr>
20+
<tr>
21+
<td>zebra stripes</td>
22+
<td style="text-align:center;">are neat</td>
23+
<td style="text-align:right;">$1</td>
24+
</tr>
25+
</tbody>
26+
</table>
27+
<ol>
28+
<li>test</li>
29+
</ol>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| Tables | Are | Cool |
2+
| ------------- |:-------------:| -----:|
3+
| **col 3 is** | right-aligned | $1600 |
4+
| col 2 is | *centered* | $12 |
5+
| zebra stripes | are neat | $1 |
6+
7+
1. test

0 commit comments

Comments
 (0)