Skip to content

Commit

Permalink
Merge pull request #616 from raulbocanegra/patch-1
Browse files Browse the repository at this point in the history
fix: make tables without body gfm compatible
  • Loading branch information
nicholasserra authored Dec 27, 2024
2 parents b75eb21 + 1dfef83 commit eed13f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ def run(self, text):
(?:
^[ ]{0,%d}(?!\ ) # ensure line begins with 0 to less_than_tab spaces
.*\|.*[ ]*\n
)+
)*
)
''' % (less_than_tab, less_than_tab, less_than_tab), re.M | re.X)
return table_re.sub(self.sub, text)
Expand Down Expand Up @@ -3415,17 +3415,19 @@ def sub(self, match: re.Match) -> str:
hlines.append('</thead>')

# tbody
hlines.append('<tbody>')
for line in body.strip('\n').split('\n'):
hlines.append('<tr>')
cols = [re.sub(escape_bar_re, '|', cell.strip()) for cell in re.split(split_bar_re, re.sub(trim_bar_re, "", re.sub(trim_space_re, "", line)))]
for col_idx, col in enumerate(cols):
hlines.append(' <td{}>{}</td>'.format(
align_from_col_idx.get(col_idx, ''),
self.md._run_span_gamut(col)
))
hlines.append('</tr>')
hlines.append('</tbody>')
body = body.strip('\n')
if body:
hlines.append('<tbody>')
for line in body.split('\n'):
hlines.append('<tr>')
cols = [re.sub(escape_bar_re, '|', cell.strip()) for cell in re.split(split_bar_re, re.sub(trim_bar_re, "", re.sub(trim_space_re, "", line)))]
for col_idx, col in enumerate(cols):
hlines.append(' <td{}>{}</td>'.format(
align_from_col_idx.get(col_idx, ''),
self.md._run_span_gamut(col)
))
hlines.append('</tr>')
hlines.append('</tbody>')
hlines.append('</table>')

return '\n'.join(hlines) + '\n'
Expand Down
11 changes: 11 additions & 0 deletions test/tm-cases/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,14 @@ <h1>escaping of pipes</h1>
</tr>
</tbody>
</table>

<h1>table without rows</h1>

<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
</table>
5 changes: 5 additions & 0 deletions test/tm-cases/tables.text
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,8 @@ the rule is it must have at least a single dash in there.
| A | \| | C \| C |
|--- |--- | ------ |
|\|\|| BB | C |

# table without rows

| abc | def |
| --- | --- |

0 comments on commit eed13f8

Please sign in to comment.