Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nptable tokenizer #2126

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 0 additions & 7 deletions src/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ module.exports = class Lexer {
continue;
}

// table no leading pipe (gfm)
if (token = this.tokenizer.nptable(src)) {
src = src.substring(token.raw.length);
tokens.push(token);
continue;
}

// hr
if (token = this.tokenizer.hr(src)) {
src = src.substring(token.raw.length);
Expand Down
40 changes: 1 addition & 39 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,42 +134,6 @@ module.exports = class Tokenizer {
}
}

nptable(src) {
const cap = this.rules.block.nptable.exec(src);
if (cap) {
const item = {
type: 'table',
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : [],
raw: cap[0]
};

if (item.header.length === item.align.length) {
let l = item.align.length;
let i;
for (i = 0; i < l; i++) {
if (/^ *-+: *$/.test(item.align[i])) {
item.align[i] = 'right';
} else if (/^ *:-+: *$/.test(item.align[i])) {
item.align[i] = 'center';
} else if (/^ *:-+ *$/.test(item.align[i])) {
item.align[i] = 'left';
} else {
item.align[i] = null;
}
}

l = item.cells.length;
for (i = 0; i < l; i++) {
item.cells[i] = splitCells(item.cells[i], item.header.length);
}

return item;
}
}
}

hr(src) {
const cap = this.rules.block.hr.exec(src);
if (cap) {
Expand Down Expand Up @@ -385,9 +349,7 @@ module.exports = class Tokenizer {

l = item.cells.length;
for (i = 0; i < l; i++) {
item.cells[i] = splitCells(
item.cells[i].replace(/^ *\| *| *\| *$/g, ''),
item.header.length);
item.cells[i] = splitCells(item.cells[i], item.header.length);
}

return item;
Expand Down
4 changes: 4 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function splitCells(tableRow, count) {
cells = row.split(/ \|/);
let i = 0;

// First/last cell in a row cannot be empty if it has no leading/trailing pipe
if (!cells[0].trim()) { cells.shift(); }
if (!cells[cells.length - 1].trim()) { cells.pop(); }

if (cells.length > count) {
cells.splice(count);
} else {
Expand Down
19 changes: 2 additions & 17 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const block = {
+ '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
+ ')',
def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
nptable: noopTest,
table: noopTest,
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
// regex template, placeholders will be replaced according to different paragraph
Expand Down Expand Up @@ -97,25 +96,11 @@ block.normal = merge({}, block);
*/

block.gfm = merge({}, block.normal, {
nptable: '^ *([^|\\n ].*\\|.*)\\n' // Header
+ ' {0,3}([-:]+ *\\|[-| :]*)' // Align
+ '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)', // Cells
table: '^ *\\|(.+)\\n' // Header
+ ' {0,3}\\|?( *[-:]+[-| :]*)' // Align
table: '^ *([^\\n ].*\\|.*)\\n' // Header
+ ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)\\|?' // Align
+ '(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
});

block.gfm.nptable = edit(block.gfm.nptable)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('blockquote', ' {0,3}>')
.replace('code', ' {4}[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
.getRegex();

block.gfm.table = edit(block.gfm.table)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
Expand Down
2 changes: 1 addition & 1 deletion test/specs/new/table_cells.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1|\</td><td>2|\</td></tr></tbody></table>

<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>2</td><td></td></tr></tbody></table>

<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2|</td></tr></tbody></table>

Expand Down