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

Fix tables.js for pipes inside backticks #697

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 8 additions & 12 deletions lib/rules_block/table.js
Expand Up @@ -19,8 +19,7 @@ function escapedSplit(str) {
ch,
escapes = 0,
lastPos = 0,
backTicked = false,
lastBackTick = 0;
backTicked = false;

ch = str.charCodeAt(pos);

Expand All @@ -30,12 +29,14 @@ function escapedSplit(str) {
// make \` close code sequence, but not open it;
// the reason is: `\` is correct code block
backTicked = false;
lastBackTick = pos;
} else if (escapes % 2 === 0) {
backTicked = true;
lastBackTick = pos;
}
} else if (ch === 0x7c/* | */ && (escapes % 2 === 0) && !backTicked) {
} else if (ch === 0x7c/* | */ && (escapes % 2 === 0)
&& (!backTicked || str.charCodeAt(pos - 1) !== 0x5c/* \ */)) {
// If there was an un-closed backtick, close it
backTicked = false;

result.push(str.substring(lastPos, pos));
lastPos = pos + 1;
}
Expand All @@ -48,18 +49,13 @@ function escapedSplit(str) {

pos++;

// If there was an un-closed backtick, go back to just after
// the last backtick, but as if it was a normal character
if (pos === max && backTicked) {
backTicked = false;
pos = lastBackTick + 1;
}

ch = str.charCodeAt(pos);
}

result.push(str.substring(lastPos));

result = result.map(function (x){ return x.replace('\\|', '|'); });

return result;
}

Expand Down
60 changes: 57 additions & 3 deletions test/fixtures/markdown-it/tables.txt
Expand Up @@ -319,12 +319,12 @@ Delimiter escaping:
</table>
.

Pipes inside backticks don't split cells:
Pipes inside backticks split cells:
.
| Heading 1 | Heading 2
| --------- | ---------
| Cell 1 | Cell 2
| `Cell|3` | Cell 4
| `Cell 3|Cell 4` | Removed
.
<table>
<thead>
Expand All @@ -339,7 +339,61 @@ Pipes inside backticks don't split cells:
<td>Cell 2</td>
</tr>
<tr>
<td><code>Cell|3</code></td>
<td>`Cell 3</td>
<td>Cell 4`</td>
</tr>
</tbody>
</table>
.

Escaped pipes inside backticks don't split cells:
.
| 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><code>Cell 3|</code></td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
.

Escape before escaped Pipes inside backticks don't split cells:
.
| 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><code>Cell 3\|</code></td>
<td>Cell 4</td>
</tr>
</tbody>
Expand Down