Skip to content

Merge adjacent links with the same URL when pasting#719

Open
yanfroes wants to merge 3 commits intobasecamp:mainfrom
yanfroes:adjancent-links-should-be-merged
Open

Merge adjacent links with the same URL when pasting#719
yanfroes wants to merge 3 commits intobasecamp:mainfrom
yanfroes:adjancent-links-should-be-merged

Conversation

@yanfroes
Copy link

@yanfroes yanfroes commented Feb 5, 2026

Fixes #594

Problem:
When pasting a URL over a selection containing multiple words or existing links, Lexical creates separate LinkNode elements for each text segment. This results in HTML like:
<a href="https://example.com">link1</a><a href="https://example.com"> </a><a href="https://example.com">link2</a>

Instead of the expected single link:
<a href="https://example.com">link1 link2</a>

This also affects selections spanning multiple list items or other block elements.

Solution:
After $toggleLink() applies the URL, we now scan all parent elements touched by the selection and merge any consecutive LinkNode siblings that share the same URL. The merge moves children from the second link into the first, then removes the duplicate node.

Changes:
Added #mergeAdjacentLinks() method to collect parent elements from selection nodes
Added #mergeAdjacentLinksInElement() method to scan and merge consecutive links with matching URLs
Added $isLinkNode import from @lexical/link

When pasting a URL over a selection that spans multiple text nodes or
existing links, Lexical creates separate LinkNodes for each segment.
This results in multiple adjacent anchor tags with the same URL, which
is semantically incorrect and causes issues when editing.

After applying a link via paste, scan the parent elements of the
selection and merge any consecutive LinkNodes that share the same URL
by moving children from the second link into the first and removing
the duplicate.

Fixes basecamp#594
Copilot AI review requested due to automatic review settings February 5, 2026 00:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #594 where pasting a URL over a selection containing multiple words creates separate LinkNode elements instead of a single merged link. The solution adds link merging functionality that runs after $toggleLink() to consolidate adjacent links with the same URL.

Changes:

  • Added $isLinkNode import from @lexical/link for type checking
  • Implemented #mergeAdjacentLinks() to collect parent elements and trigger merging
  • Implemented #mergeAdjacentLinksInElement() to merge consecutive sibling links with matching URLs

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

yanfroes and others added 2 commits February 5, 2026 07:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

Two adjacent links with the same URL should be merged

1 participant

Comments