Skip to content

FIX: Keep escapes and entities in image alt text - #1211

Merged
chrisjsewell merged 5 commits into
executablebooks:masterfrom
Cherith1222:fix/1210-image-alt-escapes
Nov 9, 2026
Merged

chrisjsewell merged 5 commits into
executablebooks:masterfrom
Cherith1222:fix/1210-image-alt-escapes

Conversation

@Cherith1222

Copy link
Copy Markdown
Contributor

Fixes #1210.

Image alt text dropped text_special tokens, so a backslash escape or an HTML entity disappeared from the alt attribute. Those tokens are now kept.

Tests, on Windows with Python 3.13.3, via Resource Sentinel (1 CPU, 1.5 GiB, 1 I/O):

  • With myst_parser/mdit_to_docutils/base.py checked out at master, test_image_alt_keeps_escapes_and_entities failed. The expected alt was a * b and the rendered alt was a b.
  • On this commit the same test passed (1 passed).
  • ruff 0.15.20 check passed on myst_parser/mdit_to_docutils/base.py and tests/test_image_alt.py. That is the version in the project dependency group. ruff format --check also passed on those files.

Not run: the full suite, and anything other than Windows.

This patch was drafted with AI assistance (Cursor).

Image alt text dropped text_special tokens, so a backslash escape or an HTML entity disappeared from the alt attribute.

AI-assisted (Cursor). Not yet reviewed by a human.
Image rendering is pinned in `docutil_syntax_elements.md`, so the alt
cases live there as doctree fixtures rather than as html5 writer strings.
Adds an escape and entity inside emphasis, an escape in a nested image,
and a reference-style image. All four fail without the fix.
@chrisjsewell
chrisjsewell merged commit af515c1 into executablebooks:master Nov 9, 2026
24 checks passed
@chrisjsewell

Copy link
Copy Markdown
Member

Merged in af515c1, thanks @Cherith1222! The fix was right as written: it handles escapes and entities at any nesting depth, including an image nested in an image, which markdown-it JS 15's text_join does not. Before merging I merged master in and moved the cases from tests/test_image_alt.py into tests/test_renderers/fixtures/docutil_syntax_elements.md, where image rendering is already pinned, adding an escape and an entity inside emphasis, an escape in a nested image, and a reference-style image. It composes cleanly with #1214, which fixed the HTML <img alt> side.

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.

Image alt text loses backslash escapes

2 participants