Skip to content

Commit 4ef4c5e

Browse files
committed
fix(tables): raw html inside code tags in tables no longer breaks tables
Under certains conditions, raw html inside code tags in tables would break table parsing. This commit fixes that. Closes #471
1 parent 32541e6 commit 4ef4c5e

8 files changed

Lines changed: 34 additions & 43 deletions

File tree

dist/showdown.js

Lines changed: 7 additions & 20 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/lists.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ showdown.subParser('lists', function (text, options, globals) {
154154
style = styleStartNumber(list, listType);
155155
if (pos !== -1) {
156156
// slice
157-
result += '\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
157+
result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
158158

159159
// invert counterType and listType
160160
listType = (listType === 'ul') ? 'ol' : 'ul';
@@ -163,12 +163,12 @@ showdown.subParser('lists', function (text, options, globals) {
163163
//recurse
164164
parseCL(txt.slice(pos));
165165
} else {
166-
result += '\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
166+
result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
167167
}
168168
})(list);
169169
} else {
170170
var style = styleStartNumber(list, listType);
171-
result = '\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
171+
result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
172172
}
173173

174174
return result;

src/subParsers/tables.js

Lines changed: 3 additions & 16 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|<ol|<ul|¨0)/gm,
8+
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨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}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|<ol|<ul|¨0)/gm;
10+
singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;
1111

1212
function parseStyles (sLine) {
1313
if (/^:[ \t]*--*$/.test(sLine)) {
@@ -24,7 +24,7 @@ showdown.subParser('tables', function (text, options, globals) {
2424
function parseHeaders (header, style) {
2525
var id = '';
2626
header = header.trim();
27-
// support both tablesHeaderId and tableHeaderId due to error in documention so we don't break backwards compatibility
27+
// support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
2828
if (options.tablesHeaderId || options.tableHeaderId) {
2929
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
3030
}
@@ -125,24 +125,11 @@ showdown.subParser('tables', function (text, options, globals) {
125125
return buildTable(headers, cells);
126126
}
127127

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

138130
// find escaped pipe characters
139131
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
140132

141-
// hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
142-
// tables not being parsed when followed by a list
143-
text = text.replace(tableRgx, hackFixTableFollowedByList);
144-
text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
145-
146133
// parse multi column tables
147134
text = text.replace(tableRgx, parseTable);
148135

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th style="text-align:right;">h1</th>
5+
<th style="text-align:left;">h2</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td style="text-align:right;">asdf</td>
11+
<td style="text-align:left;">one <code>two &lt;ol&gt; three</code></td>
12+
</tr>
13+
</tbody>
14+
</table>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| h1 | h2 |
2+
|--------:|:---------------------|
3+
| asdf | one `two <ol> three` |

0 commit comments

Comments
 (0)