Skip to content

MarkdownElementBuilder replaces only first child when builder returns a widget for inline elements #132

Description

@Jess-Gabia

Describe the bug

When a custom MarkdownElementBuilder is registered for an inline tag (e.g. a) and visitElementAfterWithContext returns a non-null widget, the builder only replaces current.children[0] instead of clearing all children. If the element has multiple children (e.g. link text containing delimiter characters like _ or *), the remaining children are still appended to the parent, causing duplicate text rendering.

To Reproduce

  1. Register a custom builder for the a tag
  2. Send a message containing a link whose text includes a delimiter character, e.g. [_hello world](https://example.com)
  3. The markdown parser splits the link text into multiple children: [Text("_"), Text("hello world")]
  4. The builder widget replaces only children[0], so Text("hello world") is also appended to the parent
  5. Result: "hello world" appears twice

Expected behavior

When visitElementAfterWithContext returns a non-null widget, all existing children should be replaced by that widget — not just children[0].

Suggested fix

In builder.dart, change:

if (current.children.isEmpty) {
  current.children.add(child);
} else {
  current.children[0] = child;
}

to:

current.children
  ..clear()
  ..add(child);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions