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
    • Improved link creation and editing to avoid unnecessary text replacements when the new link text matches the existing selection. This results in smoother, more responsive link updates and fewer unintended content changes when creating or editing links in 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 Ready Ready Preview Apr 13, 2026 7:43am
blocknote-website Error Error Apr 13, 2026 7:43am

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

📝 Walkthrough

Walkthrough

Both StyleManager.createLink and LinkToolbar.editLink now avoid replacing link text when the provided text equals the document's existing text; they read existing text with tr.doc.textBetween(...) and call tr.insertText(...) only if different, while still applying the link mark over the target range.

Changes

Cohort / File(s) Summary
Link transaction optimization
packages/core/src/editor/managers/StyleManager.ts, packages/core/src/extensions/LinkToolbar/LinkToolbar.ts
Both functions now compute existing text via tr.doc.textBetween(from, to) / textBetween(range.from, range.to) and conditionally call tr.insertText(...) only when the new text differs; mark application (tr.addMark(...)) remains unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I checked the words before a hop and write,
If letters match, I skip the bite.
Marks stay glowing, links still sing,
A careful hop keeps everything. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main issue being fixed: inserting a link was removing comments, and this PR fixes that behavior.
Description check ✅ Passed The description covers all required template sections with substantive content explaining the problem, rationale, changes, and impacts, though testing is marked TODO.
Linked Issues check ✅ Passed The PR successfully addresses issue #2573 by making tr.insertText conditional in createLink, preserving comments when link text remains unchanged.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the comment-removal issue via conditional insertText calls in StyleManager and LinkToolbar, with no unrelated modifications.
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
Copy link
Copy Markdown
Contributor

@nperez0111 nperez0111 left a comment

Choose a reason for hiding this comment

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

I'm okay with this as a stop-gap solution for right now, only because it is so simple. But, I would want more out of this later. But, that would come at the cost of higher complexity in comments. Let's just fix the user-facing issue for now

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/core/src/extensions/LinkToolbar/LinkToolbar.ts (1)

92-95: Consider extracting shared link-text replacement logic.

This same guard now exists in both LinkToolbarExtension.editLink and StyleManager.createLink; a shared helper would reduce behavior drift risk over time.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/extensions/LinkToolbar/LinkToolbar.ts` around lines 92 -
95, There is duplicated guard logic that compares textBetween(...) and only
calls tr.insertText(...) found in LinkToolbarExtension.editLink and
StyleManager.createLink; extract this into a shared helper (e.g.,
replaceLinkTextIfDifferent(tr, range, text) or LinkUtils.replaceTextIfDifferent)
and replace both implementations to call that helper so the equality check and
insertion live in one place and behavior stays consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/core/src/extensions/LinkToolbar/LinkToolbar.ts`:
- Around line 92-95: There is duplicated guard logic that compares
textBetween(...) and only calls tr.insertText(...) found in
LinkToolbarExtension.editLink and StyleManager.createLink; extract this into a
shared helper (e.g., replaceLinkTextIfDifferent(tr, range, text) or
LinkUtils.replaceTextIfDifferent) and replace both implementations to call that
helper so the equality check and insertion live in one place and behavior stays
consistent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 95806b06-0205-4cbc-a64f-060e399caae7

📥 Commits

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

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 13, 2026

Open in StackBlitz

@blocknote/ariakit

npm i https://pkg.pr.new/@blocknote/ariakit@2620

@blocknote/code-block

npm i https://pkg.pr.new/@blocknote/code-block@2620

@blocknote/core

npm i https://pkg.pr.new/@blocknote/core@2620

@blocknote/mantine

npm i https://pkg.pr.new/@blocknote/mantine@2620

@blocknote/react

npm i https://pkg.pr.new/@blocknote/react@2620

@blocknote/server-util

npm i https://pkg.pr.new/@blocknote/server-util@2620

@blocknote/shadcn

npm i https://pkg.pr.new/@blocknote/shadcn@2620

@blocknote/xl-ai

npm i https://pkg.pr.new/@blocknote/xl-ai@2620

@blocknote/xl-docx-exporter

npm i https://pkg.pr.new/@blocknote/xl-docx-exporter@2620

@blocknote/xl-email-exporter

npm i https://pkg.pr.new/@blocknote/xl-email-exporter@2620

@blocknote/xl-multi-column

npm i https://pkg.pr.new/@blocknote/xl-multi-column@2620

@blocknote/xl-odt-exporter

npm i https://pkg.pr.new/@blocknote/xl-odt-exporter@2620

@blocknote/xl-pdf-exporter

npm i https://pkg.pr.new/@blocknote/xl-pdf-exporter@2620

commit: b89283f

return;
}
tr.insertText(text, range.from, range.to);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should actually probably use style manager.createLink here no?

@nperez0111
Copy link
Copy Markdown
Contributor

@matthewlipski you may want to review this implementation too, to see which is worth keeping: #2592 test case might be good to take from there

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

2 participants