Skip to content

Image alt text loses backslash escapes and entities #445

Closed

Description

@kdeldycke

Describe the bug

context
When I render an image whose alt text holds a backslash escape or an entity.

expectation
I expected the escaped character or the entity to stay in the alt attribute, as it does in link text.

bug
But instead it is dropped: ![a \* b](/u) renders alt="a b".

problem
markdown-it fixed this in 15.0.0: markdown-it/markdown-it@974e2d7 makes text_join also convert the text_special tokens held by an image. The Python rule only walks the direct children of an inline token:

for inline_token in state.tokens[:]:
if inline_token.type != "inline":
continue
# convert text_special to text and join all adjacent text nodes
new_tokens: list[Token] = []
children = inline_token.children or []
i = 0
while i < len(children):
child_token = children[i]
if child_token.type == "text_special":
child_token.type = "text"

An image keeps them as they are:

>>> from markdown_it import MarkdownIt
>>> image = MarkdownIt("commonmark").parse(r"![a \* b &amp; c](/u)")[1].children[0]
>>> [(t.type, t.content) for t in image.children]
[('text', 'a '), ('text_special', '*'), ('text', ' b '), ('text_special', '&'), ('text', ' c')]

Reproduce the bug

from markdown_it import MarkdownIt

md = MarkdownIt("commonmark")
for src in (
    r"![a \* b](/u)",
    "![a &amp; b](/u)",
    "![a &lt; b](/u)",
    r"![a \* *b* &amp; c](/u)",
    r"![C:\Python26](/u)",
    r"[a \* b &amp; c](/u)",
):
    print(md.render(src), end="")

Output, the same on 4.2.0 and on master:

<p><img src="/u" alt="a  b" /></p>
<p><img src="/u" alt="a  b" /></p>
<p><img src="/u" alt="a  b" /></p>
<p><img src="/u" alt="a  b  c" /></p>
<p><img src="/u" alt="C:ython26" /></p>
<p><a href="/u">a * b &amp; c</a></p>

The upstream test expects this for the first four inputs:

assert.strictEqual(md.render('![a \\* b](/u)'),
  '<p><img src="/u" alt="a * b"></p>\n')

assert.strictEqual(md.render('![a &amp; b](/u)'),
  '<p><img src="/u" alt="a &amp; b"></p>\n')

assert.strictEqual(md.render('![a &lt; b](/u)'),
  '<p><img src="/u" alt="a &lt; b"></p>\n')

assert.strictEqual(md.render('![a \\* *b* &amp; c](/u)'),
  '<p><img src="/u" alt="a * b &amp; c"></p>\n')

https://github.com/markdown-it/markdown-it/blob/974e2d7f3bf1356123032a68c831735c5891ec05/test/markdown-it/misc.test.mjs#L327-L341

List your environment

  • markdown-it-py 4.2.0, and master at 6f58654
  • Python 3.14.7
  • macOS 27.0.1 on arm64

Activity

  1. added
    bug
    on Oct 6, 2026
  2. chrisjsewell commented on Nov 10, 2026

    @chrisjsewell
    Member

    For the record: this was fixed by #446 (escapes and entities, ported from markdown-it 15.0.0) and #460 (code span text, as in markdown-it 15). Thanks for the clear report, @kdeldycke!

  3. kdeldycke commented on Nov 10, 2026

    @kdeldycke
    Author

    For the record: this was fixed by #446 (escapes and entities, ported from markdown-it 15.0.0) and #460 (code span text, as in markdown-it 15). Thanks for the clear report, @kdeldycke!

    Ah yes! I am following your progress over the last few days. Thanks for the multiple merges! Will confirm the fixes once the packages are released! :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions