Skip to content

馃悰 Keep backslash escapes and entities in image alt text - #446

Open
gyanu2507 wants to merge 1 commit into
executablebooks:masterfrom
gyanu2507:fix/image-alt-text-special
Open

gyanu2507 wants to merge 1 commit into
executablebooks:masterfrom
gyanu2507:fix/image-alt-text-special

Conversation

@gyanu2507

Copy link
Copy Markdown

Fixes #445

Image alt text dropped backslash escapes and entities. ![a \* b](/u) rendered alt="a b" and ![C:\Python26](/u) rendered alt="C:ython26". Link text with the same content was fine.

The cause is in text_join. It turns text_special tokens into text and merges adjacent text, but only for the direct children of each inline token. An image holds its alt text as its own child token list, so the text_special tokens in there were left alone, and renderInlineAsText (which only emits text, nested images, line breaks and raw HTML) skipped them.

markdown-it fixed the same thing in 15.0.0 (markdown-it/markdown-it@974e2d7) by also joining the children of each child token. This ports that. The existing join loop moves into a _join_text_tokens helper that runs over the inline children and over each child's own children. Same one-level scope as upstream.

Tests

Added a case to tests/test_port/fixtures/issue-fixes.md with the four inputs from the upstream test plus ![C:\Python26](/u).

  • pytest tests/test_port/test_fixtures.py -k issue_fixes: the new case fails without the change and passes with it.
  • pytest tests/ (with the testing and linkify extras): 1033 passed.
  • mdit-py-plugins test suite against this branch, as in the plugins tox env: 511 passed, same as on master.
  • pre-commit run on the changed files (ruff, ruff format, mypy): passed.

Added a changelog entry under Unreleased.

text_join only converted the text_special tokens that are direct
children of an inline token. An image keeps its alt text as its own
child token list, so escapes and entities there were never turned into
text and the alt renderer dropped them: ![a \* b](/u) gave alt="a  b".

Join the children of each child token too, as markdown-it 15.0.0 does.

Fixes executablebooks#445

This branch has not been deployed

No deployments
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 and entities

1 participant