diff --git a/lib/marked.js b/lib/marked.js index d48997c8..aa9df030 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -63,8 +63,8 @@ block.normal = { block.gfm = { fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/, paragraph: /^/, - table: /^ {0,3}[|](.+)\n {0,3}[|]( *[-:]+[-| :]*)\n((?: *[|].*\n)*)\n*/, - nptable: /^ {0,3}(\S.*[|].*)\n {0,3}([-:]+ *[|][-| :]*)\n((?:.*[|].*\n)*)\n*/ + table: /^ {0,3}\|(.+)\n {0,3}\|( *[-:]+[-| :]*)\n((?: *\|.*\n)*)\n*/, + nptable: /^ {0,3}(\S.*\|.*)\n {0,3}([-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*)\n*/ }; block.gfm.paragraph = replace(block.paragraph) @@ -137,15 +137,18 @@ block.token = function(src, tokens, top) { src = src.substring(cap[0].length); tokens.push({ type: 'table', - header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */), - align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){ - return row.match(/^ *-+: *$/) ? "right" - : row.match(/^ *:-+: *$/) ? "center" - : row.match(/^ *:-+ *$/) ? "left" - : false; + header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */), + align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) { + return /^ *-+: *$/.test(row) + ? 'right' + : /^ *:-+: *$/.test(row) + ? 'center' + : /^ *:-+ *$/.test(row) + ? 'left' + : false; }), - cells: cap[3].replace(/( *[|] *)?\n$/, '').split("\n").map(function(row){ - return row.replace(/(^ *[|] *| *[|] *$)/g, '').split(/ *[|] */); + cells: cap[3].replace(/( *\| *)?\n$/, '').split('\n').map(function(row) { + return row.replace(/(^ *\| *| *\| *$)/g, '').split(/ *\| */); }) }); continue; @@ -156,15 +159,18 @@ block.token = function(src, tokens, top) { src = src.substring(cap[0].length); tokens.push({ type: 'table', - header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */), - align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){ - return row.match(/^ *-+: *$/) ? "right" - : row.match(/^ *:-+: *$/) ? "center" - : row.match(/^ *:-+ *$/) ? "left" - : false; + header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */), + align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) { + return /^ *-+: *$/.test(row) + ? 'right' + : /^ *:-+: *$/.test(row) + ? 'center' + : /^ *:-+ *$/.test(row) + ? 'left' + : false; }), - cells: cap[3].replace(/\n$/, '').split("\n").map(function(row){ - return row.split(/ *[|] */); + cells: cap[3].replace(/\n$/, '').split('\n').map(function(row) { + return row.split(/ *\| */); }) }); continue; @@ -605,13 +611,17 @@ function tok() { } case 'table': { var thead = '\t\n\t\t'; - token.header.forEach(function(heading, i){ + token.header.forEach(function(heading, i) { heading = inline.lexer(heading); - var align = i < token.align.length ? token.align[i] : false; - switch(align){ - case "left": - case "right": - case "center": + + var align = i < token.align.length + ? token.align[i] + : false; + + switch (align) { + case 'left': + case 'right': + case 'center': thead += '' + heading + ''; break; default: @@ -621,15 +631,19 @@ function tok() { thead += '\n\t\n'; var tbody = '\t\n'; - token.cells.forEach(function(row){ + token.cells.forEach(function(row) { tbody += '\t\t'; - row.forEach(function(cell, i){ + row.forEach(function(cell, i) { cell = inline.lexer(cell); - var align = i < token.align.length ? token.align[i] : false; - switch(align){ - case "left": - case "right": - case "center": + + var align = i < token.align.length + ? token.align[i] + : false; + + switch (align) { + case 'left': + case 'right': + case 'center': tbody += '' + cell + ''; break; default: