Skip to content

fix: Inserting link removes comment#2620

Open
matthewlipski wants to merge 1 commit intomainfrom
link-overwrite-comments-fix
Open

fix: Inserting link removes comment#2620
matthewlipski wants to merge 1 commit intomainfrom
link-overwrite-comments-fix

Conversation

@matthewlipski
Copy link
Copy Markdown
Collaborator

@matthewlipski matthewlipski commented Apr 2, 2026

Summary

When adding a link, if the end aligns with the end of a comment, the comment will be removed across the content spanned by the link.

This is because adding a link calls tr.insertText, which replaces the content spanned by the link. This is a problem especially with comments as they are not inclusive in their TipTap mark spec. Therefore, when content that they cover gets replaced, they get cleared instead of preserved.

This PR makes it so that tr.insertText only gets called if the text has actually changed, which doesn't happen in most cases. If it does happen, there are a bunch of edge cases that have to be considered, e.g.:

Given the following content, where {} marks the span of a link and [] marks the span of a comment:

1{23[456}78]9

If the link's text is changed to something completely different, like "qwe", where should the comment start?

Therefor, this PR only covers the case where the text remains the same.

Closes #2573

Rationale

It's annoying for users to accidentally clear comments when trying to add links.

Changes

  • Made tr.insertText get called conditionally in createLink.

Impact

N/A

Testing

TODO

Screenshots/Video

N/A

Checklist

  • Code follows the project's coding standards.
  • Unit tests covering the new feature have been added.
  • All existing tests pass.
  • The documentation has been updated to reflect the new feature

Additional Notes

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced link creation and editing functionality to intelligently check existing text content before making replacements. Links now handle scenarios where the selected content matches the existing text more efficiently, delivering a cleaner and more responsive editing experience when creating, updating, or managing links throughout your documents.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
blocknote Error Error Apr 2, 2026 1:23pm
blocknote-website Ready Ready Preview Apr 2, 2026 1:23pm

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5a14459f-060b-4dce-852b-a4ff10e6d0aa

📥 Commits

Reviewing files that changed from the base of the PR and between 07df972 and 0490e5d.

📒 Files selected for processing (2)
  • packages/core/src/editor/managers/StyleManager.ts
  • packages/core/src/extensions/LinkToolbar/LinkToolbar.ts

📝 Walkthrough

Walkthrough

The changes optimize link creation and editing transactions by conditionally inserting text only when it differs from the existing document content. Both StyleManager.createLink and LinkToolbar.editLink now check if replacement text matches the current text before performing insertion, reducing unnecessary mutations.

Changes

Cohort / File(s) Summary
Link transaction optimization
packages/core/src/editor/managers/StyleManager.ts, packages/core/src/extensions/LinkToolbar/LinkToolbar.ts
Added conditional text insertion logic: both methods now retrieve existing text via textBetween() and only call insertText() if the provided text differs from the existing content, while preserving mark application behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A link once trampled all in sight,
But now we check before we write,
When text's already there to stay,
We skip the step and hop away! ✨
Comments keep their glow so bright,
As marks dance on with pure delight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: Inserting link removes comment' is specific and accurately describes the main issue being fixed in the pull request.
Description check ✅ Passed The description covers most required sections with clear rationale and explanation of changes, though Testing section is marked TODO and screenshots are N/A.
Linked Issues check ✅ Passed The PR correctly addresses the linked issue #2573 by preventing comment removal when adding links through conditional insertText calls.
Out of Scope Changes check ✅ Passed All changes are tightly scoped to the specific issue: only modifying createLink and editLink to conditionally call insertText when text actually changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch link-overwrite-comments-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@matthewlipski matthewlipski requested a review from nperez0111 April 2, 2026 13:23
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.

Add link on commented text removes the comment highlight

1 participant