Skip to content

Commit 423b7e9

Browse files
committed
add option to allow inline images in selected tags
fixes #61
1 parent ed3eee7 commit 423b7e9

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ escape_underscores
106106
If set to ``False``, do not escape ``_`` to ``\_`` in text.
107107
Defaults to ``True``.
108108

109+
keep_inline_images_in
110+
Images are converted to their alt-text when the images are located inside
111+
headlines or table cells. If some inline images should be converted to
112+
markdown images instead, this option can be set to a list of parent tags
113+
that should be allowed to contain inline images, for example ``['td']``.
114+
Defaults to an empty list.
115+
109116
Options may be specified as kwargs to the ``markdownify`` function, or as a
110117
nested ``Options`` class in ``MarkdownConverter`` subclasses.
111118

markdownify/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class DefaultOptions:
7575
default_title = False
7676
escape_underscores = True
7777
heading_style = UNDERLINED
78+
keep_inline_images_in = []
7879
newline_style = SPACES
7980
strip = None
8081
strong_em_symbol = ASTERISK
@@ -278,7 +279,8 @@ def convert_img(self, el, text, convert_as_inline):
278279
src = el.attrs.get('src', None) or ''
279280
title = el.attrs.get('title', None) or ''
280281
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
281-
if convert_as_inline:
282+
if (convert_as_inline
283+
and el.parent.name not in self.options['keep_inline_images_in']):
282284
return alt
283285

284286
return '![%s](%s%s)' % (alt, src, title_part)

tests/test_conversions.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,13 @@ def test_hn_nested_simple_tag():
133133

134134
def test_hn_nested_img():
135135
image_attributes_to_markdown = [
136-
("", ""),
137-
("alt='Alt Text'", "Alt Text"),
138-
("alt='Alt Text' title='Optional title'", "Alt Text"),
136+
("", "", ""),
137+
("alt='Alt Text'", "Alt Text", ""),
138+
("alt='Alt Text' title='Optional title'", "Alt Text", " \"Optional title\""),
139139
]
140-
for image_attributes, markdown in image_attributes_to_markdown:
141-
assert md('<h3>A <img src="/path/to/img.jpg " ' + image_attributes + '/> B</h3>') == '### A ' + markdown + ' B\n\n'
140+
for image_attributes, markdown, title in image_attributes_to_markdown:
141+
assert md('<h3>A <img src="/path/to/img.jpg" ' + image_attributes + '/> B</h3>') == '### A ' + markdown + ' B\n\n'
142+
assert md('<h3>A <img src="/path/to/img.jpg" ' + image_attributes + '/> B</h3>', keep_inline_images_in=['h3']) == '### A ![' + markdown + '](/path/to/img.jpg' + title + ') B\n\n'
142143

143144

144145
def test_hn_atx_headings():

0 commit comments

Comments
 (0)