From cf8de25218a7327e5f372f8309bdc6a6c615d08d Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 26 Mar 2021 17:28:09 +0200 Subject: [PATCH 1/2] Fix table/list parsing ambiguity --- lib/rules_block/table.js | 20 +++++++++++++++++--- test/fixtures/markdown-it/tables.txt | 13 +++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/rules_block/table.js b/lib/rules_block/table.js index 89ed65c93..d232ba1e0 100644 --- a/lib/rules_block/table.js +++ b/lib/rules_block/table.js @@ -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; } @@ -71,8 +71,22 @@ 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 (pos >= state.eMarks[nextLine]) { return false; } + secondCh = state.src.charCodeAt(pos++); + + // if first character is '-', then second character must not be a space + // (due to parsing ambiguity with list) + if (firstCh === 0x2D/* - */) { + if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */) { return false; } + } else if (firstCh === 0x7C/* | */ || firstCh === 0x3A/* : */) { + if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */ && !isSpace(secondCh)) { + return false; + } + } else { + return false; + } while (pos < state.eMarks[nextLine]) { ch = state.src.charCodeAt(pos); diff --git a/test/fixtures/markdown-it/tables.txt b/test/fixtures/markdown-it/tables.txt index cd71b8d96..4737df7d6 100644 --- a/test/fixtures/markdown-it/tables.txt +++ b/test/fixtures/markdown-it/tables.txt @@ -793,3 +793,16 @@ GFM 4.10 Tables (extension), Example 205 . + +A list takes precedence in case of ambiguity +. +a | b +- | - +1 | 2 +. +

a | b

+ +. From 751eb4e446ac85e3eb2f70afe9dd0cea6a895b3c Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Sat, 27 Mar 2021 03:23:08 +0200 Subject: [PATCH 2/2] Increase readability --- lib/rules_block/table.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/rules_block/table.js b/lib/rules_block/table.js index d232ba1e0..7d9208fe8 100644 --- a/lib/rules_block/table.js +++ b/lib/rules_block/table.js @@ -72,21 +72,18 @@ module.exports = function table(state, startLine, endLine, silent) { if (pos >= state.eMarks[nextLine]) { 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/* - */) { - if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */) { return false; } - } else if (firstCh === 0x7C/* | */ || firstCh === 0x3A/* : */) { - if (secondCh !== 0x7C/* | */ && secondCh !== 0x2D/* - */ && secondCh !== 0x3A/* : */ && !isSpace(secondCh)) { - return false; - } - } else { - return false; - } + if (firstCh === 0x2D/* - */ && isSpace(secondCh)) { return false; } while (pos < state.eMarks[nextLine]) { ch = state.src.charCodeAt(pos);