Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(gui): preserve valid trailing URL markers

Rach Pradhanbb623af2f314
diff --git a/gui/src/components/chat/utils/chatLinks.test.ts b/gui/src/components/chat/utils/chatLinks.test.ts
index 5ebcc19a..7c93cd29 100644
--- a/gui/src/components/chat/utils/chatLinks.test.ts
+++ b/gui/src/components/chat/utils/chatLinks.test.ts
@@ -29,8 +29,19 @@ describe("getChatLinkMatches URL vs Markdown delimiters", () => {
expect(firstUrl("see http://a.com/x*y here")).toBe("http://a.com/x*y");
});
- test("preserves a legitimate tilde in the URL path", () => {
+ test("preserves legitimate trailing asterisks on unwrapped URLs", () => {
+ expect(firstUrl("https://example.com/path*")).toBe("https://example.com/path*");
+ expect(firstUrl("https://example.com/?q=*")).toBe("https://example.com/?q=*");
+ });
+
+ test("preserves legitimate tildes in unwrapped URLs", () => {
expect(firstUrl("https://example.com/~user")).toBe("https://example.com/~user");
+ expect(firstUrl("https://example.com/~")).toBe("https://example.com/~");
+ });
+
+ test("removes only the matching closing delimiter", () => {
+ expect(firstUrl("*https://example.com/path**")).toBe("https://example.com/path*");
+ expect(firstUrl("~~https://example.com/path~~~")).toBe("https://example.com/path~");
});
test("still balances closing parens in a Wikipedia-style URL", () => {
diff --git a/gui/src/components/chat/utils/chatLinks.ts b/gui/src/components/chat/utils/chatLinks.ts
index b0ef81cf..1d8fdb84 100644
--- a/gui/src/components/chat/utils/chatLinks.ts
+++ b/gui/src/components/chat/utils/chatLinks.ts
@@ -4,7 +4,6 @@ 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;
@@ -77,14 +76,6 @@ function trimLinkCandidate(candidate: string): string {
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;
- }
-
if (TRAILING_CLOSERS.has(lastChar)) {
const opener =
lastChar === ")" ? "(" : lastChar === "]" ? "[" : lastChar === "}" ? "{" : null;
@@ -104,6 +95,35 @@ function trimLinkCandidate(candidate: string): string {
return value;
}
+function markdownClosingDelimiterBefore(text: string, start: number): string | null {
+ const opening = text.slice(0, start).match(/[~*]+$/u)?.[0];
+ if (opening == null) {
+ return null;
+ }
+
+ for (let index = 0; index < opening.length; index += 1) {
+ if (opening[index] === "*") {
+ continue;
+ }
+ if (opening[index] !== "~" || opening[index + 1] !== "~") {
+ return null;
+ }
+ index += 1;
+ }
+
+ return [...opening].reverse().join("");
+}
+
+function trimMarkdownWrappedLinkCandidate(text: string, start: number, candidate: string): string {
+ const value = trimLinkCandidate(candidate);
+ const closingDelimiter = markdownClosingDelimiterBefore(text, start);
+ if (closingDelimiter == null || !value.endsWith(closingDelimiter)) {
+ return value;
+ }
+
+ return trimLinkCandidate(value.slice(0, -closingDelimiter.length));
+}
+
function safeDecode(value: string): string {
try {
return decodeURIComponent(value);
@@ -425,7 +445,7 @@ export function getChatLinkMatches(
URL_PATTERN.lastIndex = 0;
let urlMatch: RegExpExecArray | null;
while ((urlMatch = URL_PATTERN.exec(text)) !== null) {
- const value = trimLinkCandidate(urlMatch[0]);
+ const value = trimMarkdownWrappedLinkCandidate(text, urlMatch.index, urlMatch[0]);
if (value.length === 0) {
continue;
}