Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/markitdown/src/markitdown/converters/_markdownify.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,24 @@ def convert_img(
self,
el: Any,
text: str,
convert_as_inline: Optional[bool] = False,
parent_tags: Optional[Any] = None,
**kwargs,
) -> str:
"""Same as usual converter, but removes data URIs"""

# markdownify >= 1.0 passes `parent_tags` (containing the pseudo-tag
# "_inline" for headings/table cells) instead of the old
# `convert_as_inline` boolean; check that so inline images still reduce
# to their alt text.
parent_tags = parent_tags or set()
alt = el.attrs.get("alt", None) or ""
src = el.attrs.get("src", None) or el.attrs.get("data-src", None) or ""
title = el.attrs.get("title", None) or ""
title_part = ' "%s"' % title.replace('"', r"\"") if title else ""
# Remove all line breaks from alt
alt = alt.replace("\n", " ")
if (
convert_as_inline
"_inline" in parent_tags
and el.parent.name not in self.options["keep_inline_images_in"]
):
return alt
Expand Down
26 changes: 26 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,29 @@ def test_markitdown_llm() -> None:
test()
print("OK")
print("All tests passed!")


def test_inline_image_in_heading_reduced_to_alt() -> None:
from markitdown.converters._html_converter import HtmlConverter

md = HtmlConverter().convert_string('<h1>Hello <img src="a.png" alt="World"></h1>').markdown
assert md.strip() == "# Hello World", repr(md)


def test_inline_image_in_table_cell_reduced_to_alt() -> None:
from markitdown.converters._html_converter import HtmlConverter

html = (
"<table><tr><th>Pic</th><th>Name</th></tr>"
'<tr><td><img src="a.png" alt="AA"></td><td>bob</td></tr></table>'
)
md = HtmlConverter().convert_string(html).markdown
assert "| AA | bob |" in md, repr(md)
assert "![AA]" not in md, repr(md)


def test_block_image_still_rendered() -> None:
from markitdown.converters._html_converter import HtmlConverter

md = HtmlConverter().convert_string('<p><img src="a.png" alt="x"></p>').markdown
assert "![x](a.png)" in md, repr(md)