Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-3280: fix(editor): repair split email links caused by autolink + inclusive:false

Kagura2ab7b5b7afa4
diff --git a/packages/views/editor/extensions/autolink-email-repair.test.ts b/packages/views/editor/extensions/autolink-email-repair.test.ts
new file mode 100644
index 0000000000..86164b4d2e
--- /dev/null
+++ b/packages/views/editor/extensions/autolink-email-repair.test.ts
@@ -0,0 +1,142 @@
+import { afterEach, describe, expect, it } from "vitest";
+import { Editor } from "@tiptap/core";
+import StarterKit from "@tiptap/starter-kit";
+import Link from "@tiptap/extension-link";
+import { Markdown } from "@tiptap/markdown";
+import { AutolinkEmailRepairExtension } from "./autolink-email-repair";
+
+const LinkExtension = Link.extend({ inclusive: false }).configure({
+ openOnClick: false,
+ autolink: true,
+ linkOnPaste: true,
+ defaultProtocol: "https",
+});
+
+let editor: Editor | null = null;
+
+function makeEditor(): Editor {
+ const element = document.createElement("div");
+ document.body.appendChild(element);
+ return new Editor({
+ element,
+ extensions: [
+ StarterKit.configure({ link: false }),
+ LinkExtension,
+ AutolinkEmailRepairExtension,
+ Markdown.configure({ indentation: { style: "space", size: 3 } }),
+ ],
+ });
+}
+
+/**
+ * Simulate the split-link scenario: a mailto: link followed by trailing plain
+ * text in the same paragraph. This mirrors what happens when autolink fires on
+ * `[email protected]` and the user then types `m`.
+ */
+function setSplitEmailLink(
+ ed: Editor,
+ linkText: string,
+ trailingText: string,
+): void {
+ const linkMark = ed.schema.marks.link!.create({
+ href: `mailto:${linkText}`,
+ });
+ const { tr } = ed.state;
+ const linkedNode = ed.schema.text(linkText, [linkMark]);
+ const trailingNode = ed.schema.text(trailingText);
+ const paragraph = ed.schema.nodes.paragraph!.create(null, [
+ linkedNode,
+ trailingNode,
+ ]);
+ tr.replaceWith(0, ed.state.doc.content.size, paragraph);
+ ed.view.dispatch(tr);
+}
+
+afterEach(() => {
+ editor?.destroy();
+ editor = null;
+ document.body.innerHTML = "";
+});
+
+describe("AutolinkEmailRepairExtension", () => {
+ it("extends .co link when trailing text completes .com", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", "m");
+
+ const markdown = editor.getMarkdown().trim();
+ expect(markdown).toBe("[[email protected]](mailto:[email protected])");
+ });
+
+ it("extends .co link when trailing text completes .co.uk", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", ".uk");
+
+ const markdown = editor.getMarkdown().trim();
+ expect(markdown).toBe(
+ );
+ });
+
+ it("extends .i link when trailing text completes .io", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", "o");
+
+ const markdown = editor.getMarkdown().trim();
+ expect(markdown).toBe("[[email protected]](mailto:[email protected])");
+ });
+
+ it("does NOT absorb random text after an email link", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", " hello");
+
+ const markdown = editor.getMarkdown().trim();
+ // The link should stay as-is; " hello" should remain outside.
+ expect(markdown).toContain("[[email protected]](mailto:[email protected])");
+ expect(markdown).toContain("hello");
+ expect(markdown).not.toBe(
+ "[[email protected] hello](mailto:[email protected] hello)",
+ );
+ });
+
+ it("does NOT affect regular URL links", () => {
+ editor = makeEditor();
+ const linkMark = editor.schema.marks.link!.create({
+ href: "https://example.co",
+ });
+ const { tr } = editor.state;
+ const linkedNode = editor.schema.text("example.co", [linkMark]);
+ const trailingNode = editor.schema.text("m");
+ const paragraph = editor.schema.nodes.paragraph!.create(null, [
+ linkedNode,
+ trailingNode,
+ ]);
+ tr.replaceWith(0, editor.state.doc.content.size, paragraph);
+ editor.view.dispatch(tr);
+
+ const markdown = editor.getMarkdown().trim();
+ // The trailing "m" should NOT be absorbed into the URL link.
+ expect(markdown).toContain("[example.co](https://example.co)");
+ expect(markdown).toMatch(/\)m/);
+ });
+
+ it("does NOT extend when no prefix of trailing text forms a valid email", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", "random");
+
+ const markdown = editor.getMarkdown().trim();
+ // "[email protected]" — no prefix of "random" makes a valid email.
+ expect(markdown).toContain("[[email protected]](mailto:[email protected])");
+ expect(markdown).toContain("random");
+ });
+
+ it("finds the longest valid extension (.co + .uk, not just .co + .)", () => {
+ editor = makeEditor();
+ setSplitEmailLink(editor, "[email protected]", ".uk extra");
+
+ const markdown = editor.getMarkdown().trim();
+ expect(markdown).toContain(
+ );
+ expect(markdown).toContain("extra");
+ });
+});
diff --git a/packages/views/editor/extensions/autolink-email-repair.ts b/packages/views/editor/extensions/autolink-email-repair.ts
new file mode 100644
index 0000000000..7366b7d111
--- /dev/null
+++ b/packages/views/editor/extensions/autolink-email-repair.ts
@@ -0,0 +1,102 @@
+/**
+ * AutolinkEmailRepair — fixes split email links caused by TipTap autolink.
+ *
+ * Problem: when a user types `[email protected]`, TipTap's autolink fires
+ * because `.co` is a valid TLD. With `inclusive: false` on the Link extension,
+ * the next character typed (e.g. `m` to complete `.com`) lands outside the link
+ * mark. The markdown output becomes `[[email protected]](mailto:…)m` instead
+ * of `[[email protected]](mailto:…)`.
+ *
+ * Fix: an `appendTransaction` plugin that runs after every doc change. It scans
+ * for `mailto:` link marks that are immediately followed by plain text. If the
+ * combined string (link text + trailing text) forms a valid email according to
+ * the shared link detector, the plugin extends the link mark to cover the full
+ * address and updates the href.
+ *
+ * Only `mailto:` links are affected — regular URL links are left untouched.
+ */
+import { Extension } from "@tiptap/core";
+import { Plugin, PluginKey } from "@tiptap/pm/state";
+import { detectLinks } from "@multica/ui/markdown/linkify";
+
+export const AutolinkEmailRepairExtension = Extension.create({
+ name: "autolinkEmailRepair",
+
+ addProseMirrorPlugins() {
+ const linkType = this.editor.schema.marks.link;
+ if (!linkType) return [];
+
+ return [
+ new Plugin({
+ key: new PluginKey("autolinkEmailRepair"),
+ appendTransaction(_transactions, _oldState, newState) {
+ const { tr } = newState;
+ let modified = false;
+
+ newState.doc.descendants((node, pos) => {
+ if (!node.isTextblock) return;
+
+ // Walk children of this text block looking for link-mark boundaries.
+ for (let i = 0; i < node.childCount - 1; i++) {
+ const child = node.child(i);
+ const nextChild = node.child(i + 1);
+
+ // Find a text node with a mailto: link mark followed by a plain
+ // text node (no link mark).
+ const linkMark = child.marks.find(
+ (m) =>
+ m.type === linkType &&
+ typeof m.attrs.href === "string" &&
+ m.attrs.href.startsWith("mailto:"),
+ );
+ if (!linkMark) continue;
+ if (!nextChild.isText || !nextChild.text) continue;
+ if (nextChild.marks.some((m) => m.type === linkType)) continue;
+
+ const linkText = child.text || "";
+ const trailingText = nextChild.text!;
+
+ // Try progressively longer slices of the trailing text to find the
+ // longest valid email extension (e.g. `.co` + `.uk` → `.co.uk`).
+ let bestLen = 0;
+ let bestHref = "";
+ for (let len = 1; len <= trailingText.length; len++) {
+ const candidate = linkText + trailingText.slice(0, len);
+ const matches = detectLinks(candidate);
+ const match = matches[0];
+ if (
+ matches.length === 1 &&
+ match &&
+ match.type === "email" &&
+ match.text === candidate
+ ) {
+ bestLen = len;
+ bestHref = match.url;
+ }
+ }
+
+ if (bestLen > 0) {
+ // Compute absolute positions. `pos` is the position before the
+ // text block node; content starts at pos + 1. We need the offset
+ // of `child` within the text block.
+ let childOffset = 0;
+ for (let j = 0; j < i; j++) {
+ childOffset += node.child(j).nodeSize;
+ }
+ const linkFrom = pos + 1 + childOffset;
+ const linkEnd = linkFrom + linkText.length;
+ const extendTo = linkEnd + bestLen;
+
+ const newMark = linkType.create({ href: bestHref });
+ tr.addMark(linkFrom, extendTo, newMark);
+ modified = true;
+ }
+ }
+ });
+
+ return modified ? tr : undefined;
+ },
+ }),
+ ];
+ },
+});
diff --git a/packages/views/editor/extensions/index.ts b/packages/views/editor/extensions/index.ts
index aac3f459c1..b9b0ea9492 100644
--- a/packages/views/editor/extensions/index.ts
+++ b/packages/views/editor/extensions/index.ts
@@ -52,6 +52,7 @@ import { FileCardExtension } from "./file-card";
import { ImageView } from "./image-view";
import { BlockMathExtension, InlineMathExtension } from "./math";
import { HighlightExtension } from "./highlight";
+import { AutolinkEmailRepairExtension } from "./autolink-email-repair";
const lowlight = createLowlight(common);
@@ -179,6 +180,7 @@ export function createEditorExtensions(
// linkOnPaste relies on Link's handlePaste plugin firing first;
// markdownPaste's handlePaste is a catch-all that returns true.
LinkExtension,
+ AutolinkEmailRepairExtension,
ImageExtension,
// renderWrapper wraps the table in `<div class="tableWrapper">` (the same
// wrapper the resizable NodeView emits), which prose.css styles with