Skip to content

Fix #132: inline builder duplicates children#136

Merged
gazreese merged 1 commit into
mainfrom
fix/132-inline-builder-duplicate-children
Jul 6, 2026
Merged

Fix #132: inline builder duplicates children#136
gazreese merged 1 commit into
mainfrom
fix/132-inline-builder-duplicate-children

Conversation

@gazreese

@gazreese gazreese commented Jul 6, 2026

Copy link
Copy Markdown
Member

Fixes #132.

The bug

When a custom MarkdownElementBuilder is registered for an inline tag (e.g. a) and visitElementAfterWithContext returns a non-null widget, the builder replaced only current.children[0] instead of all children. If the element had multiple inline children — which happens when link text contains a delimiter like _ or * that the markdown parser splits into separate text nodes ([Text("_"), Text("hello world")]) — the leftover children were still appended to the parent, so the trailing text rendered twice.

Reported by @Jess-Gabia, with @amungi running a patched fork in production.

The fix

In lib/src/builder.dart, the inline builder path now discards all existing children and replaces them with the returned widget:

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

The returned widget represents the entire element. This matches the block-element path, which already replaces all children — so inline and block behaviour are now consistent. Behaviour is unchanged for empty/single-child elements (which is why existing tests were unaffected and the bug went unnoticed).

Tests

Added two regression tests in test/custom_syntax_test.dart using a custom a builder that returns a marker widget:

  • [_hello world](…)_ delimiter
  • [*hello world](…)* delimiter

Both assert the marker renders once and the link text does not leak through a second time. Verified red before / green after the fix — before the fix, a stray RichText("hello world") is found; after, it's gone.

Full suite: 373 passing (was 371), flutter analyze clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_014oVQtXCPb3bSLnL2eDwGrh

When a custom MarkdownElementBuilder for an inline tag (e.g. `a`) returned a
widget from visitElementAfterWithContext, only current.children[0] was replaced.
If the element had multiple inline children — such as link text split on a `_`
or `*` delimiter into separate text nodes — the remaining children were still
appended to the parent, rendering the trailing text twice.

Replace all children with the returned widget (..clear()..add(child)), matching
the block-element path which already discards children. Behaviour is unchanged
for empty/single-child elements.

Adds regression tests covering `_` and `*` delimiter link text with a custom
`a` builder.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014oVQtXCPb3bSLnL2eDwGrh
@gazreese
gazreese merged commit 33183c9 into main Jul 6, 2026
2 checks passed
@gazreese
gazreese deleted the fix/132-inline-builder-duplicate-children branch July 6, 2026 09:58
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.

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

1 participant