Fix(tables): allow for one column table

Closes #406
This commit is contained in:
Estevao Soares dos Santos 2017-08-05 03:34:49 +01:00
parent 8f05be7788
commit fef110cccb
7 changed files with 142 additions and 23 deletions

26
dist/showdown.js vendored
View File

@ -2785,7 +2785,9 @@ showdown.subParser('tables', function (text, options, globals) {
return text; return text;
} }
var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{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|¨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;
function parseStyles (sLine) { function parseStyles (sLine) {
if (/^:[ \t]*--*$/.test(sLine)) { if (/^:[ \t]*--*$/.test(sLine)) {
@ -2836,14 +2838,7 @@ showdown.subParser('tables', function (text, options, globals) {
return tb; return tb;
} }
text = globals.converter._dispatch('tables.before', text, options, globals); function parseTable (rawTable) {
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// parse tables
text = text.replace(tableRgx, function (rawTable) {
var i, tableLines = rawTable.split('\n'); var i, tableLines = rawTable.split('\n');
// strip wrong first and last column if wrapped tables are used // strip wrong first and last column if wrapped tables are used
@ -2906,7 +2901,18 @@ showdown.subParser('tables', function (text, options, globals) {
} }
return buildTable(headers, cells); return buildTable(headers, cells);
}); }
text = globals.converter._dispatch('tables.before', text, options, globals);
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// parse multi column tables
text = text.replace(tableRgx, parseTable);
// parse one column tables
text = text.replace(singeColTblRgx, parseTable);
text = globals.converter._dispatch('tables.after', text, options, globals); text = globals.converter._dispatch('tables.after', text, options, globals);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
return text; return text;
} }
var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{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|¨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;
function parseStyles (sLine) { function parseStyles (sLine) {
if (/^:[ \t]*--*$/.test(sLine)) { if (/^:[ \t]*--*$/.test(sLine)) {
@ -56,14 +58,7 @@ showdown.subParser('tables', function (text, options, globals) {
return tb; return tb;
} }
text = globals.converter._dispatch('tables.before', text, options, globals); function parseTable (rawTable) {
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// parse tables
text = text.replace(tableRgx, function (rawTable) {
var i, tableLines = rawTable.split('\n'); var i, tableLines = rawTable.split('\n');
// strip wrong first and last column if wrapped tables are used // strip wrong first and last column if wrapped tables are used
@ -126,7 +121,18 @@ showdown.subParser('tables', function (text, options, globals) {
} }
return buildTable(headers, cells); return buildTable(headers, cells);
}); }
text = globals.converter._dispatch('tables.before', text, options, globals);
// find escaped pipe characters
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
// parse multi column tables
text = text.replace(tableRgx, parseTable);
// parse one column tables
text = text.replace(singeColTblRgx, parseTable);
text = globals.converter._dispatch('tables.after', text, options, globals); text = globals.converter._dispatch('tables.after', text, options, globals);

View File

@ -0,0 +1,81 @@
<table>
<thead>
<tr>
<th>some header</th>
</tr>
</thead>
<tbody>
<tr>
<td>some content</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>some header</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<table>
<thead>
<tr>
<th>some header</th>
</tr>
</thead>
<tbody>
<tr>
<td>some content</td>
</tr>
<tr>
<td>some content</td>
</tr>
<tr>
<td>some content</td>
</tr>
<tr>
<td>some content</td>
</tr>
<tr>
<td>some content</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th style="text-align:left;">some header</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">some content</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th style="text-align:right;">some header</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;">some content</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th style="text-align:center;">some header</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center;">some content</td>
</tr>
</tbody>
</table>

View File

@ -0,0 +1,26 @@
|some header |
|------------|
|some content|
|some header |
|------------|
|some header |
|------------|
|some content|
|some content|
|some content|
|some content|
|some content|
|some header |
|:-----------|
|some content|
|some header |
|-----------:|
|some content|
|some header |
|:----------:|
|some content|