fix(desktop): unwrap angle-bracket links before parsing

Co-authored-by: Taylor Ho <taylorkmho@gmail.com>
Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
This commit is contained in:
Taylor Ho
2026-08-04 21:09:50 -07:00
committed by Carl
parent c8b5b5e281
commit 10998681b4
2 changed files with 40 additions and 7 deletions

View File

@@ -135,6 +135,10 @@ function shouldAppendSpaceAfterPaste(text: string): boolean {
return PASTED_LINK_AT_END_RE.test(trimmedEnd);
}
function unwrapExactHttpAutolink(text: string): string | null {
return /^<(https?:\/\/[^\s<>]+)>$/i.exec(text)?.[1] ?? null;
}
const LinkPasteTrailingSpace = Extension.create({
name: "linkPasteTrailingSpace",
@@ -487,6 +491,32 @@ export function useRichTextEditor({
}),
],
editorProps: {
handleDOMEvents: {
paste: (view, event) => {
const url = unwrapExactHttpAutolink(
(event as ClipboardEvent).clipboardData?.getData("text/plain") ??
"",
);
if (!url) return false;
const link = view.state.schema.marks.link;
if (!link) return false;
const { from, to } = view.state.selection;
let transaction = view.state.tr.replaceRangeWith(
from,
to,
view.state.schema.text(url, [link.create({ href: url })]),
);
const end = transaction.mapping.map(to);
transaction = transaction.insertText(" ", end);
transaction = transaction.removeMark(end, end + 1, link);
transaction = transaction.setSelection(
TextSelection.create(transaction.doc, end + 1),
);
view.dispatch(transaction.setStoredMarks([]).scrollIntoView());
event.preventDefault();
return true;
},
},
attributes: {
autocapitalize: "none",
autocorrect: "off",

View File

@@ -480,7 +480,7 @@ test("link preview style defaults to compact and Rich unfurls descriptions", asy
await page.getByTestId("link-preview-style-compact").click();
});
test("link preview paste paints its loading card before cold resolver work", async ({
test("angle-bracket link preview paste paints before cold resolver work", async ({
page,
}) => {
const previewUrl = "https://github.com/block/buzz/pull/3246?paste=async";
@@ -489,9 +489,10 @@ test("link preview paste paints its loading card before cold resolver work", asy
const input = page.getByTestId("message-input");
await input.focus();
const firstPaint = await input.evaluate(async (element, url) => {
const pasteText = `<${previewUrl}>`;
const firstPaint = await input.evaluate(async (element, pasteText) => {
const clipboardData = new DataTransfer();
clipboardData.setData("text/plain", url);
clipboardData.setData("text/plain", pasteText);
const startedAt = performance.now();
element.dispatchEvent(
new ClipboardEvent("paste", {
@@ -510,7 +511,7 @@ test("link preview paste paints its loading card before cold resolver work", asy
),
text: element.textContent,
};
}, previewUrl);
}, pasteText);
expect(firstPaint.text).toContain(previewUrl);
expect(firstPaint.resolverStarted).toBe(false);
expect(firstPaint.elapsedMs).toBeLessThan(100);
@@ -518,9 +519,11 @@ test("link preview paste paints its loading card before cold resolver work", asy
.locator("[data-composer-link-previews]")
.locator('[data-link-preview="github-pull-request"]');
await expect(input).toContainText(previewUrl, { timeout: 1_000 });
await expect(composerPreview).toHaveAttribute("data-state", "processing", {
timeout: 1_000,
});
await expect(composerPreview).toHaveAttribute(
"data-state",
/^(processing|done)$/,
{ timeout: 1_000 },
);
await expect(page.getByTestId("send-message")).toBeEnabled();
});