From a261f919575d875a746abfb05e2bc967fced4c67 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 4 Nov 2014 20:22:15 +0300 Subject: [PATCH] allow one-column tables --- lib/rules_block/table.js | 44 ++++++++++++++++++----------- test/fixtures/remarkable/tables.txt | 17 +++++++++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/rules_block/table.js b/lib/rules_block/table.js index 9c84cfb..064a70b 100644 --- a/lib/rules_block/table.js +++ b/lib/rules_block/table.js @@ -3,16 +3,16 @@ 'use strict'; -function lineMatch(state, line, reg) { +function getLine(state, line) { var pos = state.bMarks[line] + state.blkIndent, max = state.eMarks[line]; - return state.src.substr(pos, max - pos).match(reg); + return state.src.substr(pos, max - pos); } module.exports = function table(state, startLine, endLine, silent) { - var ch, firstLineMatch, secondLineMatch, pos, i, nextLine, m, rows, + var ch, lineText, pos, i, nextLine, rows, aligns, t, tableLines, tbodyLines; // should have at least three lines @@ -30,27 +30,37 @@ module.exports = function table(state, startLine, endLine, silent) { ch = state.src.charCodeAt(pos); if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */) { return false; } - secondLineMatch = lineMatch(state, startLine + 1, - /^ *\|?(( *[:-]-+[:-] *\|)+( *[:-]-+[:-] *))\|? *$/); - if (!secondLineMatch) { return false; } + lineText = getLine(state, startLine + 1); + if (!/^[-:| ]+$/.test(lineText)) { return false; } - rows = secondLineMatch[1].split('|'); + rows = lineText.split('|'); + if (rows <= 2) { return false; } aligns = []; for (i = 0; i < rows.length; i++) { t = rows[i].trim(); + if (!t) { + // allow empty columns before and after table, but not in between columns; + // e.g. allow ` |---| `, disallow ` ---||--- ` + if (i === 0 || i === rows.length - 1) { + continue; + } else { + return false; + } + } + + if (!/^:?-+:?$/.test(t)) { return false; } if (t.charCodeAt(t.length - 1) === 0x3A/* : */) { - aligns[i] = t.charCodeAt(0) === 0x3A/* : */ ? 'center' : 'right'; + aligns.push(t.charCodeAt(0) === 0x3A/* : */ ? 'center' : 'right'); } else if (t.charCodeAt(0) === 0x3A/* : */) { - aligns[i] = 'left'; + aligns.push('left'); } else { - aligns[i] = ''; + aligns.push(''); } } - firstLineMatch = lineMatch(state, startLine, /^ *\|?(.*?\|.*?)\|? *$/); - if (!firstLineMatch) { return false; } - - rows = firstLineMatch[1].split('|'); + lineText = getLine(state, startLine).trim(); + if (lineText.indexOf('|') === -1) { return false; } + rows = lineText.replace(/^\||\|$/g, '').split('|'); if (aligns.length !== rows.length) { return false; } if (silent) { return true; } @@ -98,9 +108,9 @@ module.exports = function table(state, startLine, endLine, silent) { for (nextLine = startLine + 2; nextLine < endLine; nextLine++) { if (state.tShift[nextLine] < state.blkIndent) { break; } - m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/); - if (!m) { break; } - rows = m[1].split('|'); + lineText = getLine(state, nextLine).trim(); + if (lineText.indexOf('|') === -1) { break; } + rows = lineText.replace(/^\||\|$/g, '').split('|'); state.tokens.push({ type: 'tr_open', level: state.level++ }); for (i = 0; i < rows.length; i++) { diff --git a/test/fixtures/remarkable/tables.txt b/test/fixtures/remarkable/tables.txt index a7b8042..6e41515 100644 --- a/test/fixtures/remarkable/tables.txt +++ b/test/fixtures/remarkable/tables.txt @@ -110,3 +110,20 @@ Nested tables inside lists:

baz|baz

. + +Minimal one-column table test: + +. +| foo +|---- +| test2 +. + + + + + + + +
foo
test2
+.