Skip to content

Commit 8bd2e44

Browse files
authored
Merge pull request markedjs#1245 from tomtheisen/gfm-tables
Gfm tables
2 parents c294d5c + 0245c55 commit 8bd2e44

File tree

4 files changed

+62
-51
lines changed

4 files changed

+62
-51
lines changed

lib/marked.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ block.gfm.paragraph = edit(block.paragraph)
107107
*/
108108

109109
block.tables = merge({}, block.gfm, {
110-
nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
111-
table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
110+
nptable: /^ *([^|\n ].*\|.*)\n *([-:]+ *\|[-| :]*)(?:\n((?:.*[^>\n ].*(?:\n|$))*)\n*|$)/,
111+
table: /^ *\|(.+)\n *\|?( *[-:]+[-| :]*)(?:\n((?: *[^>\n ].*(?:\n|$))*)\n*|$)/
112112
});
113113

114114
/**
@@ -245,34 +245,36 @@ Lexer.prototype.token = function(src, top) {
245245

246246
// table no leading pipe (gfm)
247247
if (top && (cap = this.rules.nptable.exec(src))) {
248-
src = src.substring(cap[0].length);
249-
250248
item = {
251249
type: 'table',
252250
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
253251
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
254-
cells: cap[3].replace(/\n$/, '').split('\n')
252+
cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
255253
};
256254

257-
for (i = 0; i < item.align.length; i++) {
258-
if (/^ *-+: *$/.test(item.align[i])) {
259-
item.align[i] = 'right';
260-
} else if (/^ *:-+: *$/.test(item.align[i])) {
261-
item.align[i] = 'center';
262-
} else if (/^ *:-+ *$/.test(item.align[i])) {
263-
item.align[i] = 'left';
264-
} else {
265-
item.align[i] = null;
255+
if (item.header.length === item.align.length) {
256+
src = src.substring(cap[0].length);
257+
258+
for (i = 0; i < item.align.length; i++) {
259+
if (/^ *-+: *$/.test(item.align[i])) {
260+
item.align[i] = 'right';
261+
} else if (/^ *:-+: *$/.test(item.align[i])) {
262+
item.align[i] = 'center';
263+
} else if (/^ *:-+ *$/.test(item.align[i])) {
264+
item.align[i] = 'left';
265+
} else {
266+
item.align[i] = null;
267+
}
266268
}
267-
}
268269

269-
for (i = 0; i < item.cells.length; i++) {
270-
item.cells[i] = splitCells(item.cells[i]);
271-
}
270+
for (i = 0; i < item.cells.length; i++) {
271+
item.cells[i] = splitCells(item.cells[i], item.header.length);
272+
}
272273

273-
this.tokens.push(item);
274+
this.tokens.push(item);
274275

275-
continue;
276+
continue;
277+
}
276278
}
277279

278280
// hr
@@ -412,35 +414,38 @@ Lexer.prototype.token = function(src, top) {
412414

413415
// table (gfm)
414416
if (top && (cap = this.rules.table.exec(src))) {
415-
src = src.substring(cap[0].length);
416-
417417
item = {
418418
type: 'table',
419419
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
420420
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
421-
cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
421+
cells: cap[3] ? cap[3].replace(/(?: *\| *)?\n$/, '').split('\n') : []
422422
};
423423

424-
for (i = 0; i < item.align.length; i++) {
425-
if (/^ *-+: *$/.test(item.align[i])) {
426-
item.align[i] = 'right';
427-
} else if (/^ *:-+: *$/.test(item.align[i])) {
428-
item.align[i] = 'center';
429-
} else if (/^ *:-+ *$/.test(item.align[i])) {
430-
item.align[i] = 'left';
431-
} else {
432-
item.align[i] = null;
424+
if (item.header.length === item.align.length) {
425+
src = src.substring(cap[0].length);
426+
427+
for (i = 0; i < item.align.length; i++) {
428+
if (/^ *-+: *$/.test(item.align[i])) {
429+
item.align[i] = 'right';
430+
} else if (/^ *:-+: *$/.test(item.align[i])) {
431+
item.align[i] = 'center';
432+
} else if (/^ *:-+ *$/.test(item.align[i])) {
433+
item.align[i] = 'left';
434+
} else {
435+
item.align[i] = null;
436+
}
433437
}
434-
}
435438

436-
for (i = 0; i < item.cells.length; i++) {
437-
item.cells[i] = splitCells(
438-
item.cells[i].replace(/^ *\| *| *\| *$/g, ''));
439-
}
439+
for (i = 0; i < item.cells.length; i++) {
440+
item.cells[i] = splitCells(
441+
item.cells[i].replace(/^ *\| *| *\| *$/g, ''),
442+
item.header.length);
443+
}
440444

441-
this.tokens.push(item);
445+
this.tokens.push(item);
442446

443-
continue;
447+
continue;
448+
}
444449
}
445450

446451
// lheading
@@ -927,13 +932,13 @@ Renderer.prototype.paragraph = function(text) {
927932
};
928933

929934
Renderer.prototype.table = function(header, body) {
935+
if (body) body = '<tbody>' + body + '</tbody>';
936+
930937
return '<table>\n'
931938
+ '<thead>\n'
932939
+ header
933940
+ '</thead>\n'
934-
+ '<tbody>\n'
935941
+ body
936-
+ '</tbody>\n'
937942
+ '</table>\n';
938943
};
939944

@@ -944,7 +949,7 @@ Renderer.prototype.tablerow = function(content) {
944949
Renderer.prototype.tablecell = function(content, flags) {
945950
var type = flags.header ? 'th' : 'td';
946951
var tag = flags.align
947-
? '<' + type + ' style="text-align:' + flags.align + '">'
952+
? '<' + type + ' align="' + flags.align + '">'
948953
: '<' + type + '>';
949954
return tag + content + '</' + type + '>\n';
950955
};
@@ -1310,10 +1315,16 @@ function merge(obj) {
13101315
return obj;
13111316
}
13121317

1313-
function splitCells(tableRow) {
1318+
function splitCells(tableRow, count) {
13141319
var cells = tableRow.replace(/([^\\])\|/g, '$1 |').split(/ +\| */),
13151320
i = 0;
13161321

