Skip to content

Alt Text is Dropped Within Code Spans #398

Closed

Description

@rdestefa

Example Markdown:

![`code` _em_ **strong** plain](path/to/image.png)

Expected behavior:

<p><img src="path/to/image.png" alt="code em strong plain" /></p>

Actual behavior:

<p><img src="path/to/image.png" alt=" em strong plain" /></p>

Example from commonmark.js

This is probably because AltTextVisitor doesn't implement visit(Code code), and since Code nodes don't have child Text nodes their text is never added.

Relevant Code

private static class AltTextVisitor extends AbstractVisitor {
private final StringBuilder sb = new StringBuilder();
String getAltText() {
return sb.toString();
}
@Override
public void visit(Text text) {
sb.append(text.getLiteral());
}
@Override
public void visit(SoftLineBreak softLineBreak) {
sb.append('\n');
}
@Override
public void visit(HardLineBreak hardLineBreak) {
sb.append('\n');
}
}

Activity

  1. robinst commented on Sep 10, 2025

    @robinst
    Collaborator

    This is probably because AltTextVisitor doesn't implement visit(Code code), and since Code nodes don't have child Text nodes their text is never added.

    Yep, that's it! Do you want to submit a PR for this?

  2. rdestefa commented on Sep 10, 2025

    @rdestefa
    ContributorAuthor

    Sure! #401

  3. robinst commented on Sep 13, 2025

    @robinst
    Collaborator
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions