Skip to content

Creating a link over part of an existing link replaces the whole link's text with the selection #3277

Closed

Description

@NodarDavituri

What’s broken?

The formatting toolbar's link button, and Ctrl+K or Cmd+K, opens a URL form for the selected text. When the selection starts inside an existing link, or exactly where one ends, and does not cover the whole link, submitting that form replaces the whole link's text with the selected text, and the rest of the link's text is deleted. It happens when the pre-filled URL is submitted unchanged and when a different one is typed, and with the button, Ctrl+K and Cmd+K alike.

What did you expect to happen?

Submitting the pre-filled URL unchanged should change nothing, because the selected text already links to that address. Given a different address, only the selected text should link to it, and the rest of the link should keep its text and its old address: in the steps below, amp linked to the new address and Ex and le still linked to the old one. That is what already happens when the selection starts before the link (the re Exa row in the table below).

Steps to reproduce

In BlockNote's minimal example, a default useCreateBlockNote() editor rendered by BlockNoteView:

  1. Type Example, select it, press Ctrl+K (Cmd+K on macOS), type https://example.com and press Enter. The word is now a link.
  2. Select amp, the three characters in the middle of the link.
  3. Press Ctrl+K, or click the formatting toolbar's link button. The URL field is pre-filled with https://example.com. Press Enter without changing it.

Result: the paragraph holds only the link amp, and Ex and le are gone. editor.document[0].content is [{ type: "link", href: "https://example.com", content: [{ type: "text", text: "amp", styles: {} }] }]. With a different address typed over the pre-filled one in step 3, the result is the same text, amp, linked to the new address.

BlockNote version

0.57.0 (@blocknote/core, @blocknote/react and @blocknote/mantine). 0.56.0 and 0.55.0 give the same result in every case below. 0.54.2 gives the same result for the steps above and differs at the link's end, as the table shows.

Environment

Google Chrome 156.0.8078.76, headless, driven by Playwright 1.62.0-alpha, on macOS 26.4; React 19.3.1, @mantine/core 9.7.4, Vite 8.3.5. A selection made with Shift and the arrow keys, forward or backward, and one made by dragging the mouse gave the same result. Not tried in Firefox or Safari, or on Linux or Windows.

Additional context

More selections

Each case started from a freshly loaded page holding before Example after, with Example linked to https://example.com through the form as in step 1, and was run with Ctrl+K. The amp cases were also run with Cmd+K and with the toolbar button, with the same results. new stands for a new URL, https://example.org/new, typed over the pre-filled URL or into the empty field. The URL field opens pre-filled with https://example.com when the selection starts inside the link, and empty when it starts at the link's end or before the link; on 0.54.2 it also opens empty when the selection starts at the link's first character.

