Skip to content

Commit eed13f8

Browse files
Merge pull request #616 from raulbocanegra/patch-1
fix: make tables without body gfm compatible
2 parents b75eb21 + 1dfef83 commit eed13f8

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/markdown2.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,7 @@ def run(self, text):
33793379
(?:
33803380
^[ ]{0,%d}(?!\ ) # ensure line begins with 0 to less_than_tab spaces
33813381
.*\|.*[ ]*\n
3382-
)+
3382+
)*
33833383
)
33843384
''' % (less_than_tab, less_than_tab, less_than_tab), re.M | re.X)
33853385
return table_re.sub(self.sub, text)
@@ -3415,17 +3415,19 @@ def sub(self, match: re.Match) -> str:
34153415
hlines.append('</thead>')
34163416

34173417
# tbody
3418-
hlines.append('<tbody>')
3419-
for line in body.strip('\n').split('\n'):
3420-
hlines.append('<tr>')
3421-
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)))]
3422-
for col_idx, col in enumerate(cols):
3423-
hlines.append(' <td{}>{}</td>'.format(
3424-
align_from_col_idx.get(col_idx, ''),
3425-
self.md._run_span_gamut(col)
3426-
))
3427-
hlines.append('</tr>')
3428-
hlines.append('</tbody>')
3418+
body = body.strip('\n')
3419+
if body:
3420+
hlines.append('<tbody>')
3421+
for line in body.split('\n'):
3422+
hlines.append('<tr>')
3423+
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)))]
3424+
for col_idx, col in enumerate(cols):
3425+
hlines.append(' <td{}>{}</td>'.format(
3426+
align_from_col_idx.get(col_idx, ''),
3427+
self.md._run_span_gamut(col)
3428+
))
3429+
hlines.append('</tr>')
3430+
hlines.append('</tbody>')
34293431
hlines.append('</table>')
34303432

34313433
return '\n'.join(hlines) + '\n'

test/tm-cases/tables.html

+11
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,14 @@ <h1>escaping of pipes</h1>
394394
</tr>
395395
</tbody>
396396
</table>
397+
398+
<h1>table without rows</h1>
399+
400+
<table>
401+
<thead>
402+
<tr>
403+
<th>abc</th>
404+
<th>def</th>
405+
</tr>
406+
</thead>
407+
</table>

test/tm-cases/tables.text

+5
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,8 @@ the rule is it must have at least a single dash in there.
156156
| A | \| | C \| C |
157157
|--- |--- | ------ |
158158
|\|\|| BB | C |
159+
160+
# table without rows
161+
162+
| abc | def |
163+
| --- | --- |

0 commit comments

Comments
 (0)