graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(gui): exclude trailing Markdown emphasis delimiters from autolinked URLs (#197)
Rach Pradhanb0b25c8027b4
diff --git a/gui/src/components/chat/utils/chatLinks.test.ts b/gui/src/components/chat/utils/chatLinks.test.tsnew file mode 100644index 00000000..8f2d9d1c--- /dev/null+++ b/gui/src/components/chat/utils/chatLinks.test.ts@@ -0,0 +1,56 @@+import { describe, expect, test } from "bun:test";++import { getChatLinkMatches } from "./chatLinks";++function firstUrl(text: string): string | undefined {+ return getChatLinkMatches(text).find((match) => match.kind === "url")?.value;+}++describe("getChatLinkMatches URL vs Markdown delimiters", () => {+ test("strips trailing ** from a bold-wrapped bare URL", () => {+ // Regression for #197: Cmd-hover underlined the closing ** and opened+ // http://localhost:3003** instead of http://localhost:3003.+ expect(firstUrl("**http://localhost:3003**")).toBe("http://localhost:3003");+ });++ test("strips a single trailing * from an italic-wrapped URL", () => {+ expect(firstUrl("*https://example.com*")).toBe("https://example.com");+ });++ test("strips trailing ~~ from a strikethrough-wrapped URL", () => {+ expect(firstUrl("~~https://example.com~~")).toBe("https://example.com");+ });++ test("strips Markdown delimiters interleaved with trailing punctuation", () => {+ expect(firstUrl("**http://localhost:3003.**")).toBe("http://localhost:3003");+ });++ test("preserves a legitimate interior asterisk in the URL path", () => {+ expect(firstUrl("see http://a.com/x*y here")).toBe("http://a.com/x*y");+ });++ test("preserves a legitimate tilde in the URL path", () => {+ expect(firstUrl("https://example.com/~user")).toBe("https://example.com/~user");+ });++ test("still balances closing parens in a Wikipedia-style URL", () => {+ expect(firstUrl("https://en.wikipedia.org/wiki/Foo_(bar)")).toBe(+ "https://en.wikipedia.org/wiki/Foo_(bar)",+ );+ });++ test("still trims a wrapping paren and trailing sentence punctuation", () => {+ expect(firstUrl("(https://example.com)")).toBe("https://example.com");+ expect(firstUrl("visit https://example.com.")).toBe("https://example.com");+ });++ test("reports the correct span end after trimming the delimiters", () => {+ const [match] = getChatLinkMatches("**http://localhost:3003**");+ expect(match).toMatchObject({+ kind: "url",+ start: 2,+ end: 2 + "http://localhost:3003".length,+ value: "http://localhost:3003",+ });+ });+});diff --git a/gui/src/components/chat/utils/chatLinks.ts b/gui/src/components/chat/utils/chatLinks.tsindex 6d3d1df3..b0ef81cf 100644--- a/gui/src/components/chat/utils/chatLinks.ts+++ b/gui/src/components/chat/utils/chatLinks.ts@@ -4,6 +4,7 @@ const FILE_PATH_PATTERN =const PATH_BOUNDARY_PATTERN = /[\s([{"'`]/u;const TRAILING_PUNCTUATION_PATTERN = /[.,;!?]+$/u;const TRAILING_CLOSERS = new Set([")", "]", "}", ">", "'", '"', "`"]);+const TRAILING_MARKDOWN_DELIMITERS = new Set(["*", "~"]);const FILE_BASENAME_EXTENSION_PATTERN = /\.[A-Za-z0-9_-]+$/u;const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/u;const WINDOWS_UNC_PATH_PATTERN = /^\\\\/u;@@ -58,24 +59,46 @@ export function isMailtoUrl(href: string): boolean {}function trimLinkCandidate(candidate: string): string {- let value = candidate.replace(TRAILING_PUNCTUATION_PATTERN, "");+ let value = candidate;++ let trimmed = true;+ while (trimmed && value.length > 0) {+ trimmed = false;++ const withoutPunctuation = value.replace(TRAILING_PUNCTUATION_PATTERN, "");+ if (withoutPunctuation.length !== value.length) {+ value = withoutPunctuation;+ trimmed = true;+ continue;+ }- while (value.length > 0) {const lastChar = value.at(-1);- if (lastChar == null || !TRAILING_CLOSERS.has(lastChar)) {+ if (lastChar == null) {break;}- const opener = lastChar === ")" ? "(" : lastChar === "]" ? "[" : lastChar === "}" ? "{" : null;- if (opener != null) {- const openerCount = value.split(opener).length - 1;- const closerCount = value.split(lastChar).length - 1;- if (closerCount <= openerCount) {- break;- }+ // Bare URLs/paths wrapped in Markdown emphasis (**url**, *url*, ~~url~~) capture the+ // closing delimiters; strip trailing markers so the clickable target excludes them.+ if (TRAILING_MARKDOWN_DELIMITERS.has(lastChar)) {+ value = value.slice(0, -1);+ trimmed = true;+ continue;}- value = value.slice(0, -1);+ if (TRAILING_CLOSERS.has(lastChar)) {+ const opener =+ lastChar === ")" ? "(" : lastChar === "]" ? "[" : lastChar === "}" ? "{" : null;+ if (opener != null) {+ const openerCount = value.split(opener).length - 1;+ const closerCount = value.split(lastChar).length - 1;+ if (closerCount <= openerCount) {+ break;+ }+ }++ value = value.slice(0, -1);+ trimmed = true;+ }}return value;