Selection 0.55.0, 0.56.0 and 0.57.0 0.54.2
amp, URL unchanged before [amp](https://example.com) after the same
amp, a new URL before [amp](new) after the same
Exa, a new URL before [Exa](new) after the same
e, the last character, URL unchanged before [e](https://example.com) after unchanged
e, a new URL before [e](new) after before [Exampl](https://example.com)[e](new) after, the link split, correct
af, the plain text right after the link, a new URL before [ af](new) after: the text reads before af after before [Example](https://example.com)[ af](new)ter, correct
le a, from inside the link past its end, a new URL before [le a](new) after: the text reads before le a after the same
re Exa, from before the link into it, a new URL befo[re Exa](new)[mple](https://example.com) after, correct the same
Example, the whole link, a new URL before [Example](new) after, correct the same

On 0.57.0, one Cmd+Z straight after the submit restores the link and its text.

In 0.55.0 and later

#3058 made getLinkMarkAtPos() find a link at either of its edges, which is right for a caret and for the link toolbar's own edit form, and widens this bug for a selection. A selection of a link's last character now loses the rest of the link, where 0.54.2 left the link alone or, given a new URL, split it. And a selection of plain text that starts exactly where a link ends is now treated as an edit of that link: the preceding link's text is replaced by the selection, which then appears twice, once linked where the old link was and once as the plain text it was. Both were measured in the browser on 0.54.2 and on 0.55.0, 0.56.0 and 0.57.0.

The same call, made programmatically

This makes the call the form makes on submit, against a bare editor holding before Example after with Example linked to https://example.com. Run in the same browser on 0.57.0, it leaves before amp after.

import { BlockNoteEditor } from "@blocknote/core";
import { TextSelection } from "prosemirror-state";

const editor = BlockNoteEditor.create({
  initialContent: [
    {
      type: "paragraph",
      content: [
        { type: "text", text: "before ", styles: {} },
        { type: "link", href: "https://example.com", content: "Example" },
        { type: "text", text: " after", styles: {} },
      ],
    },
  ],
});
editor.mount(document.getElementById("editor")!);

// Find the link's first character.
let from = -1;
editor.prosemirrorState.doc.descendants((node, pos) => {
  if (from === -1 && node.isText && node.marks.some((m) => m.type.name === "link")) {
    from = pos;
  }
  return from === -1;
});
editor.transact((tr) =>
  tr.setSelection(TextSelection.create(tr.doc, from + 2, from + 5)), // "amp"
);

// What the form CreateLinkButton renders does on submit.
editor.editLink(
  editor.getSelectedLinkUrl() ?? "",
  editor.getSelectedText(),
  editor.prosemirrorState.selection.from,
);

editor.prosemirrorState.doc.textContent; // "before amp after"

The cause

Paths and lines are at v0.57.0. CreateLinkButton builds its state from the selection, url: editor.getSelectedLinkUrl(), text: editor.getSelectedText() and range from the selection's from and to (packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx:95-102), and renders the link toolbar's own EditLinkMenuItems with that text and range and showTextField={false} (:164-173). On submit, EditLinkMenuItems calls editLink(validateUrl(currentUrl), currentText, props.range.from) (packages/react/src/components/LinkToolbar/EditLinkMenuItems.tsx:64), the link toolbar extension's method, which passes the three arguments on to editor.editLink() (packages/core/src/extensions/LinkToolbar/LinkToolbar.ts:64-70).

editLink() treats that position as a place inside a link to edit whole (packages/core/src/editor/managers/StyleManager.ts:236-256): it looks up getLinkMarkAtPos(position) (:242), which returns the whole link's from and to whenever the position is in a link or, since #3058, at either edge of one (:158-184), reads the whole link's text, and, because the selected text differs from it, calls tr.insertText(text, from, to) (:251) over the whole link, replacing all of its text with the selection's. In 0.54.2 the lookup is getLinkMarkAtPos(position + 1) (StyleManager.ts:225, insertText() at :234), which misses a selection of the last character or one starting at a link's end, and so only the other cases lose text there.

editLink() is right for the link toolbar's own edit form, whose text field starts as the whole link's text. The create-link form passes only the selected text, so the same call deletes the part of the link that was not selected.

Suggested fix

One of:

  • Commit the create-link form over the selection rather than over the link at its start: for example, let EditLinkMenuItems take a way to commit over range itself, and have CreateLinkButton use it, or call createLink() (StyleManager.ts:209-228), which already marks exactly the selection, from the create path.
  • Or have editLink() expand to the whole link only when it is given a collapsed position, and keep to a non-empty selection's own range otherwise.

Either way, a selection inside a link that is given a different address would become a link of its own and leave the rest of the link as it was.

What this project does meanwhile

The BlockNote module for Drupal (https://www.drupal.org/project/blocknote) replaces both link forms with its own, and commits a link through a function of its own that does what editLink() does, the same insertText()-when-changed and addMark() in one transact(), over exactly the range its caller gives, without expanding it. The link toolbar's edit form gives the link's own full range, so it still edits the whole link, as upstream's does. The formatting toolbar's form gives the selection, so a selected part given a different address becomes a link of its own and the rest keeps its text and address, and a test pins that.

AI use: I maintain the BlockNote module for Drupal. Its code, tests and documentation, this report and the reproduction behind it were made with AI coding agents that I direct.

Activity

  1. nperez0111 commented on Nov 2, 2026

    @nperez0111
    Contributor

    I would look at the behavior of what Notion does to determine what to do here. In my head, expanding the link seems like the right thing to do, since I want to over for cases where the user may have missed a character by accident

  2. NodarDavituri commented on Nov 3, 2026

    @NodarDavituri
    Author

    I checked couple of editors, linking Example and then selecting amp inside it:

    Same URL New URL
    Notion no change only amp gets the new URL, Ex and le keep the old one
    CKEditor 5 no change same as Notion
    Tiptap's demo no change, whole link selected afterwards whole link gets the new URL
    Google Docs widens the selection to the whole link when the dialog opens whole link gets the new URL

    None of them deletes text.

    A selection that runs from inside the link past its end becomes one link in all of them, with the new URL or (except in Notion) the unchanged one. So Notion splits rather than expands.

    Either behavior would be fine for us, since the part that matters is not losing the rest of the link's text. If you prefer expanding, Tiptap's behavior is the closest match, and I'm happy to open a PR for whichever you choose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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