From 293bd903fba9d0aee92d850444aa95f3c264e1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 4 Apr 2016 14:12:15 +0200 Subject: [PATCH 1/3] Add pipe to characters to escape --- lib/rules/inline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/inline.js b/lib/rules/inline.js index 1d2fb57..fc6aa46 100644 --- a/lib/rules/inline.js +++ b/lib/rules/inline.js @@ -8,7 +8,7 @@ var noop = _utils.noop; */ var inline = { - escape: /^\\([\\`*{}\[\]()#$+\-.!_>])/, + escape: /^\\([\\`*{}\[\]()#$+\-.!_>|])/, autolink: /^<([^ >]+(@|:\/)[^ >]+)>/, url: noop, html: /^|^<(\w+(?!:\/|[^\w\s@]*@)\b)*?(?:"[^"]*"|'[^']*'|[^'">])*?>([\s\S]*?)?<\/\1>|^<(\w+(?!:\/|[^\w\s@]*@)\b)(?:"[^"]*"|'[^']*'|[^'">])*?>/, From d657543399137cc2b3c5b08324ee9907df72785e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 4 Apr 2016 15:02:27 +0200 Subject: [PATCH 2/3] Convert escaped pipe as html entities --- lib/lex/block.js | 34 ++++++++++++++++++++++-------- lib/rules/table.js | 43 ++++++++++++++++++++++++++++++++++++++ test/tests/table_pipe.html | 5 +++++ test/tests/table_pipe.text | 4 ++++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 lib/rules/table.js create mode 100644 test/tests/table_pipe.html create mode 100644 test/tests/table_pipe.text diff --git a/lib/lex/block.js b/lib/lex/block.js index 96fc6f6..f9b9605 100644 --- a/lib/lex/block.js +++ b/lib/lex/block.js @@ -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'); /** @@ -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++) { @@ -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); @@ -317,9 +327,15 @@ 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, "|") + .replace(tableRules.trailingPipe, '') + .split(tableRules.cell), + align: cap[2].replace(/\\\|/g, "|") + .replace(tableRules.trailingPipeAlign, '') + .split(tableRules.cell), + cells: cap[3].replace(/\\\|/g, "|") + .replace(tableRules.trailingPipeCell, '') + .split('\n').slice(0), }; for (i = 0; i < item.align.length; i++) { @@ -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); diff --git a/lib/rules/table.js b/lib/rules/table.js new file mode 100644 index 0000000..53cfb31 --- /dev/null +++ b/lib/rules/table.js @@ -0,0 +1,43 @@ +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 +}; + +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; diff --git a/test/tests/table_pipe.html b/test/tests/table_pipe.html new file mode 100644 index 0000000..842d6f4 --- /dev/null +++ b/test/tests/table_pipe.html @@ -0,0 +1,5 @@ + + + + +
MyTable
12
3| 4
diff --git a/test/tests/table_pipe.text b/test/tests/table_pipe.text new file mode 100644 index 0000000..f35542d --- /dev/null +++ b/test/tests/table_pipe.text @@ -0,0 +1,4 @@ +| My | Table | +| --- | --- | +| 1 | 2 | +| 3 | \| 4 | From af7f7b32b237175c88a88609afb90928e9e4d136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 4 Apr 2016 15:17:57 +0200 Subject: [PATCH 3/3] Add all rules for table to lib/rules/table --- lib/lex/block.js | 6 +++--- lib/rules/table.js | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/lex/block.js b/lib/lex/block.js index f9b9605..3efc6ab 100644 --- a/lib/lex/block.js +++ b/lib/lex/block.js @@ -339,11 +339,11 @@ Lexer.prototype.token = function(src, top, bq) { }; 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; diff --git a/lib/rules/table.js b/lib/rules/table.js index 53cfb31..9f0fbd1 100644 --- a/lib/rules/table.js +++ b/lib/rules/table.js @@ -15,7 +15,13 @@ var table = { trailingPipeCell: /(?: *pipe *)?\n$/, // Remove edge pipes of a cell - edgePipesCell: /^ *pipe *| *pipe *$/g + edgePipesCell: /^ *pipe *| *pipe *$/g, + + + // Alignements + alignRight: /^ *-+: *$/, + alignCenter: /^ *:-+: *$/, + alignLeft: /^ *:-+ *$/ }; var pipe = /\|/;