The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep Markdown link destinations un-chipped (#125886)
kokhlo2708ee4dba82
diff --git a/apps/desktop/src/app/chat/composer/url-refs.test.ts b/apps/desktop/src/app/chat/composer/url-refs.test.tsindex 65aa9c3f3ed..01699c42ab7 100644--- a/apps/desktop/src/app/chat/composer/url-refs.test.ts+++ b/apps/desktop/src/app/chat/composer/url-refs.test.ts@@ -116,6 +116,37 @@ describe('linkifyUrls', () => {)})+ // #125886: a pasted [label](url) is link syntax, not prose — rewriting the+ // destination into a reference marker leaves the link pointing at the+ // marker instead of the href.+ it('preserves the destination of a Markdown link', () => {+ const text = 'Show this retained source without using tools: [Mission Control issue 7](https://example.invalid/projects/synthetic/issues/7)'++ expect(linkifyUrls(text)).toBe(text)+ })++ it('preserves a Markdown link with parenthesized URL segments', () => {+ expect(linkifyUrls('see [wiki](https://en.wikipedia.org/wiki/A_(b)) now')).toBe(+ 'see [wiki](https://en.wikipedia.org/wiki/A_(b)) now'+ )+ })++ it('preserves a Markdown link being composed, before its closing paren', () => {+ expect(linkifyUrls('[docs](https://example.dev/a')).toBe('[docs](https://example.dev/a')+ })++ it('still chips prose links around a Markdown link', () => {+ expect(linkifyUrls('read https://before.dev then [docs](https://example.dev/a) and https://after.dev')).toBe(+ 'read @url:`https://before.dev` then [docs](https://example.dev/a) and @url:`https://after.dev`'+ )+ })++ it('does not treat a bare `](` in prose as a link label end', () => {+ expect(linkifyUrls('array indexing a](1) then https://example.dev')).toBe(+ 'array indexing a](1) then @url:`https://example.dev`'+ )+ })+it('leaves text without a scheme alone', () => {expect(linkifyUrls('example.dev/a and src/foo.ts')).toBe('example.dev/a and src/foo.ts')})@@ -307,4 +338,14 @@ describe('chipTypedUrlOnSpace', () => {editor.remove()})++ it('does not chip a link typed into a Markdown link destination', () => {+ const text = 'see [docs]( https://example.dev/a'+ const { editor, event } = spaceOn(text, text.length)++ expect(chipTypedUrlOnSpace(event)).toBe(false)+ expect(composerPlainText(editor)).toBe(text)++ editor.remove()+ })})diff --git a/apps/desktop/src/app/chat/composer/url-refs.ts b/apps/desktop/src/app/chat/composer/url-refs.tsindex c1ba2e3c9c5..8e28baf55ea 100644--- a/apps/desktop/src/app/chat/composer/url-refs.ts+++ b/apps/desktop/src/app/chat/composer/url-refs.ts@@ -123,6 +123,61 @@ function markdownCodeRanges(text: string) {return [...fenced, ...inlineCodeRanges(text, fenced)]}+/** True when a `[` that nothing has closed precedes the `]` at `index` on the+ * same line, so the `](` there really ends a link label. Brackets pair by+ * depth, so `[a [b] c](` reads as one label. */+function hasLabelOpenerBefore(text: string, index: number) {+ let depth = 0++ for (let cursor = index - 1; cursor >= 0 && text[cursor] !== '\n'; cursor -= 1) {+ const char = text[cursor]++ if (char === ']') {+ depth += 1+ } else if (char === '[') {+ if (depth === 0) {+ return true+ }++ depth -= 1+ }+ }++ return false+}++/** Markdown inline link destinations — the `(dest)` of `[label](dest)`. A+ * destination is link syntax the user wrote or pasted, not prose to chip:+ * rewriting it into an `@url:` reference leaves the link pointing at a+ * reference marker instead of the href, and the rendered link breaks. An+ * unclosed `](` still being composed takes the rest of the line, so a link+ * typed by hand isn't chipped mid-destination either. */+function markdownLinkDestinationRanges(text: string) {+ const ranges: TextRange[] = []++ for (const match of text.matchAll(/\]\(/g)) {+ const open = (match.index ?? 0) + 1++ if (!hasLabelOpenerBefore(text, match.index ?? 0)) {+ continue+ }++ // Parentheses pair inside the destination, so a Wikipedia-style URL keeps+ // its own `(b)` and the span still ends at the link's closing paren.+ let depth = 1+ let cursor = open + 1++ while (cursor < text.length && text[cursor] !== '\n' && depth > 0) {+ depth += text[cursor] === '(' ? 1 : text[cursor] === ')' ? -1 : 0+ cursor += 1+ }++ ranges.push({ end: cursor, start: open + 1 })+ }++ return ranges+}+/** A URL at the end of a sentence carries the punctuation that ended it. */function splitUrlTail(raw: string) {let url = raw.replace(/[,.;:!?]+$/, '')@@ -151,9 +206,11 @@ export const hasHttpUrlHost = (url: string) => {export function linkifyUrls(text: string) {REF_RE.lastIndex = 0- // URLs inside an existing `@url:` directive or a Markdown code span are not- // prose to chip — the directive is already a reference, and code is verbatim- // payload the user pasted (a stack trace, a command, a log line).+ // URLs inside an existing `@url:` directive, a Markdown code span, or a+ // Markdown link destination are not prose to chip — the directive is already+ // a reference, code is verbatim payload the user pasted (a stack trace, a+ // command, a log line), and a link destination is the href of a link the+ // user wrote.const protectedRanges = Array.from(text.matchAll(REF_RE)).map(match => {const start = match.index ?? 0@@ -161,6 +218,7 @@ export function linkifyUrls(text: string) {})protectedRanges.push(...markdownCodeRanges(text))+ protectedRanges.push(...markdownLinkDestinationRanges(text))let out = ''let cursor = 0@@ -271,11 +329,16 @@ export function chipTypedUrlOnSpace(event: KeyboardEvent<HTMLDivElement>) {}// A link typed inside a code block or span is verbatim payload, not prose —- // chipping it would rewrite code the user is authoring.+ // chipping it would rewrite code the user is authoring. Same for a URL being+ // typed into a link destination the user is still composing.if (containsIndex(markdownCodeRanges(before), before.length - token.length)) {return false}+ if (containsIndex(markdownLinkDestinationRanges(before), before.length - token.length)) {+ return false+ }+const { trailing, url } = splitUrlTail(token)if (!hasHttpUrlHost(url)) {