Skip to content

Hyperlink: keep a custom href when autolink runs over an existing link #772

Closed

Description

@HMarzban

Part of #769. Depends on #770 (same file).

Summary

Autolink replaces a link's custom target with its label. A link whose label looks like a URL but points elsewhere loses its real target when the user types a space after it or bolds it. The user's target is lost without any sign. Upstream @tiptap/extension-link keeps the custom target.

Reproduce

Content: <p><a href="https://other.example/x">example.com</a> after</p> (a label that looks like a URL, a different target).

  1. Put the caret right after example.com (position 12) and type a space. Or select positions 1-13 and press Mod-b.
  2. Today: the link's href becomes https://example.com. Expected: it stays https://other.example/x.

Real case: the user edits a link's target with Mod-k, then keeps typing next to it.

Cause (read from code)

  • The add pass in extensions/extension-hyperlink/src/interactions/autolink.ts:117-139 finds the last word before the whitespace and calls tr.addMark(from, to, type.create({ href })).
  • It never checks whether that range already carries a hyperlink mark, so the new mark replaces the old one.
  • Upstream skips such ranges: @tiptap/extension-link 3.32.2, src/helpers/autolink.ts:184-190, uses getMarksBetween(link.from, link.to, newState.doc).some(...) before addMark.

Fix

