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'; 'use strict';
function lineMatch(state, line, reg) { function getLine(state, line) {
var pos = state.bMarks[line] + state.blkIndent, var pos = state.bMarks[line] + state.blkIndent,
max = state.eMarks[line]; 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) { 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; aligns, t, tableLines, tbodyLines;
// should have at least three lines // should have at least three lines
@ -30,27 +30,37 @@ module.exports = function table(state, startLine, endLine, silent) {
ch = state.src.charCodeAt(pos); ch = state.src.charCodeAt(pos);
if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */) { return false; } if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */) { return false; }
secondLineMatch = lineMatch(state, startLine + 1, lineText = getLine(state, startLine + 1);
/^ *\|?(( *[:-]-+[:-] *\|)+( *[:-]-+[:-] *))\|? *$/); if (!/^[-:| ]+$/.test(lineText)) { return false; }
if (!secondLineMatch) { return false; }
rows = secondLineMatch[1].split('|'); rows = lineText.split('|');
if (rows <= 2) { return false; }
aligns = []; aligns = [];
for (i = 0; i < rows.length; i++) { for (i = 0; i < rows.length; i++) {
t = rows[i].trim(); 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/* : */) { 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/* : */) { } else if (t.charCodeAt(0) === 0x3A/* : */) {
aligns[i] = 'left'; aligns.push('left');
} else { } else {
aligns[i] = ''; aligns.push('');
} }
} }
firstLineMatch = lineMatch(state, startLine, /^ *\|?(.*?\|.*?)\|? *$/); lineText = getLine(state, startLine).trim();
if (!firstLineMatch) { return false; } if (lineText.indexOf('|') === -1) { return false; }
rows = lineText.replace(/^\||\|$/g, '').split('|');
rows = firstLineMatch[1].split('|');
if (aligns.length !== rows.length) { return false; } if (aligns.length !== rows.length) { return false; }
if (silent) { return true; } if (silent) { return true; }
@ -98,9 +108,9 @@ module.exports = function table(state, startLine, endLine, silent) {
for (nextLine = startLine + 2; nextLine < endLine; nextLine++) { for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
if (state.tShift[nextLine] < state.blkIndent) { break; } if (state.tShift[nextLine] < state.blkIndent) { break; }
m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/); lineText = getLine(state, nextLine).trim();
if (!m) { break; } if (lineText.indexOf('|') === -1) { break; }
rows = m[1].split('|'); rows = lineText.replace(/^\||\|$/g, '').split('|');
state.tokens.push({ type: 'tr_open', level: state.level++ }); state.tokens.push({ type: 'tr_open', level: state.level++ });
for (i = 0; i < rows.length; i++) { for (i = 0; i < rows.length; i++) {

17
test/fixtures/remarkable/tables.txt

@ -110,3 +110,20 @@ Nested tables inside lists:
</ul> </ul>
<p>baz|baz</p> <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