Skip to content

Commit

Permalink
fix: single column table (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Sep 15, 2023
1 parent ccd0229 commit 0743d4a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') : []
};

Expand Down
4 changes: 2 additions & 2 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
46 changes: 46 additions & 0 deletions test/specs/new/table_vs_setext.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<h2>| setext |</h2>
<p>| setext |</p>

<h2>setext</h2>
<p>setext</p>

<table>
<thead>
<tr>
<th align="left">table</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">table</td>
</tr>
</tbody>
</table>

<table>
<thead>
<tr>
<th align="left">table</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">table</td>
</tr>
</tbody>
</table>

<table>
<thead>
<tr>
<th>table</th>
</tr>
</thead>
<tbody>
<tr>
<td>table</td>
</tr>
</tbody>
</table>


22 changes: 22 additions & 0 deletions test/specs/new/table_vs_setext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
gfm: true
---
| setext |
----------
| setext |

setext
------
setext

| table |
:--------
| table |

table
:----
table

| table |
|--------
| table |

1 comment on commit 0743d4a

@vercel
Copy link

@vercel vercel bot commented on 0743d4a Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.