Browse Source

allow one-column tables

pull/14/head
Alex Kocharin 10 years ago
parent
commit
a261f91957
  1. 44
      lib/rules_block/table.js
  2. 17
      test/fixtures/remarkable/tables.txt

44
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++) {

17
test/fixtures/remarkable/tables.txt

@ -110,3 +110,20 @@ Nested tables inside lists:
</ul>
<p>baz|baz</p>
.
Minimal one-column table test:
.
| foo
|----
| test2
.
<table>
<thead>
<tr><th>foo</th></tr>
</thead>
<tbody>
<tr><td>test2</td></tr>
</tbody>
</table>
.

Loading…
Cancel
Save