In the add pass, skip a detected URL whose range already carries a hyperlink mark. Match the upstream rule. The partial-format fix (#770) does not touch this path.

Benchmark

Field Value
Metric Behaviour group href in scripts/bench/behaviour.ts (2 cases: custom href after a space, after bold 1-13). Bytes a StarterKit app pays.
Before href 0 / 2 (on main and with #770). Bytes: the #770 after value.
After not measured yet
Confounds Land after #770 and measure against it.
Condition jsdom on Bun 1.4.4, one laptop. esbuild minify, Bun zlib level 9.
How to run NODE_ENV=production bun scripts/bench/run.ts --ref <commit before this fix>
Pass rule href 2 / 2; every other behaviour case still passes; bytes under +50 B gzip; speed inside the control band.
Guard The UX contract below.
Owner Agent in the repo: code, tests, bench. Maintainer: the pad check after deploy.
Claim "A link keeps its own target when you type next to it."

UX contract

  • A plain example.com still autolinks.
  • Typing a URL right after an existing link (no space) still autolinks the new URL, and the old link keeps its own href.
  • Pad: edit a link's target with Mod-k, then type a space after it. The target stays.
  • Every Hyperlink: keep the whole link when part of it is formatted #770 test still passes.

Acceptance checks

  • Unit tests in src/interactions/__tests__/autolink.test.ts: a space at 12 and bold 1-13 keep https://other.example/x. They fail with the fix reverted.
  • bun run test:unit and the full package Cypress suite pass.
  • Bench before and after posted here. CHANGELOG Fixed line.

Out of scope

Activity

  1. added this to the v2.3.8 milestone on Nov 12, 2026
  2. self-assigned this
    on Nov 12, 2026
  3. added
    bug
    Editor
    on Nov 12, 2026
  4. HMarzban commented on Nov 12, 2026

    @HMarzban
    CollaboratorAuthor

    Measured (2026-11-12, working tree, not committed yet)

    Fix: the add pass in src/interactions/autolink.ts skips a detected URL whose range already carries a hyperlink mark (newState.doc.rangeHasMark(from, to, type)), as upstream does.

    Package bench, base = a copy of src/ with #770 only, head = #770 + this fix:

    Metric base head Change
    Bytes: npm dist/index.js (min / gz) 57,291 / 20,566 57,343 / 20,588 +52 / +22
    Bytes: a StarterKit app pays (min / gz) 70,997 / 25,258 71,049 / 25,273 +52 / +15
    Bytes: normalizeHref-only import (min / gz) 368,173 / 121,447 368,225 / 121,463 +52 / +16
    Behaviour: all cases 16/18 18/18 +2
    Behaviour: strip 10/10 10/10
    Behaviour: undo 2/2 2/2
    Behaviour: keep 4/4 4/4
    Behaviour: href 0/2 2/2
    Speed: pasteText2000 (ms) 464.816 471.735 +1% · control -0%
    Speed: boldAll2000 (ms) 351.89 347.929 -1% · control +2%
    Speed: typeEnd2000 (µs/key) 157.693 158.775 +1% · control +4%
    Speed: pasteOverSelection250k (ms) 23.238 22.931 -1% · control -2%
    Speed: boldPartOfLink (ms) 0.221 0.207 -6% · control +9%
    Speed: mount1000Links (ms) 26.872 28.189 +5% · control -1%

    jsdom on Bun 1.4.4, darwin arm64, 9 rounds. Speed rows are within the noise band (the control copy moved up to ±9 % in the same run).

    Checks run:

    • 3 new unit cases: a space after a custom-href link, bold over it and the space after, and a URL typed after it. The first two fail with the fix reverted; the third pins the UX contract and passes both ways.
    • bun run test:unit: 456 / 456.
    • CHANGELOG [Unreleased] → Fixed line.

    Still owed: the full package Cypress suite (runs once with #776), the pad check (Mod-k a new target, then type a space after the link), the commit.

  5. HMarzban commented on Nov 12, 2026

    @HMarzban
    CollaboratorAuthor

    Pad check (local dev-local, 2026-11-12)

    On a local pad with this fix and #770, #776 and #773 built in: a link example.com → https://other.example/x, caret right after it, a real space key, then typed more. The link kept https://other.example/x. A typed docs.plus followed by a real space key still autolinked to https://docs.plus.

    Not done: the Mod-k edit-then-type flow through the webapp form.

  6. HMarzban commented on Nov 12, 2026

    @HMarzban
    CollaboratorAuthor

    Committed

    c9cd2c98a on main, 2026-11-12, not pushed yet. One commit holds #770, #772, #773 and #776 together, by the maintainer's choice, so it cites each with Refs, and none closes on push. It is the same code the measured comments above describe.

    Open before this issue closes:

    • The full Mod-k flow: change a link's target in the form, then type next to the link.
    • Production pad check after deploy.
  7. HMarzban commented on Nov 12, 2026

    @HMarzban
    CollaboratorAuthor

    In-app check at c9cd2c98a (2026-11-12, local webapp :3001)

    Setup: headless Chrome for Testing 158.0.8127.18 (build 1254), Playwright 1.64.1, Bun 1.4.4, Apple M5 Max, 1440x900. All keys were real keyboard events.

    On a fresh scratch pad:

    • Typing docs at example.com autolinks it to https://example.com.
    • ArrowLeft × 4 puts the caret inside the link.
    • Mod-k opens the preview, and its Edit link button opens the Edit form.
    • The URL is set to https://other.example/x, then Apply.
    • With the caret after the link, a space and more are typed.

    The href stays https://other.example/x after the save, after the space and after more. The link text stays example.com, and more stays unlinked. This passed in 6 of 6 sessions, plus 2 of 2 verifier re-runs: 8 of 8. Four of the 8 sessions placed the caret with a real ArrowRight, and four with commands.focus.

    Negative control. The verifier rewrote the served autolink chunk to the parent logic. With that, the check failed in 0 of 2 sessions: the href went back to https://example.com after the space. This is not the parent build.

    The app runs the fix: the served chunks hold the rangeHasMark filter from c9cd2c98a. One setup step was not a key: the pad caret went to the end with commands.focus('end'). Only headless desktop was tested.

    This evidence covers the contract line "Pad: edit a link's target with Mod-k, then type a space after it." The production pad check after deploy is still owed.

    Raw data: local workflow folders wf4/p3-signed-in-ux (runs 7–12) and wf4/v-p3-signed-in-ux (v1, v2, s1, s2).

  8. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

    Production check, 2026-11-16. docs.plus at b7888dce1 (deployed 2026-11-15). Chrome on macOS, signed in. One scratch pad, never /demo. Each result was read from the editor DOM after the action. n = 1.

    Steps: type example.com so it autolinks. Open the link's preview, press Edit, set the URL to https://other.example/x and Apply. Put the caret after the link and type a space and more.

    Result: the link keeps https://other.example/x. Autolink did not rewrite it from the label. Cypress 289/289 was recorded on #773. The fix is c9cd2c98a, live since 3d90636a9 and still in b7888dce1. Release credit v2.3.8. Closing as done.

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

Metadata

Metadata

Assignees

Labels

EditorExtensionbugtiptap

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions