Skip to content

Commit 7f04023

Browse files
fix(web,mobile): thread links keep the project icon and skip code
Web shows a thread link with its project icon again, as #16782 did, but the label is now the thread's live title. Mobile's relabel no longer rewrites links written inside code spans or fences. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5465879 commit 7f04023

4 files changed

Lines changed: 77 additions & 15 deletions

File tree

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ 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 {
92-
formatThreadLink,
93-
parseThreadLinkHref,
94-
THREAD_LINK_PROTOCOL,
95-
} from "@t3tools/shared/threadLinks";
91+
import { parseThreadLinkHref, THREAD_LINK_PROTOCOL } from "@t3tools/shared/threadLinks";
9692
import { AssistantCitationChip } from "./chat/AssistantCitationChip";
97-
import { ThreadContextChip } from "./ThreadContextChip";
93+
import { MarkdownThreadLink } from "./chat/MarkdownThreadLink";
9894
import remarkGfm from "remark-gfm";
9995
import type { Processor } from "unified";
10096
import { isWindowsAbsolutePath } from "@t3tools/shared/path";
@@ -3171,17 +3167,14 @@ const CHAT_MARKDOWN_COMPONENTS = {
31713167
} = use(ChatMarkdownRendererContext);
31723168
const citation = href ? parseAssistantCitationHref(href) : null;
31733169
if (citation) return <AssistantCitationChip citation={citation} />;
3174-
// A thread link is the same chip as an attached thread: current title, opens in the app.
3170+
// A thread link names a thread in this message's environment and opens it in the app.
31753171
const linkedThreadId = href ? parseThreadLinkHref(href) : null;
31763172
if (linkedThreadId) {
3177-
const title = hastPlainTextDeep(node) || linkedThreadId;
3173+
const label = hastPlainTextDeep(node) || linkedThreadId;
31783174
return environmentId ? (
3179-
<ThreadContextChip
3180-
record={{ environmentId, threadId: linkedThreadId, title }}
3181-
copyMarkdown={formatThreadLink(linkedThreadId, title)}
3182-
/>
3175+
<MarkdownThreadLink environmentId={environmentId} threadId={linkedThreadId} label={label} />
31833176
) : (
3184-
<span>{title}</span>
3177+
<span>{label}</span>
31853178
);
31863179
}
31873180
const contextReference = href ? parseComposerContextHref(href) : null;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import { formatThreadLink } from "@t3tools/shared/threadLinks";
4+
import { Link } from "@tanstack/react-router";
5+
import { MessageSquareTextIcon } from "lucide-react";
6+
7+
import { useProject, useThreadShell } from "../../state/entities";
8+
import { ProjectFavicon } from "../ProjectFavicon";
9+
10+
/**
11+
* A `t3-thread://` link in chat. It shows the thread's current title, so a rename
12+
* reaches every message that links to it, and leads with the thread's project icon
13+
* the way a web link leads with its favicon. `label` is what the message wrote,
14+
* shown only when this client cannot see the thread. Opens the thread in the app.
15+
*/
16+
export function MarkdownThreadLink(props: {
17+
readonly environmentId: EnvironmentId;
18+
readonly threadId: ThreadId;
19+
readonly label: string;
20+
}) {
21+
const thread = useThreadShell(scopeThreadRef(props.environmentId, props.threadId));
22+
const project = useProject(
23+
thread === null ? null : scopeProjectRef(props.environmentId, thread.projectId),
24+
);
25+
const title = thread?.title.trim() || props.label;
26+
return (
27+
<Link
28+
to="/$environmentId/$threadId"
29+
params={{ environmentId: props.environmentId, threadId: props.threadId }}
30+
title={project?.title}
31+
data-markdown-copy={formatThreadLink(props.threadId, title)}
32+
>
33+
<span
34+
className="ms-[0.25em] me-[0.2em] inline-flex size-[14px] [vertical-align:-0.125em]"
35+
aria-hidden
36+
>
37+
{project === null ? (
38+
<MessageSquareTextIcon className="block size-full shrink-0" />
39+
) : (
40+
<ProjectFavicon project={project} className="size-full" />
41+
)}
42+
</span>
43+
{title}
44+
</Link>
45+
);
46+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ describe("thread links", () => {
1616
expect(parseThreadLinkHref("t3-thread://v1/ ")).toBeNull();
1717
});
1818

19+
it("leaves links inside code spans and fences as written", () => {
20+
const markdown = [
21+
"Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
22+
"```md",
23+
"[old](t3-thread://v1/t1)",
24+
"```",
25+
"After [old](t3-thread://v1/t1)",
26+
].join("\n");
27+
expect(relabelThreadLinks(markdown, () => "New")).toBe(
28+
[
29+
"Live [New](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
30+
"```md",
31+
"[old](t3-thread://v1/t1)",
32+
"```",
33+
"After [New](t3-thread://v1/t1)",
34+
].join("\n"),
35+
);
36+
});
37+
1938
it("formats a label that would otherwise break the Markdown link", () => {
2039
expect(formatThreadLink("t1", "Fix [ci] \\ build")).toBe("[Fix ci build](t3-thread://v1/t1)");
2140
expect(formatThreadLink("t1", " ] ")).toBe("[t1](t3-thread://v1/t1)");

‎packages/shared/src/threadLinks.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import * as Schema from "effect/Schema";
99
*/
1010
export const THREAD_LINK_PROTOCOL = "t3-thread";
1111
const THREAD_LINK_HREF_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`;
12-
const THREAD_LINK = /\[([^\]\n]*)\]\((t3-thread:\/\/v1\/[^\s)]+)\)/g;
12+
// Code comes first in the alternation so a link written inside a code span or fence is skipped.
13+
const THREAD_LINK_OUTSIDE_CODE =
14+
/(?<fence>(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(`+)[^\n]*?\4)|\[[^\]\n]*\]\((?<href>t3-thread:\/\/v1\/[^\s)]+)\)/g;
1315

1416
const decodeThreadId = Schema.decodeUnknownOption(ThreadId);
1517

@@ -38,7 +40,9 @@ export function relabelThreadLinks(
3840
title: (threadId: ThreadId) => string | undefined,
3941
): string {
4042
if (!hasThreadLinks(markdown)) return markdown;
41-
return markdown.replace(THREAD_LINK, (source, _label: string, href: string) => {
43+
return markdown.replace(THREAD_LINK_OUTSIDE_CODE, (source, ...args) => {
44+
const href = (args.at(-1) as { href?: string }).href;
45+
if (href === undefined) return source;
4246
const threadId = parseThreadLinkHref(href);
4347
const label = threadId === null ? undefined : title(threadId)?.trim();
4448
return threadId !== null && label ? formatThreadLink(threadId, label) : source;

0 commit comments

Comments
 (0)