1322+
if (cells.length > count) {
1323+
cells.splice(count);
1324+
} else {
1325+
while (cells.length < count) cells.push('');
1326+
}
1327+
13171328
for (; i < cells.length; i++) {
13181329
cells[i] = cells[i].replace(/\\\|/g, '|');
13191330
}

test/new/gfm_tables.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
</table>
1010
<table>
1111
<thead>
12-
<tr><th style="text-align:center">Header 1</th><th style="text-align:right">Header 2</th><th style="text-align:left">Header 3</th><th>Header 4</th></tr>
12+
<tr><th align="center">Header 1</th><th align="right">Header 2</th><th align="left">Header 3</th><th>Header 4</th></tr>
1313
</thead>
1414
<tbody>
15-
<tr><td style="text-align:center">Cell 1</td><td style="text-align:right">Cell 2</td><td style="text-align:left">Cell 3</td><td>Cell 4</td></tr>
16-
<tr><td style="text-align:center">Cell 5</td><td style="text-align:right">Cell 6</td><td style="text-align:left">Cell 7</td><td>Cell 8</td></tr>
15+
<tr><td align="center">Cell 1</td><td align="right">Cell 2</td><td align="left">Cell 3</td><td>Cell 4</td></tr>
16+
<tr><td align="center">Cell 5</td><td align="right">Cell 6</td><td align="left">Cell 7</td><td>Cell 8</td></tr>
1717
</tbody>
1818
</table>
1919
<pre><code>Test code</code></pre>
@@ -28,10 +28,10 @@
2828
</table>
2929
<table>
3030
<thead>
31-
<tr><th style="text-align:left">Header 1</th><th style="text-align:center">Header 2</th><th style="text-align:right">Header 3</th><th>Header 4</th></tr>
31+
<tr><th align="left">Header 1</th><th align="center">Header 2</th><th align="right">Header 3</th><th>Header 4</th></tr>
3232
</thead>
3333
<tbody>
34-
<tr><td style="text-align:left">Cell 1</td><td style="text-align:center">Cell 2</td><td style="text-align:right">Cell 3</td><td>Cell 4</td></tr>
35-
<tr><td style="text-align:left"><em>Cell 5</em></td><td style="text-align:center">Cell 6</td><td style="text-align:right">Cell 7</td><td>Cell 8</td></tr>
34+
<tr><td align="left">Cell 1</td><td align="center">Cell 2</td><td align="right">Cell 3</td><td>Cell 4</td></tr>
35+
<tr><td align="left"><em>Cell 5</em></td><td align="center">Cell 6</td><td align="right">Cell 7</td><td>Cell 8</td></tr>
3636
</tbody>
3737
</table>

test/specs/gfm/gfm-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var messenger = new Messenger();
2828
describe('GFM 0.28 Tables', function() {
2929
var section = 'Tables';
3030

31-
var shouldPassButFails = [192, 195, 196, 197];
31+
var shouldPassButFails = [];
3232

3333
var willNotBeAttemptedByCoreTeam = [];
3434

test/specs/gfm/gfm.0.28.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"section": "Tables",
4646
"html": "<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead></table>",
4747
"markdown": "| abc | def |\n| --- | --- |",
48-
"example": 197
48+
"example": 198
4949
},
5050
{
5151
"section": "Task list items",

0 commit comments

Comments
 (0)