Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions lib/lex/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var _utils = require('../utils');
var noop = _utils.noop;

var block = require('../rules/block');
var tableRules = require('../rules/table');
var defaultOptions = require('./options');

/**
Expand Down Expand Up @@ -146,9 +147,18 @@ 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')
header: cap[1]
.replace(/\\\|/g, "|")
.replace(tableRules.trailingPipe, '')
.split(tableRules.cell),
align: cap[2]
.replace(/\\\|/g, "|")
.replace(tableRules.trailingPipeAlign, '')
.split(tableRules.cell),
cells: cap[3]
.replace(/\\\|/g, "|")
.replace(/\n$/, '')
.split('\n')
};

for (i = 0; i < item.align.length; i++) {
Expand All @@ -164,7 +174,7 @@ Lexer.prototype.token = function(src, top, bq) {
}

for (i = 0; i < item.cells.length; i++) {
item.cells[i] = item.cells[i].split(/ *\| */);
item.cells[i] = item.cells[i].split(tableRules.cell);
}

this.tokens.push(item);
Expand Down Expand Up @@ -317,17 +327,23 @@ 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),
header: cap[1].replace(/\\\|/g, "&#124;")
.replace(tableRules.trailingPipe, '')
.split(tableRules.cell),
align: cap[2].replace(/\\\|/g, "&#124;")
.replace(tableRules.trailingPipeAlign, '')
.split(tableRules.cell),
cells: cap[3].replace(/\\\|/g, "&#124;")
.replace(tableRules.trailingPipeCell, '')
.split('\n').slice(0),
};

for (i = 0; i < item.align.length; i++) {
if (/^ *-+: *$/.test(item.align[i])) {
if (tableRules.alignRight.test(item.align[i])) {
item.align[i] = 'right';
} else if (/^ *:-+: *$/.test(item.align[i])) {
} else if (tableRules.alignCenter.test(item.align[i])) {
item.align[i] = 'center';
} else if (/^ *:-+ *$/.test(item.align[i])) {
} else if (tableRules.alignLeft.test(item.align[i])) {
item.align[i] = 'left';
} else {
item.align[i] = null;
Expand All @@ -336,8 +352,8 @@ Lexer.prototype.token = function(src, top, bq) {

for (i = 0; i < item.cells.length; i++) {
item.cells[i] = item.cells[i]
.replace(/^ *\| *| *\| *$/g, '')
.split(/ *\| */);
.replace(tableRules.edgePipesCell, '')
.split(tableRules.cell);
}

this.tokens.push(item);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var noop = _utils.noop;
*/

var inline = {
escape: /^\\([\\`*{}\[\]()#$+\-.!_>])/,
escape: /^\\([\\`*{}\[\]()#$+\-.!_>|])/,
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
url: noop,
html: /^<!--[\s\S]*?-->|^<(\w+(?!:\/|[^\w\s@]*@)\b)*?(?:"[^"]*"|'[^']*'|[^'">])*?>([\s\S]*?)?<\/\1>|^<(\w+(?!:\/|[^\w\s@]*@)\b)(?:"[^"]*"|'[^']*'|[^'">])*?>/,
Expand Down
49 changes: 49 additions & 0 deletions lib/rules/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var _utils = require('../utils');
var replace = _utils.replace;

var table = {
// Split a row into cells
cell: / *pipe */,

// Replace trailing pipe
trailingPipe: /^ *| *pipe *$/g,

// Remove trailing pipe of align
trailingPipeAlign: /^ *|pipe *$/g,

// Remove trailing pipe of cell
trailingPipeCell: /(?: *pipe *)?\n$/,

// Remove edge pipes of a cell
edgePipesCell: /^ *pipe *| *pipe *$/g,


// Alignements
alignRight: /^ *-+: *$/,
alignCenter: /^ *:-+: *$/,
alignLeft: /^ *:-+ *$/
};

var pipe = /\|/;

table.cell = replace(table.cell)
(/pipe/g, pipe)
();

table.trailingPipe = replace(table.trailingPipe, 'g')
(/pipe/g, pipe)
();

table.trailingPipeAlign = replace(table.trailingPipeAlign, 'g')
(/pipe/g, pipe)
();

table.trailingPipeCell = replace(table.trailingPipeCell, 'g')
(/pipe/g, pipe)
();

table.edgePipesCell = replace(table.edgePipesCell, 'g')
(/pipe/g, pipe)
();

module.exports = table;
5 changes: 5 additions & 0 deletions test/tests/table_pipe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<table>
<thead>
<tr><th>My</th><th>Table</th></tr>
</thead>
<tbody><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>&#124; 4</td></tr></tbody></table>
4 changes: 4 additions & 0 deletions test/tests/table_pipe.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| My | Table |
| --- | --- |
| 1 | 2 |
| 3 | \| 4 |