From 0743d4adb48f7ccf410912d4c211545772882479 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 15 Sep 2023 13:21:58 -0600 Subject: [PATCH] fix: single column table (#2985) --- src/Tokenizer.ts | 7 ++++- src/rules.ts | 4 +-- test/specs/new/table_vs_setext.html | 46 +++++++++++++++++++++++++++++ test/specs/new/table_vs_setext.md | 22 ++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 test/specs/new/table_vs_setext.html create mode 100644 test/specs/new/table_vs_setext.md diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 38256ba0f2..443cef4898 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -399,13 +399,18 @@ export class _Tokenizer { table(src: string): Tokens.Table | undefined { const cap = this.rules.block.table.exec(src); if (cap) { + if (!/[:|]/.test(cap[2])) { + // delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading + return; + } + const item: Tokens.Table = { type: 'table', raw: cap[0], header: splitCells(cap[1]).map(c => { return { text: c, tokens: [] }; }), - align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */), + align: cap[2].replace(/^\||\| *$/g, '').split('|'), rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : [] }; diff --git a/src/rules.ts b/src/rules.ts index 1fd7832f6f..1c6fda259f 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -157,8 +157,8 @@ block.normal = { ...block }; block.gfm = { ...block.normal, - table: '^ *([^\\n ].*\\|.*)\\n' // Header - + ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align + table: '^ *([^\\n ].*)\\n' // Header + + ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells }; diff --git a/test/specs/new/table_vs_setext.html b/test/specs/new/table_vs_setext.html new file mode 100644 index 0000000000..6e251ce548 --- /dev/null +++ b/test/specs/new/table_vs_setext.html @@ -0,0 +1,46 @@ +

| setext |

+

| setext |

+ +

setext

+

setext

+ + + + + + + + + + + + +
table
table
+ + + + + + + + + + + + +
table
table
+ + + + + + + + + + + + +
table
table
+ + diff --git a/test/specs/new/table_vs_setext.md b/test/specs/new/table_vs_setext.md new file mode 100644 index 0000000000..00969cb292 --- /dev/null +++ b/test/specs/new/table_vs_setext.md @@ -0,0 +1,22 @@ +--- +gfm: true +--- +| setext | +---------- +| setext | + +setext +------ +setext + +| table | +:-------- +| table | + +table +:---- +table + +| table | +|-------- +| table |