Skip to content

Commit 0b57394

Browse files
committed
fix(markdown): preserve descriptive file-link labels
1 parent 061543e commit 0b57394

7 files changed

Lines changed: 134 additions & 21 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isMarkdownFileLinkLabel } from "@t3tools/client-runtime/markdown-links";
12
import type { MarkdownNode } from "react-native-nitro-markdown/headless";
23

34
import type { SelectableMarkdownSkill } from "./SelectableMarkdownText.types";
@@ -270,7 +271,7 @@ function appendChildren(
270271
return runs;
271272
}
272273

273-
function nodeTextContent(node: MarkdownNode): string {
274+
export function nodeTextContent(node: MarkdownNode): string {
274275
if (node.content !== undefined) {
275276
return node.content;
276277
}
@@ -312,7 +313,11 @@ function appendNode(
312313
case "link": {
313314
const presentation = resolveMarkdownLinkPresentation(node.href ?? "");
314315
if (presentation.kind === "file") {
315-
return appendRun(runs, presentation.label, {
316+
const descriptive = !isMarkdownFileLinkLabel(nodeTextContent(node), presentation.href);
317+
if (descriptive) {
318+
appendChildren(runs, node, { ...context, href: presentation.href });
319+
}
320+
return appendRun(runs, descriptive ? ` (${presentation.label})` : presentation.label, {
316321
...context,
317322
href: presentation.href,
318323
fileIcon: presentation.icon,

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isMarkdownFileLinkLabel } from "@t3tools/client-runtime/markdown-links";
2+
import { nodeTextContent } from "@t3tools/mobile-markdown-text/markdown";
13
import * as Haptics from "expo-haptics";
24
import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";
35
import { useViewabilityAmount, type LegendListRef } from "@legendapp/list/react-native";
@@ -1055,21 +1057,26 @@ function useMarkdownStyles(
10551057
preserveSoftBreaks: boolean,
10561058
highlightCode: boolean,
10571059
): CustomRenderers => ({
1058-
link: ({ children, href = "" }) => {
1060+
link: ({ node, children, href = "" }) => {
10591061
const presentation = resolveMarkdownLinkPresentation(href);
10601062
if (presentation.kind === "file") {
1063+
const descriptive = !isMarkdownFileLinkLabel(nodeTextContent(node), href);
10611064
return (
1062-
<NativeText
1063-
className="font-t3-bold"
1064-
onPress={() => onLinkPress(href)}
1065-
style={{ color: inlineTextColor }}
1066-
>
1067-
<Image
1068-
source={markdownFileIconSource(presentation.icon)}
1069-
style={markdownLinkStyles.inlineIcon}
1070-
/>
1071-
{presentation.label}
1072-
</NativeText>
1065+
<MarkdownLinkLabelContext.Provider value>
1066+
<NativeText
1067+
className="font-t3-bold"
1068+
onPress={() => onLinkPress(href)}
1069+
style={{ color: inlineTextColor }}
1070+
>
1071+
{descriptive ? <>{children} (</> : null}
1072+
<Image
1073+
source={markdownFileIconSource(presentation.icon)}
1074+
style={markdownLinkStyles.inlineIcon}
1075+
/>
1076+
{presentation.label}
1077+
{descriptive ? ")" : null}
1078+
</NativeText>
1079+
</MarkdownLinkLabelContext.Provider>
10731080
);
10741081
}
10751082
if (presentation.kind === "external") {

‎apps/mobile/src/lib/nativeMarkdownText.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,39 @@ describe("nativeMarkdownTextRuns", () => {
9292
externalHost: "example.com",
9393
},
9494
{ text: " " },
95+
{ text: "ignored label", href: "file:///repo/README.md#L12" },
9596
{
96-
text: "README.md:12",
97+
text: " (README.md:12)",
9798
href: "file:///repo/README.md#L12",
9899
fileIcon: "readme",
99100
},
100101
]);
101102
});
102103

104+
it("preserves descriptive file-link formatting and keeps filename links compact", () => {
105+
const href = "/repo/src/example.ts:12";
106+
expect(
107+
nativeMarkdownTextRuns({
108+
type: "paragraph",
109+
children: [
110+
{
111+
type: "link",
112+
href,
113+
children: [
114+
{ type: "bold", children: [{ type: "text", content: "validates" }] },
115+
{ type: "text", content: " the input" },
116+
],
117+
},
118+
{ type: "link", href, children: [{ type: "code_inline", content: "src/example.ts:12" }] },
119+
],
120+
}),
121+
).toEqual([
122+
{ text: "validates", bold: true, href },
123+
{ text: " the input", href },
124+
{ text: " (example.ts:12)example.ts:12", href, fileIcon: "typescript" },
125+
]);
126+
});
127+
103128
it("keeps hard breaks and collapses soft breaks", () => {
104129
const node: MarkdownNode = {
105130
type: "paragraph",

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ function codeButton(renderer: ReactTestRenderer, label: string) {
7272
return button.props as ComponentProps<typeof Button>;
7373
}
7474

75+
describe("ChatMarkdown file-link labels", () => {
76+
it("retains descriptive prose, emphasis, destinations, and copy text", async () => {
77+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
78+
let renderer: ReactTestRenderer | undefined;
79+
try {
80+
await act(async () => {
81+
renderer = create(
82+
<ChatMarkdown
83+
cwd="/repo"
84+
text="This function [**validates** the input](/repo/src/example.ts:12)."
85+
/>,
86+
);
87+
});
88+
expect(renderer!.root.findByType("strong").children).toEqual(["validates"]);
89+
const label = renderer!.root
90+
.findAllByType("span")
91+
.find((node) => node.children.includes(" the input"));
92+
expect(label).toBeDefined();
93+
expect(label!.props["data-markdown-copy"]).toBe(
94+
"[validates the input](/repo/src/example.ts:12)",
95+
);
96+
expect(
97+
renderer!.root
98+
.findAllByType("span")
99+
.some((node) => node.children.includes("example.ts · L12")),
100+
).toBe(true);
101+
} finally {
102+
await act(async () => renderer?.unmount());
103+
vi.unstubAllGlobals();
104+
}
105+
});
106+
});
107+
75108
describe("ChatMarkdown favicon privacy", () => {
76109
it("suppresses private link images while preserving public links across updates", async () => {
77110
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ import {
4343
classifyMarkdownImageSource,
4444
markdownImageSourceFragment,
4545
} from "@t3tools/client-runtime/markdown-images";
46-
import { inlineCodeFilePathCandidate } from "@t3tools/client-runtime/markdown-links";
46+
import {
47+
inlineCodeFilePathCandidate,
48+
isMarkdownFileLinkLabel,
49+
} from "@t3tools/client-runtime/markdown-links";
4750
import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
4851
import { mediaKindFromPath, mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview";
4952
import * as Cause from "effect/Cause";
@@ -2963,11 +2966,15 @@ const CHAT_MARKDOWN_COMPONENTS = {
29632966
);
29642967
}
29652968

2966-
return fileLinkChip(
2967-
fileLinkMeta,
2968-
`[${fileLinkMeta.basename}](${normalizedHref})`,
2969-
props.className,
2970-
normalizedHref,
2969+
const label = nodeToPlainText(children);
2970+
const copyMarkdown = `[${label || fileLinkMeta.basename}](${normalizedHref})`;
2971+
const chip = fileLinkChip(fileLinkMeta, copyMarkdown, props.className, normalizedHref);
2972+
return isMarkdownFileLinkLabel(label, normalizedHref) ? (
2973+
chip
2974+
) : (
2975+
<span data-markdown-copy={copyMarkdown}>
2976+
{children} ({chip})
2977+
</span>
29712978
);
29722979
},
29732980
code: function MarkdownCode({ node, children, className, ...props }) {

‎packages/client-runtime/src/markdownLinks.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
fileBasename,
5+
isMarkdownFileLinkLabel,
56
inlineCodeFilePathCandidate,
67
parseFileUrlHref,
78
parseMarkdownFileLink,
@@ -168,3 +169,27 @@ describe("workspaceRelativeFilePath", () => {
168169
expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
169170
});
170171
});
172+
173+
describe("isMarkdownFileLinkLabel", () => {
174+
it.each([
175+
"example.ts",
176+
"example.ts:12",
177+
"src/example.ts",
178+
"./src/example.ts:12",
179+
"/repo/src/example.ts",
180+
"file:///repo/src/example.ts#L12",
181+
"",
182+
])("keeps destination label %s compact", (label) =>
183+
expect(isMarkdownFileLinkLabel(label, "/repo/src/example.ts:12")).toBe(true),
184+
);
185+
it.each(["validates the input", "the example.ts", "other.ts", "src/other.ts"])(
186+
"preserves authored label %s",
187+
(label) => expect(isMarkdownFileLinkLabel(label, "/repo/src/example.ts:12")).toBe(false),
188+
);
189+
it("recognizes Windows paths and encoded destinations", () => {
190+
expect(isMarkdownFileLinkLabel("src\\example.ts:12", "C:\\repo\\src\\example.ts:12")).toBe(
191+
true,
192+
);
193+
expect(isMarkdownFileLinkLabel("my file.ts", "/repo/my%20file.ts#L12")).toBe(true);
194+
});
195+
});

‎packages/client-runtime/src/markdownLinks.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,14 @@ export function workspaceRelativeFilePath(
339339
if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
340340
return normalizedPath.slice(normalizedRoot.length + 1);
341341
}
342+
343+
export function isMarkdownFileLinkLabel(label: string, href: string): boolean {
344+
if (!label.trim()) return true;
345+
const target = parseMarkdownFileLink(href);
346+
if (!target) return false;
347+
const path = (parseMarkdownFileLink(label)?.path ?? splitFilePathPosition(label.trim()).path)
348+
.replaceAll("\\", "/")
349+
.replace(/^\.\//, "");
350+
const targetPath = target.path.replaceAll("\\", "/");
351+
return path === targetPath || targetPath.endsWith(`/${path}`);
352+
}

0 commit comments

Comments
 (0)