Skip to content

Commit fef110c

Browse files
committed
Fix(tables): allow for one column table
Closes #406
1 parent 8f05be7 commit fef110c

7 files changed

+142
-23
lines changed

dist/showdown.js

+16-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/tables.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
55
return text;
66
}
77

8-
var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{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|¨0)/gm,
9+
//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;
911

1012
function parseStyles (sLine) {
1113
if (/^:[ \t]*--*$/.test(sLine)) {
@@ -56,14 +58,7 @@ showdown.subParser('tables', function (text, options, globals) {
5658
return tb;
5759
}
5860

59-
text = globals.converter._dispatch('tables.before', text, options, globals);
60-
61-
// find escaped pipe characters
62-
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
63-
64-
// parse tables
65-
text = text.replace(tableRgx, function (rawTable) {
66-
61+
function parseTable (rawTable) {
6762
var i, tableLines = rawTable.split('\n');
6863

6964
// strip wrong first and last column if wrapped tables are used
@@ -126,7 +121,18 @@ showdown.subParser('tables', function (text, options, globals) {
126121
}
127122

128123
return buildTable(headers, cells);
129-
});
124+
}
125+
126+
text = globals.converter._dispatch('tables.before', text, options, globals);
127+
128+
// find escaped pipe characters
129+
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
130+
131+
// parse multi column tables
132+
text = text.replace(tableRgx, parseTable);
133+
134+
// parse one column tables
135+
text = text.replace(singeColTblRgx, parseTable);
130136

131137
text = globals.converter._dispatch('tables.after', text, options, globals);
132138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>some header</th>
5+
</tr>
6+
</thead>
7+
<tbody>
8+
<tr>
9+
<td>some content</td>
10+
</tr>
11+
</tbody>
12+
</table>
13+
<table>
14+
<thead>
15+
<tr>
16+
<th>some header</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
</tbody>
21+
</table>
22+
<table>
23+
<thead>
24+
<tr>
25+
<th>some header</th>
26+
</tr>
27+
</thead>
28+
<tbody>
29+
<tr>
30+
<td>some content</td>
31+
</tr>
32+
<tr>
33+
<td>some content</td>
34+
</tr>
35+
<tr>
36+
<td>some content</td>
37+
</tr>
38+
<tr>
39+
<td>some content</td>
40+
</tr>
41+
<tr>
42+
<td>some content</td>
43+
</tr>
44+
</tbody>
45+
</table>
46+
<table>
47+
<thead>
48+
<tr>
49+
<th style="text-align:left;">some header</th>
50+
</tr>
51+
</thead>
52+
<tbody>
53+
<tr>
54+
<td style="text-align:left;">some content</td>
55+
</tr>
56+
</tbody>
57+
</table>
58+
<table>
59+
<thead>
60+
<tr>
61+
<th style="text-align:right;">some header</th>
62+
</tr>
63+
</thead>
64+
<tbody>
65+
<tr>
66+
<td style="text-align:right;">some content</td>
67+
</tr>
68+
</tbody>
69+
</table>
70+
<table>
71+
<thead>
72+
<tr>
73+
<th style="text-align:center;">some header</th>
74+
</tr>
75+
</thead>
76+
<tbody>
77+
<tr>
78+
<td style="text-align:center;">some content</td>
79+
</tr>
80+
</tbody>
81+
</table>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
|some header |
2+
|------------|
3+
|some content|
4+
5+
|some header |
6+
|------------|
7+
8+
|some header |
9+
|------------|
10+
|some content|
11+
|some content|
12+
|some content|
13+
|some content|
14+
|some content|
15+
16+
|some header |
17+
|:-----------|
18+
|some content|
19+
20+
|some header |
21+
|-----------:|
22+
|some content|
23+
24+
|some header |
25+
|:----------:|
26+
|some content|

0 commit comments

Comments
 (0)