Browse Source

Added ability to escape table row separator, fix #5

pull/52/head
Kirill Efimov 9 years ago
parent
commit
0076a1ebab
  1. 34
      lib/rules_block/table.js
  2. 18
      test/fixtures/markdown-it/tables.txt

34
lib/rules_block/table.js

@ -10,6 +10,34 @@ function getLine(state, line) {
return state.src.substr(pos, max - pos);
}
function escapedSplit(str) {
var result = [],
pos = 0,
max = str.length,
ch,
escapes = 0,
lastPos = 0;
ch = str.charCodeAt(pos);
while (pos < max) {
if (ch === 0x7c/* | */ && (escapes % 2 === 0)) {
result.push(str.substring(lastPos, pos));
lastPos = pos + 1;
} else if (ch === 0x5c/* \ */) {
escapes++;
} else {
escapes = 0;
}
ch = str.charCodeAt(++pos);
}
result.push(str.substring(lastPos));
return result;
}
module.exports = function table(state, startLine, endLine, silent) {
var ch, lineText, pos, i, nextLine, rows,
@ -60,7 +88,7 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, startLine).trim();
if (lineText.indexOf('|') === -1) { return false; }
rows = lineText.replace(/^\||\|$/g, '').split('|');
rows = escapedSplit(lineText.replace(/^\||\|$/g, ''));
if (aligns.length !== rows.length) { return false; }
if (silent) { return true; }
@ -110,14 +138,14 @@ module.exports = function table(state, startLine, endLine, silent) {
lineText = getLine(state, nextLine).trim();
if (lineText.indexOf('|') === -1) { break; }
rows = lineText.replace(/^\||\|$/g, '').split('|');
rows = escapedSplit(lineText.replace(/^\||\|$/g, ''));
state.tokens.push({ type: 'tr_open', level: state.level++ });
for (i = 0; i < rows.length; i++) {
state.tokens.push({ type: 'td_open', align: aligns[i], level: state.level++ });
state.tokens.push({
type: 'inline',
content: rows[i].replace(/^\|? *| *\|?$/g, ''),
content: rows[i].trim(),
level: state.level,
children: []
});

18
test/fixtures/markdown-it/tables.txt

@ -223,3 +223,21 @@ paragraph
</table>
<p>paragraph</p>
.
Delimiter escaping:
.
| Heading 1 \\\\| Heading 2
| --------- | ---------
| Cell\|1\|| Cell\|2
\| Cell\\\|3 \\| Cell\|4
.
<table>
<thead>
<tr><th>Heading 1 \\</th><th>Heading 2</th></tr>
</thead>
<tbody>
<tr><td>Cell|1|</td><td>Cell|2</td></tr>
<tr><td>| Cell\|3 \</td><td>Cell|4</td></tr>
</tbody>
</table>
.

Loading…
Cancel
Save