Skip to content
34 changes: 24 additions & 10 deletions lib/kramed.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ block.gfm.paragraph = replace(block.paragraph)
*/

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

/**
Expand Down Expand Up @@ -245,9 +245,10 @@ Lexer.prototype.token = function(src, top, bq) {

item = {
type: 'table',
header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3].replace(/\n$/, '').split('\n')
caption: cap[1],
header: cap[2].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[3].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[4].replace(/\n$/, '').split('\n')
};

for (i = 0; i < item.align.length; i++) {
Expand Down Expand Up @@ -416,9 +417,10 @@ Lexer.prototype.token = function(src, top, bq) {

item = {
type: 'table',
header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n').slice(0),
caption: cap[1],
header: cap[2].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[3].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[4].replace(/(?: *\| *)?\n$/, '').split('\n').slice(0),
};

for (i = 0; i < item.align.length; i++) {
Expand Down Expand Up @@ -895,8 +897,9 @@ Renderer.prototype.paragraph = function(text) {
return '<p>' + text + '</p>\n';
};

Renderer.prototype.table = function(header, body) {
Renderer.prototype.table = function(caption, header, body) {
return '<table>\n'
+ caption
+ '<thead>\n'
+ header
+ '</thead>\n'
Expand Down Expand Up @@ -1092,12 +1095,23 @@ Parser.prototype.tok = function() {
case 'table': {
var header = ''
, body = ''
, caption
, i
, row
, cell
, flags
, j;

// caption
if (typeof this.token.caption === 'undefined') {
caption = '';
}
else {
caption = '<caption>'
+ this.inline.output( this.token.caption )
+ '</caption>\n';
}

// header
cell = '';
for (i = 0; i < this.token.header.length; i++) {
Expand All @@ -1122,7 +1136,7 @@ Parser.prototype.tok = function() {

body += this.renderer.tablerow(cell);
}
return this.renderer.table(header, body);
return this.renderer.table(caption, header, body);
}
case 'blockquote_start': {
var body = '';
Expand Down
10 changes: 10 additions & 0 deletions test/tests/gfm_tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@
<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>
</tbody>
</table>
<table>
<caption>Captioned table Multiple <em>lines</em></caption>
<thead>
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</tbody>
</table>
9 changes: 9 additions & 0 deletions test/tests/gfm_tables.text
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ Header 1|Header 2|Header 3|Header 4
:-------|:------:|-------:|--------
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8

'''
Captioned table
Multiple *lines*
'''
Header 1|Header 2
------- |--------
Cell 1 | Cell 2
Cell 3 | Cell 4