Skip to content

Commit

Permalink
fix(tables): raw html inside code tags in tables no longer breaks tables
Browse files Browse the repository at this point in the history
Under certains conditions, raw html inside code tags in tables would break
table parsing. This commit fixes that.

Closes #471
  • Loading branch information
tivie committed Dec 5, 2017
1 parent 32541e6 commit 4ef4c5e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 43 deletions.
27 changes: 7 additions & 20 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.

6 changes: 3 additions & 3 deletions src/subParsers/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ showdown.subParser('lists', function (text, options, globals) {
style = styleStartNumber(list, listType);
if (pos !== -1) {
// slice
result += '\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';

// invert counterType and listType
listType = (listType === 'ul') ? 'ol' : 'ul';
Expand All @@ -163,12 +163,12 @@ showdown.subParser('lists', function (text, options, globals) {
//recurse
parseCL(txt.slice(pos));
} else {
result += '\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
}
})(list);
} else {
var style = styleStartNumber(list, listType);
result = '\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
}

return result;
Expand Down
19 changes: 3 additions & 16 deletions src/subParsers/tables.js
Original file line number Diff line number Diff line change
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|<ol|<ul|¨0)/gm,
var tableRgx = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨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}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|<ol|<ul|¨0)/gm;
singeColTblRgx = /^ {0,3}\|.+\|[ \t]*\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n( {0,3}\|.+\|[ \t]*\n)*(?:\n|¨0)/gm;

function parseStyles (sLine) {
if (/^:[ \t]*--*$/.test(sLine)) {
Expand All @@ -24,7 +24,7 @@ showdown.subParser('tables', function (text, options, globals) {
function parseHeaders (header, style) {
var id = '';
header = header.trim();
// support both tablesHeaderId and tableHeaderId due to error in documention so we don't break backwards compatibility
// support both tablesHeaderId and tableHeaderId due to error in documentation so we don't break backwards compatibility
if (options.tablesHeaderId || options.tableHeaderId) {
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
}
Expand Down Expand Up @@ -125,24 +125,11 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<table>
<thead>
<tr>
<th style="text-align:right;">h1</th>
<th style="text-align:left;">h2</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;">asdf</td>
<td style="text-align:left;">one <code>two &lt;ol&gt; three</code></td>
</tr>
</tbody>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| h1 | h2 |
|--------:|:---------------------|
| asdf | one `two <ol> three` |

0 comments on commit 4ef4c5e

Please sign in to comment.