Skip to content

Commit 5465879

Browse files
fix(web): copied thread chips keep a valid Markdown link
A label with ] or \ broke the copied link. Build it with the same sanitizer the live relabel uses. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 947d1b8 commit 5465879

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
8888
import remarkBreaks from "remark-breaks";
8989
import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations";
9090
import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
91-
import { parseThreadLinkHref, THREAD_LINK_PROTOCOL } from "@t3tools/shared/threadLinks";
91+
import {
92+
formatThreadLink,
93+
parseThreadLinkHref,
94+
THREAD_LINK_PROTOCOL,
95+
} from "@t3tools/shared/threadLinks";
9296
import { AssistantCitationChip } from "./chat/AssistantCitationChip";
9397
import { ThreadContextChip } from "./ThreadContextChip";
9498
import remarkGfm from "remark-gfm";
@@ -3174,7 +3178,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
31743178
return environmentId ? (
31753179
<ThreadContextChip
31763180
record={{ environmentId, threadId: linkedThreadId, title }}
3177-
copyMarkdown={`[${title}](${href})`}
3181+
copyMarkdown={formatThreadLink(linkedThreadId, title)}
31783182
/>
31793183
) : (
31803184
<span>{title}</span>

‎packages/shared/src/threadLinks.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { parseThreadLinkHref, relabelThreadLinks } from "./threadLinks.ts";
3+
import { formatThreadLink, parseThreadLinkHref, relabelThreadLinks } from "./threadLinks.ts";
44

55
describe("thread links", () => {
66
it("takes the thread id verbatim, percent escapes included", () => {
@@ -16,6 +16,11 @@ describe("thread links", () => {
1616
expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
1717
});
1818

19+
it("formats a label that would otherwise break the Markdown link", () => {
20+
expect(formatThreadLink("t1", "Fix [ci] \\ build")).toBe("[Fix ci build](t3-thread://v1/t1)");
21+
expect(formatThreadLink("t1", " ] ")).toBe("[t1](t3-thread://v1/t1)");
22+
});
23+
1924
it("relabels links with the current title and leaves unknown threads alone", () => {
2025
const titles = new Map([["renamed", "Fix [the] build\nnow"]]);
2126
expect(

‎packages/shared/src/threadLinks.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export function parseThreadLinkHref(href: string): ThreadId | null {
1919
return Option.getOrNull(decodeThreadId(href.slice(THREAD_LINK_HREF_PREFIX.length)));
2020
}
2121

22+
/** A thread link whose label survives Markdown: no brackets, backslashes, or line breaks. */
23+
export function formatThreadLink(threadId: string, label: string): string {
24+
const cleaned = label
25+
.replace(/[[\]\\\r\n]/g, " ")
26+
.replace(/\s+/g, " ")
27+
.trim();
28+
return `[${cleaned || threadId}](${THREAD_LINK_HREF_PREFIX}${threadId})`;
29+
}
30+
2231
export function hasThreadLinks(markdown: string): boolean {
2332
return markdown.includes(`](${THREAD_LINK_HREF_PREFIX}`);
2433
}
@@ -31,14 +40,7 @@ export function relabelThreadLinks(
3140
if (!hasThreadLinks(markdown)) return markdown;
3241
return markdown.replace(THREAD_LINK, (source, _label: string, href: string) => {
3342
const threadId = parseThreadLinkHref(href);
34-
const label = threadId === null ? "" : sanitizeLinkLabel(title(threadId) ?? "");
35-
return label.length > 0 ? `[${label}](${href})` : source;
43+
const label = threadId === null ? undefined : title(threadId)?.trim();
44+
return threadId !== null && label ? formatThreadLink(threadId, label) : source;
3645
});
3746
}
38-
39-
function sanitizeLinkLabel(label: string): string {
40-
return label
41-
.replace(/[[\]\\\r\n]/g, " ")
42-
.replace(/\s+/g, " ")
43-
.trim();
44-
}

0 commit comments

Comments
 (0)