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 table/list parsing ambiguity #767

Merged
merged 2 commits into from Mar 27, 2021
Merged
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
17 changes: 14 additions & 3 deletions lib/rules_block/table.js
Expand Up @@ -52,7 +52,7 @@ function escapedSplit(str) {
module.exports = function table(state, startLine, endLine, silent) {
var ch, lineText, pos, i, l, nextLine, columns, columnCount, token,
aligns, t, tableLines, tbodyLines, oldParentType, terminate,
terminatorRules;
terminatorRules, firstCh, secondCh;

// should have at least two lines
if (startLine + 2 > endLine) { return false; }
Expand All @@ -71,8 +71,19 @@ module.exports = function table(state, startLine, endLine, silent) {
pos = state.bMarks[nextLine] + state.tShift[nextLine];
if (pos >= state.eMarks[nextLine]) { return false; }

ch = state.src.charCodeAt(pos++);
if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */) { return false; }
firstCh = state.src.charCodeAt(pos++);
if (firstCh !== 0x7C/* | */ && firstCh !== 0x2D/* - */ && firstCh !== 0x3A/* : */) { return false; }

if (pos >= state.eMarks[nextLine]) { return false; }

secondCh = state.src.charCodeAt(pos++);
if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */ && !isSpace(secondCh)) {
return false;
}

// if first character is '-', then second character must not be a space
// (due to parsing ambiguity with list)
if (firstCh === 0x2D/* - */ && isSpace(secondCh)) { return false; }

while (pos < state.eMarks[nextLine]) {
ch = state.src.charCodeAt(pos);
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/markdown-it/tables.txt
Expand Up @@ -793,3 +793,16 @@ GFM 4.10 Tables (extension), Example 205
</thead>
</table>
.

A list takes precedence in case of ambiguity
.
a | b
- | -
1 | 2
.
<p>a | b</p>
<ul>
<li>| -
1 | 2</li>
</ul>
.