From 767103e410d29c63f7652dc9246d45c7996af8ce Mon Sep 17 00:00:00 2001 From: Loi Nguyen <1948922+lntutor@users.noreply.github.com> Date: Mon, 20 Jul 2026 03:20:40 +0700 Subject: [PATCH] fix(html): reduce inline images to alt text under markdownify 1.x markdownify >= 1.0 changed the convert_* calling convention: it passes parent_tags (containing the pseudo-tag '_inline' for headings/table cells) instead of the old positional convert_as_inline boolean. _CustomMarkdownify.convert_img still declared convert_as_inline and swallowed parent_tags into **kwargs, so convert_as_inline was always False and the alt-text reduction never fired. An image inside a heading or table cell therefore leaked full image markdown -- e.g. '

Hello World

' became '# Hello ![World](a.png)' instead of '# Hello World', and a data: URI in a table cell could break the row. Switch the override to check '_inline' in parent_tags, matching base markdownify. Block (paragraph) images are unaffected. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01N6RtoHuxrDqTUo9Mw9h4Cv --- .../src/markitdown/converters/_markdownify.py | 9 +++++-- packages/markitdown/tests/test_module_misc.py | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/markitdown/src/markitdown/converters/_markdownify.py b/packages/markitdown/src/markitdown/converters/_markdownify.py index 19e8a2984..ca1358fab 100644 --- a/packages/markitdown/src/markitdown/converters/_markdownify.py +++ b/packages/markitdown/src/markitdown/converters/_markdownify.py @@ -86,11 +86,16 @@ 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 "" @@ -98,7 +103,7 @@ def convert_img( # 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 diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py index 4d62e4919..854fc8450 100644 --- a/packages/markitdown/tests/test_module_misc.py +++ b/packages/markitdown/tests/test_module_misc.py @@ -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('

Hello World

').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 = ( + "" + '
PicName
AAbob
' + ) + 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('

x

').markdown + assert "![x](a.png)" in md, repr(md)