Skip to content

Commit

Permalink
make sure there are blank lines around table/figure captions (#114)
Browse files Browse the repository at this point in the history
Signed-off-by: chrispy <[email protected]>
Co-authored-by: AlexVonB <[email protected]>
  • Loading branch information
chrispy-snps and AlexVonB authored Mar 26, 2024
1 parent e4df412 commit d5fb0fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ def convert_style(self, el, text, convert_as_inline):
def convert_table(self, el, text, convert_as_inline):
return '\n\n' + text + '\n'

def convert_caption(self, el, text, convert_as_inline):
return text + '\n'

def convert_figcaption(self, el, text, convert_as_inline):
return '\n\n' + text + '\n\n'

def convert_td(self, el, text, convert_as_inline):
return ' ' + text.strip().replace("\n", " ") + ' |'

Expand Down
5 changes: 5 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def test_br():
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'


def test_caption():
assert md('TEXT<figure><figcaption>Caption</figcaption><span>SPAN</span></figure>') == 'TEXT\n\nCaption\n\nSPAN'
assert md('<figure><span>SPAN</span><figcaption>Caption</figcaption></figure>TEXT') == 'SPAN\n\nCaption\n\nTEXT'


def test_code():
inline_tests('code', '`')
assert md('<code>*this_should_not_escape*</code>') == '`*this_should_not_escape*`'
Expand Down
9 changes: 9 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@
</tbody>
</table>"""

table_with_caption = """TEXT<table><caption>Caption</caption>
<tbody><tr><td>Firstname</td>
<td>Lastname</td>
<td>Age</td>
</tr>
</tbody>
</table>"""


def test_table():
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
Expand All @@ -213,3 +221,4 @@ def test_table():
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_missing_head) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n\n'

0 comments on commit d5fb0fb

Please sign in to comment.