Skip to content

fix(html): reduce inline images to alt text under markdownify 1.x (convert_as_inline → parent_tags)#2225

Open
lntutor wants to merge 2 commits into
microsoft:mainfrom
lntutor:fix/inline-image-alt-markdownify-1x
Open

fix(html): reduce inline images to alt text under markdownify 1.x (convert_as_inline → parent_tags)#2225
lntutor wants to merge 2 commits into
microsoft:mainfrom
lntutor:fix/inline-image-alt-markdownify-1x

Conversation

@lntutor

@lntutor lntutor commented Jul 19, 2026

Copy link
Copy Markdown

What's broken

markdownify ≥1.0 changed the convert_* calling convention: it now invokes them as convert_fn(node, text, parent_tags=<set>) (where parent_tags contains the pseudo-tag "_inline" for headings/table cells) instead of the old positional convert_as_inline boolean. _CustomMarkdownify.convert_img still declares convert_as_inline and swallows parent_tags into **kwargs, so convert_as_inline is always False and the alt-text reduction never fires:

HtmlConverter().convert_string('<h1>Hello <img src="a.png" alt="World"></h1>').markdown
# '# Hello ![World](a.png)'   -- expected '# Hello World'

An image in a table cell yields | ![AA](a.png) | bob |, which with a long/data: URI can break the table row entirely. Base markdownify.convert_img handles this correctly via "_inline" in parent_tags; the override defeated it.

Fix

Switch the override to accept parent_tags and check "_inline" in parent_tags, matching base markdownify. Block (paragraph) images and the data: URI truncation path are unaffected.

Tests

Adds three cases to test_module_misc.py: inline image in a heading and in a table cell reduce to alt text; a block image is still rendered. All pass. (convert_hn/convert_a/convert_input carry the same obsolete signature but produce no visible wrong result, so this stays a minimal single-bug fix.)

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.
'<h1>Hello <img src=a.png alt=World></h1>' 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N6RtoHuxrDqTUo9Mw9h4Cv
@lntutor

lntutor commented Jul 26, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree [company="Vexere"]

@lntutor

lntutor commented Jul 26, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree
@microsoft-github-policy-service agree company="Vexere"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant