Skip to content

Commit d5fb0fb

Browse files
make sure there are blank lines around table/figure captions (#114)
Signed-off-by: chrispy <chrispy@synopsys.com> Co-authored-by: AlexVonB <AlexVonB@users.noreply.github.com>
1 parent e4df412 commit d5fb0fb

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

markdownify/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ def convert_style(self, el, text, convert_as_inline):
369369
def convert_table(self, el, text, convert_as_inline):
370370
return '\n\n' + text + '\n'
371371

372+
def convert_caption(self, el, text, convert_as_inline):
373+
return text + '\n'
374+
375+
def convert_figcaption(self, el, text, convert_as_inline):
376+
return '\n\n' + text + '\n\n'
377+
372378
def convert_td(self, el, text, convert_as_inline):
373379
return ' ' + text.strip().replace("\n", " ") + ' |'
374380

tests/test_conversions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ def test_br():
7474
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
7575

7676

77+
def test_caption():
78+
assert md('TEXT<figure><figcaption>Caption</figcaption><span>SPAN</span></figure>') == 'TEXT\n\nCaption\n\nSPAN'
79+
assert md('<figure><span>SPAN</span><figcaption>Caption</figcaption></figure>TEXT') == 'SPAN\n\nCaption\n\nTEXT'
80+
81+
7782
def test_code():
7883
inline_tests('code', '`')
7984
assert md('<code>*this_should_not_escape*</code>') == '`*this_should_not_escape*`'

tests/test_tables.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@
201201
</tbody>
202202
</table>"""
203203

204+
table_with_caption = """TEXT<table><caption>Caption</caption>
205+
<tbody><tr><td>Firstname</td>
206+
<td>Lastname</td>
207+
<td>Age</td>
208+
</tr>
209+
</tbody>
210+
</table>"""
211+
204212

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

0 commit comments

Comments
 (0)