Improve markdown file link UX (#1956)

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
legsandJulius Marminge authored and GitHub committed 2026-04-15 14:20:28 -07:00
1 parent 28cb9db20b
commit 68061af042
10 files changed
+786 -35

No files matched your search

@@ -63,7 +63,7 @@ describe("ChatMarkdown", () => {
);
try {
const link = page.getByRole("link", { name: "PermissionRule.ts:1" });
const link = page.getByRole("link", { name: "PermissionRule.ts · L1" });
await expect.element(link).toBeInTheDocument();
await expect.element(link).toHaveAttribute("href", `${filePath}#L1`);
@@ -76,4 +76,66 @@ describe("ChatMarkdown", () => {
await screen.unmount();
}
});
it("shows column information inline when present", async () => {
const filePath =
"/Users/yashsingh/p/sco/claude-code-extract/src/utils/permissions/PermissionRule.ts";
const screen = await render(
<ChatMarkdown text={`[PermissionRule.ts](file://${filePath}#L1C7)`} cwd="/repo/project" />,
);
try {
const link = page.getByRole("link", { name: "PermissionRule.ts · L1:C7" });
await expect.element(link).toBeInTheDocument();
await expect.element(link).toHaveAttribute("href", `${filePath}#L1C7`);
await link.click();
await vi.waitFor(() => {
expect(openInPreferredEditorMock).toHaveBeenCalledWith(
expect.anything(),
`${filePath}:1:7`,
);
});
} finally {
await screen.unmount();
}
});
it("disambiguates duplicate file basenames inline", async () => {
const firstPath = "/Users/yashsingh/p/t3code/apps/web/src/components/chat/MessagesTimeline.tsx";
const secondPath = "/Users/yashsingh/p/t3code/apps/web/src/components/MessagesTimeline.tsx";
const screen = await render(
<ChatMarkdown
text={`See [MessagesTimeline.tsx](file://${firstPath}) and [MessagesTimeline.tsx](file://${secondPath}).`}
cwd="/repo/project"
/>,
);
try {
await expect
.element(page.getByRole("link", { name: "MessagesTimeline.tsx · components/chat" }))
.toBeInTheDocument();
await expect
.element(page.getByRole("link", { name: "MessagesTimeline.tsx · src/components" }))
.toBeInTheDocument();
} finally {
await screen.unmount();
}
});
it("keeps normal web links unchanged", async () => {
const screen = await render(
<ChatMarkdown text="[OpenAI](https://openai.com/docs)" cwd="/repo/project" />,
);
try {
const link = page.getByRole("link", { name: "OpenAI" });
await expect.element(link).toBeInTheDocument();
await expect.element(link).toHaveAttribute("href", "https://openai.com/docs");
await expect.element(link).toHaveAttribute("target", "_blank");
} finally {
await screen.unmount();
}
});
});
+283 -17
View File
@@ -3,6 +3,7 @@ import { CheckIcon, CopyIcon } from "lucide-react";
import React, {
Children,
Suspense,
type MouseEvent as ReactMouseEvent,
isValidElement,
use,
useCallback,
@@ -17,13 +18,17 @@ import type { Components } from "react-markdown";
import ReactMarkdown from "react-markdown";
import { defaultUrlTransform } from "react-markdown";
import remarkGfm from "remark-gfm";
import { VscodeEntryIcon } from "./chat/VscodeEntryIcon";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { toastManager } from "./ui/toast";
import { openInPreferredEditor } from "../editorPreferences";
import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering";
import { fnv1a32 } from "../lib/diffRendering";
import { LRUCache } from "../lib/lruCache";
import { useTheme } from "../hooks/useTheme";
import { resolveMarkdownFileLinkTarget, rewriteMarkdownFileUriHref } from "../markdown-links";
import { resolveMarkdownFileLinkMeta, rewriteMarkdownFileUriHref } from "../markdown-links";
import { readLocalApi } from "../localApi";
import { cn } from "../lib/utils";
class CodeHighlightErrorBoundary extends React.Component<
{ fallback: ReactNode; children: ReactNode },
@@ -236,34 +241,289 @@ function SuspenseShikiCodeBlock({
);
}
interface MarkdownFileLinkProps {
href: string;
targetPath: string;
displayPath: string;
filePath: string;
label: string;
theme: "light" | "dark";
className?: string | undefined;
}
const MARKDOWN_LINK_HREF_PATTERN = /\[[^\]]*]\(([^)\s]+)(?:\s+["'][^"']*["'])?\)/g;
const MARKDOWN_FILE_LINK_CLASS_NAME =
"chat-markdown-file-link relative top-[2px] max-w-full no-underline";
const MARKDOWN_FILE_LINK_ICON_CLASS_NAME = "chat-markdown-file-link-icon size-3.5 shrink-0";
const MARKDOWN_FILE_LINK_LABEL_CLASS_NAME = "chat-markdown-file-link-label truncate";
function pathParentSegments(path: string): string[] {
const normalized = path.replaceAll("\\", "/");
const segments = normalized.split("/").filter((segment) => segment.length > 0);
return segments.slice(0, -1);
}
function buildFileLinkParentSuffixByPath(filePaths: ReadonlyArray<string>): Map<string, string> {
const groups = new Map<string, Set<string>>();
for (const filePath of filePaths) {
const pathSegments = filePath
.replaceAll("\\", "/")
.split("/")
.filter((segment) => segment.length > 0);
const basename = pathSegments[pathSegments.length - 1];
if (!basename) continue;
const group = groups.get(basename) ?? new Set<string>();
group.add(filePath);
groups.set(basename, group);
}
const suffixByPath = new Map<string, string>();
for (const group of groups.values()) {
const uniquePaths = [...group];
if (uniquePaths.length < 2) continue;
const parentSegmentsByPath = new Map(
uniquePaths.map((filePath) => [filePath, pathParentSegments(filePath)]),
);
const minUniqueDepthByPath = new Map<string, number>();
for (const filePath of uniquePaths) {
const segments = parentSegmentsByPath.get(filePath) ?? [];
let resolvedDepth = segments.length;
for (let depth = 1; depth <= segments.length; depth += 1) {
const candidate = segments.slice(-depth).join("/");
const collision = uniquePaths.some((otherPath) => {
if (otherPath === filePath) return false;
const otherSegments = parentSegmentsByPath.get(otherPath) ?? [];
return otherSegments.slice(-depth).join("/") === candidate;
});
if (!collision) {
resolvedDepth = depth;
break;
}
}
minUniqueDepthByPath.set(filePath, resolvedDepth);
}
for (const filePath of uniquePaths) {
const segments = parentSegmentsByPath.get(filePath) ?? [];
if (segments.length === 0) continue;
const minUniqueDepth = minUniqueDepthByPath.get(filePath) ?? 1;
const suffixDepth = Math.min(segments.length, Math.max(minUniqueDepth, 2));
suffixByPath.set(filePath, segments.slice(-suffixDepth).join("/"));
}
}
return suffixByPath;
}
function extractMarkdownLinkHrefs(text: string): string[] {
const hrefs: string[] = [];
for (const match of text.matchAll(MARKDOWN_LINK_HREF_PATTERN)) {
const href = match[1]?.trim();
if (!href) continue;
hrefs.push(href);
}
return hrefs;
}
function normalizeMarkdownLinkHrefKey(href: string): string {
return rewriteMarkdownFileUriHref(href.trim()) ?? href.trim();
}
const MarkdownFileLink = memo(function MarkdownFileLink({
href,
targetPath,
displayPath,
filePath,
label,
theme,
className,
}: MarkdownFileLinkProps) {
const handleOpen = useCallback(() => {
const api = readLocalApi();
if (!api) {
toastManager.add({
type: "error",
title: "Open in editor is unavailable",
});
return;
}
void openInPreferredEditor(api, targetPath).catch((error) => {
toastManager.add({
type: "error",
title: "Unable to open file",
description: error instanceof Error ? error.message : "An error occurred.",
});
});
}, [targetPath]);
const handleCopy = useCallback((value: string, title: string) => {
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
toastManager.add({
type: "error",
title: `Failed to copy ${title.toLowerCase()}`,
description: "Clipboard API unavailable.",
});
return;
}
void navigator.clipboard.writeText(value).then(
() => {
toastManager.add({
type: "success",
title: `${title} copied`,
description: value,
});
},
(error) => {
toastManager.add({
type: "error",
title: `Failed to copy ${title.toLowerCase()}`,
description: error instanceof Error ? error.message : "An error occurred.",
});
},
);
}, []);
const handleContextMenu = useCallback(
async (event: ReactMouseEvent<HTMLAnchorElement>) => {
event.preventDefault();
event.stopPropagation();
const api = readLocalApi();
if (!api) return;
const clicked = await api.contextMenu.show(
[
{ id: "open", label: "Open in editor" },
{ id: "copy-relative", label: "Copy relative path" },
{ id: "copy-full", label: "Copy full path" },
] as const,
{ x: event.clientX, y: event.clientY },
);
if (clicked === "open") {
handleOpen();
return;
}
if (clicked === "copy-relative") {
handleCopy(displayPath, "Relative path");
return;
}
if (clicked === "copy-full") {
handleCopy(targetPath, "Full path");
}
},
[displayPath, handleCopy, handleOpen, targetPath],
);
return (
<Tooltip>
<TooltipTrigger
render={
<a
href={href}
className={cn(MARKDOWN_FILE_LINK_CLASS_NAME, className)}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
handleOpen();
}}
onContextMenu={handleContextMenu}
>
<VscodeEntryIcon
pathValue={filePath}
kind="file"
theme={theme}
className={cn(MARKDOWN_FILE_LINK_ICON_CLASS_NAME, "text-current")}
/>
<span className={MARKDOWN_FILE_LINK_LABEL_CLASS_NAME}>{label}</span>
</a>
}
/>
<TooltipPopup
side="top"
className="max-w-[min(40rem,calc(100vw-2rem))] font-mono text-[11px] leading-tight"
>
<div className="markdown-file-link-tooltip-scroll overflow-x-auto whitespace-nowrap">
{displayPath}
</div>
</TooltipPopup>
</Tooltip>
);
}, areMarkdownFileLinkPropsEqual);
function areMarkdownFileLinkPropsEqual(
previous: Readonly<MarkdownFileLinkProps>,
next: Readonly<MarkdownFileLinkProps>,
): boolean {
return (
previous.href === next.href &&
previous.targetPath === next.targetPath &&
previous.displayPath === next.displayPath &&
previous.filePath === next.filePath &&
previous.label === next.label &&
previous.theme === next.theme &&
previous.className === next.className
);
}
function ChatMarkdown({ text, cwd, isStreaming = false }: ChatMarkdownProps) {
const { resolvedTheme } = useTheme();
const diffThemeName = resolveDiffThemeName(resolvedTheme);
const markdownFileLinkMetaByHref = useMemo(() => {
const metaByHref = new Map<
string,
NonNullable<ReturnType<typeof resolveMarkdownFileLinkMeta>>
>();
for (const href of extractMarkdownLinkHrefs(text)) {
const normalizedHref = normalizeMarkdownLinkHrefKey(href);
if (metaByHref.has(normalizedHref)) continue;
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd);
if (meta) {
metaByHref.set(normalizedHref, meta);
}
}
return metaByHref;
}, [cwd, text]);
const fileLinkParentSuffixByPath = useMemo(() => {
const filePaths = [...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath);
return buildFileLinkParentSuffixByPath(filePaths);
}, [markdownFileLinkMetaByHref]);
const markdownUrlTransform = useCallback((href: string) => {
return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);
}, []);
const markdownComponents = useMemo<Components>(
() => ({
a({ node: _node, href, ...props }) {
const targetPath = resolveMarkdownFileLinkTarget(href, cwd);
if (!targetPath) {
const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : "";
const fileLinkMeta = normalizedHref ? markdownFileLinkMetaByHref.get(normalizedHref) : null;
if (!fileLinkMeta) {
return <a {...props} href={href} target="_blank" rel="noopener noreferrer" />;
}
const parentSuffix = fileLinkParentSuffixByPath.get(fileLinkMeta.filePath);
const labelParts = [fileLinkMeta.basename];
if (typeof parentSuffix === "string" && parentSuffix.length > 0) {
labelParts.push(parentSuffix);
}
if (fileLinkMeta.line) {
labelParts.push(
`L${fileLinkMeta.line}${fileLinkMeta.column ? `:C${fileLinkMeta.column}` : ""}`,
);
}
return (
<a
{...props}
href={href}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
const api = readLocalApi();
if (api) {
void openInPreferredEditor(api, targetPath);
} else {
console.warn("Native API not found. Unable to open file in editor.");
}
}}
<MarkdownFileLink
href={href ?? fileLinkMeta.targetPath}
targetPath={fileLinkMeta.targetPath}
displayPath={fileLinkMeta.displayPath}
filePath={fileLinkMeta.filePath}
label={labelParts.join(" · ")}
theme={resolvedTheme}
className={props.className}
/>
);
},
@@ -289,7 +549,13 @@ function ChatMarkdown({ text, cwd, isStreaming = false }: ChatMarkdownProps) {
);
},
}),
[cwd, diffThemeName, isStreaming],
[
diffThemeName,
fileLinkParentSuffixByPath,
isStreaming,
markdownFileLinkMetaByHref,
resolvedTheme,
],
);
return (
@@ -0,0 +1,189 @@
import { EnvironmentId, MessageId } from "@t3tools/contracts";
import { createRef } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeAll, describe, expect, it, vi } from "vitest";
import type { LegendListRef } from "@legendapp/list/react";
vi.mock("@legendapp/list/react", async () => {
const React = await import("react");
const LegendList = React.forwardRef(function MockLegendList(
props: {
data: Array<{ id: string }>;
keyExtractor: (item: { id: string }) => string;
renderItem: (args: { item: { id: string } }) => React.ReactNode;
ListHeaderComponent?: React.ReactNode;
ListFooterComponent?: React.ReactNode;
},
_ref: React.ForwardedRef<LegendListRef>,
) {
return (
<div data-testid="legend-list">
{props.ListHeaderComponent}
{props.data.map((item) => (
<div key={props.keyExtractor(item)}>{props.renderItem({ item })}</div>
))}
{props.ListFooterComponent}
</div>
);
});
return { LegendList };
});
function matchMedia() {
return {
matches: false,
addEventListener: () => {},
removeEventListener: () => {},
};
}
beforeAll(() => {
const classList = {
add: () => {},
remove: () => {},
toggle: () => {},
contains: () => false,
};
vi.stubGlobal("localStorage", {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
clear: () => {},
});
vi.stubGlobal("window", {
matchMedia,
addEventListener: () => {},
removeEventListener: () => {},
requestAnimationFrame: (callback: FrameRequestCallback) => {
callback(0);
return 0;
},
cancelAnimationFrame: () => {},
desktopBridge: undefined,
});
vi.stubGlobal("document", {
documentElement: {
classList,
offsetHeight: 0,
},
});
});
const ACTIVE_THREAD_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
function buildProps() {
return {
isWorking: false,
activeTurnInProgress: false,
activeTurnId: null,
activeTurnStartedAt: null,
listRef: createRef<LegendListRef | null>(),
completionDividerBeforeEntryId: null,
completionSummary: null,
turnDiffSummaryByAssistantMessageId: new Map(),
routeThreadKey: "environment-local:thread-1",
onOpenTurnDiff: () => {},
revertTurnCountByUserMessageId: new Map(),
onRevertUserMessage: () => {},
isRevertingCheckpoint: false,
onImageExpand: () => {},
activeThreadEnvironmentId: ACTIVE_THREAD_ENVIRONMENT_ID,
markdownCwd: undefined,
resolvedTheme: "light" as const,
timestampFormat: "locale" as const,
workspaceRoot: undefined,
onIsAtEndChange: () => {},
};
}
describe("MessagesTimeline", () => {
it("renders inline terminal labels with the composer chip UI", async () => {
const { MessagesTimeline } = await import("./MessagesTimeline");
const markup = renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
timelineEntries={[
{
id: "entry-1",
kind: "message",
createdAt: "2026-03-17T19:12:28.000Z",
message: {
id: MessageId.make("message-2"),
role: "user",
text: [
"yoo what's @terminal-1:1-5 mean",
"",
"<terminal_context>",
"- Terminal 1 lines 1-5:",
" 1 | julius@mac effect-http-ws-cli % bun i",
" 2 | bun install v1.3.9 (cf6cdbbb)",
"</terminal_context>",
].join("\n"),
createdAt: "2026-03-17T19:12:28.000Z",
streaming: false,
},
},
]}
/>,
);
expect(markup).toContain("Terminal 1 lines 1-5");
expect(markup).toContain("lucide-terminal");
expect(markup).toContain("yoo what&#x27;s ");
}, 20_000);
it("renders context compaction entries in the normal work log", async () => {
const { MessagesTimeline } = await import("./MessagesTimeline");
const markup = renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
timelineEntries={[
{
id: "entry-1",
kind: "work",
createdAt: "2026-03-17T19:12:28.000Z",
entry: {
id: "work-1",
createdAt: "2026-03-17T19:12:28.000Z",
label: "Context compacted",
tone: "info",
},
},
]}
/>,
);
expect(markup).toContain("Context compacted");
expect(markup).toContain("Work log");
});
it("formats changed file paths from the workspace root", async () => {
const { MessagesTimeline } = await import("./MessagesTimeline");
const markup = renderToStaticMarkup(
<MessagesTimeline
{...buildProps()}
timelineEntries={[
{
id: "entry-1",
kind: "work",
createdAt: "2026-03-17T19:12:28.000Z",
entry: {
id: "work-1",
createdAt: "2026-03-17T19:12:28.000Z",
label: "Updated files",
tone: "tool",
changedFiles: ["C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts"],
},
},
]}
workspaceRoot="C:/Users/mike/dev-stuff/t3code"
/>,
);
expect(markup).toContain("t3code/apps/web/src/session-logic.ts");
expect(markup).not.toContain("C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts");
});
});
@@ -60,6 +60,7 @@ import {
formatInlineTerminalContextLabel,
textContainsInlineTerminalContextLabels,
} from "./userMessageTerminalContexts";
import { formatWorkspaceRelativePath } from "../../filePathDisplay";
// ---------------------------------------------------------------------------
// Context — shared state consumed by every row component via useContext.
@@ -528,6 +529,7 @@ const WorkGroupSection = memo(function WorkGroupSection({
}: {
groupedEntries: Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"];
}) {
const { workspaceRoot } = use(TimelineRowCtx);
const [isExpanded, setIsExpanded] = useState(false);
const hasOverflow = groupedEntries.length > MAX_VISIBLE_WORK_LOG_ENTRIES;
const visibleEntries =
@@ -559,7 +561,11 @@ const WorkGroupSection = memo(function WorkGroupSection({
)}
<div className="space-y-0.5">
{visibleEntries.map((workEntry) => (
<SimpleWorkEntryRow key={`work-row:${workEntry.id}`} workEntry={workEntry} />
<SimpleWorkEntryRow
key={`work-row:${workEntry.id}`}
workEntry={workEntry}
workspaceRoot={workspaceRoot}
/>
))}
</div>
</div>
@@ -861,15 +867,17 @@ function workToneClass(tone: "thinking" | "tool" | "info" | "error"): string {
function workEntryPreview(
workEntry: Pick<TimelineWorkEntry, "detail" | "command" | "changedFiles">,
workspaceRoot: string | undefined,
) {
if (workEntry.command) return workEntry.command;
if (workEntry.detail) return workEntry.detail;
if ((workEntry.changedFiles?.length ?? 0) === 0) return null;
const [firstPath] = workEntry.changedFiles ?? [];
if (!firstPath) return null;
const displayPath = formatWorkspaceRelativePath(firstPath, workspaceRoot);
return workEntry.changedFiles!.length === 1
? firstPath
: `${firstPath} +${workEntry.changedFiles!.length - 1} more`;
? displayPath
: `${displayPath} +${workEntry.changedFiles!.length - 1} more`;
}
function workEntryRawCommand(
@@ -924,12 +932,13 @@ function toolWorkEntryHeading(workEntry: TimelineWorkEntry): string {
const SimpleWorkEntryRow = memo(function SimpleWorkEntryRow(props: {
workEntry: TimelineWorkEntry;
workspaceRoot: string | undefined;
}) {
const { workEntry } = props;
const { workEntry, workspaceRoot } = props;
const iconConfig = workToneIcon(workEntry.tone);
const EntryIcon = workEntryIcon(workEntry);
const heading = toolWorkEntryHeading(workEntry);
const preview = workEntryPreview(workEntry);
const preview = workEntryPreview(workEntry, workspaceRoot);
const rawCommand = workEntryRawCommand(workEntry);
const displayText = preview ? `${heading} - ${preview}` : heading;
const hasChangedFiles = (workEntry.changedFiles?.length ?? 0) > 0;
@@ -988,15 +997,18 @@ const SimpleWorkEntryRow = memo(function SimpleWorkEntryRow(props: {
</div>
{hasChangedFiles && !previewIsChangedFiles && (
<div className="mt-1 flex flex-wrap gap-1 pl-6">
{workEntry.changedFiles?.slice(0, 4).map((filePath) => (
<span
key={`${workEntry.id}:${filePath}`}
className="rounded-md border border-border/55 bg-background/75 px-1.5 py-0.5 font-mono text-[10px] text-muted-foreground/75"
title={filePath}
>
{filePath}
</span>
))}
{workEntry.changedFiles?.slice(0, 4).map((filePath) => {
const displayPath = formatWorkspaceRelativePath(filePath, workspaceRoot);
return (
<span
key={`${workEntry.id}:${filePath}`}
className="rounded-md border border-border/55 bg-background/75 px-1.5 py-0.5 font-mono text-[10px] text-muted-foreground/75"
title={displayPath}
>
{displayPath}
</span>
);
})}
{(workEntry.changedFiles?.length ?? 0) > 4 && (
<span className="px-1 text-[10px] text-muted-foreground/55">
+{(workEntry.changedFiles?.length ?? 0) - 4}
+41
View File
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { formatWorkspaceRelativePath } from "./filePathDisplay";
describe("formatWorkspaceRelativePath", () => {
it("formats absolute workspace paths from the workspace root", () => {
expect(
formatWorkspaceRelativePath(
"C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts:501",
"C:/Users/mike/dev-stuff/t3code",
),
).toBe("t3code/apps/web/src/session-logic.ts:501");
});
it("prefixes relative paths with the workspace root label", () => {
expect(
formatWorkspaceRelativePath(
"apps/web/src/session-logic.ts:501",
"C:/Users/mike/dev-stuff/t3code",
),
).toBe("t3code/apps/web/src/session-logic.ts:501");
});
it("keeps paths already rooted at the workspace label stable", () => {
expect(
formatWorkspaceRelativePath(
"t3code/apps/web/src/session-logic.ts:501",
"C:/Users/mike/dev-stuff/t3code",
),
).toBe("t3code/apps/web/src/session-logic.ts:501");
});
it("preserves columns when present", () => {
expect(
formatWorkspaceRelativePath(
"/C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts:501:9",
"C:/Users/mike/dev-stuff/t3code",
),
).toBe("t3code/apps/web/src/session-logic.ts:501:9");
});
});
+57
View File
@@ -0,0 +1,57 @@
import { splitPathAndPosition } from "./terminal-links";
function normalizePathSeparators(path: string): string {
return path.replaceAll("\\", "/");
}
function canonicalizeWindowsDrivePath(path: string): string {
return /^\/[A-Za-z]:\//.test(path) ? path.slice(1) : path;
}
function trimTrailingPathSeparators(path: string): string {
return path.replace(/[\\/]+$/, "");
}
function basenameOfPath(path: string): string {
const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
}
function stripRelativePrefixes(path: string): string {
return path.replace(/^\.\/+/, "").replace(/^\/+/, "");
}
export function formatWorkspaceRelativePath(
pathWithPosition: string,
workspaceRoot: string | undefined,
): string {
const { path, line, column } = splitPathAndPosition(pathWithPosition);
const normalizedPath = canonicalizeWindowsDrivePath(normalizePathSeparators(path));
let displayPath = normalizedPath;
if (workspaceRoot) {
const normalizedWorkspaceRoot = canonicalizeWindowsDrivePath(
normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
);
const workspaceLabel = basenameOfPath(normalizedWorkspaceRoot);
const pathForCompare = normalizedPath.toLowerCase();
const workspaceForCompare = normalizedWorkspaceRoot.toLowerCase();
const workspaceWithSeparator = `${workspaceForCompare}/`;
const workspaceLabelWithSeparator = `${workspaceLabel.toLowerCase()}/`;
if (pathForCompare === workspaceForCompare) {
displayPath = workspaceLabel;
} else if (pathForCompare.startsWith(workspaceWithSeparator)) {
const relativeSuffix = normalizedPath.slice(normalizedWorkspaceRoot.length + 1);
displayPath = `${workspaceLabel}/${relativeSuffix}`;
} else if (!normalizedPath.startsWith("/")) {
const relativePath = stripRelativePrefixes(normalizedPath);
displayPath = pathForCompare.startsWith(workspaceLabelWithSeparator)
? normalizedPath
: `${workspaceLabel}/${relativePath}`;
}
}
if (!line) return displayPath;
return `${displayPath}:${line}${column ? `:${column}` : ""}`;
}
+59
View File
@@ -324,6 +324,47 @@ label:has(> select#reasoning-effort) select {
font-size: 0.75rem;
}
.chat-markdown-file-link {
display: inline-flex;
align-items: center;
gap: 0.28rem;
border: 1px solid color-mix(in srgb, var(--border) 92%, transparent);
border-radius: 0.375rem;
background: color-mix(in srgb, var(--muted) 88%, var(--background));
padding: 0.08rem 0.34rem;
color: var(--foreground);
font-family: var(--font-mono, ui-monospace, monospace);
font-size: 0.75rem;
line-height: 1.15;
vertical-align: text-bottom;
transition:
background-color 120ms ease,
border-color 120ms ease,
color 120ms ease,
opacity 120ms ease;
}
.chat-markdown-file-link:hover {
opacity: 1;
color: var(--foreground);
border-color: color-mix(in srgb, var(--border) 65%, var(--foreground));
background: color-mix(in srgb, var(--muted) 72%, var(--background));
}
.chat-markdown-file-link:focus-visible {
outline: none;
box-shadow: 0 0 0 1px color-mix(in srgb, var(--ring) 70%, transparent);
}
.chat-markdown-file-link-icon {
opacity: 0.72;
}
.chat-markdown-file-link-label {
color: color-mix(in srgb, var(--foreground) 88%, transparent);
line-height: 1.1;
}
.chat-markdown pre {
max-width: 100%;
overflow-x: auto;
@@ -340,6 +381,24 @@ label:has(> select#reasoning-effort) select {
font-size: 0.75rem;
}
.markdown-file-link-tooltip-scroll {
scrollbar-width: thin;
scrollbar-color: color-mix(in srgb, var(--border) 78%, transparent) transparent;
}
.markdown-file-link-tooltip-scroll::-webkit-scrollbar {
height: 6px;
}
.markdown-file-link-tooltip-scroll::-webkit-scrollbar-track {
background: transparent;
}
.markdown-file-link-tooltip-scroll::-webkit-scrollbar-thumb {
border-radius: 999px;
background: color-mix(in srgb, var(--border) 78%, transparent);
}
.chat-markdown .chat-markdown-codeblock {
--chat-markdown-codeblock-copy-button-space: 1.5rem;
position: relative;
+28 -1
View File
@@ -1,6 +1,10 @@
import { describe, expect, it } from "vitest";
import { resolveMarkdownFileLinkTarget, rewriteMarkdownFileUriHref } from "./markdown-links";
import {
resolveMarkdownFileLinkMeta,
resolveMarkdownFileLinkTarget,
rewriteMarkdownFileUriHref,
} from "./markdown-links";
describe("rewriteMarkdownFileUriHref", () => {
it("rewrites file uri hrefs into direct path hrefs", () => {
@@ -57,6 +61,29 @@ describe("resolveMarkdownFileLinkTarget", () => {
);
});
it("formats tooltip display paths relative to the cwd when possible", () => {
expect(
resolveMarkdownFileLinkMeta(
"file:///C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts#L501",
"C:/Users/mike/dev-stuff/t3code",
),
).toMatchObject({
displayPath: "t3code/apps/web/src/session-logic.ts:501",
});
});
it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
expect(
resolveMarkdownFileLinkMeta(
"/C:/Users/mike/dev-stuff/t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
"C:/Users/mike/dev-stuff/t3code",
),
).toMatchObject({
displayPath:
"t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
});
});
it("does not treat app routes as file links", () => {
expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
});
+39 -1
View File
@@ -1,4 +1,5 @@
import { resolvePathLinkTarget } from "./terminal-links";
import { formatWorkspaceRelativePath } from "./filePathDisplay";
import { resolvePathLinkTarget, splitPathAndPosition } from "./terminal-links";
const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
@@ -21,6 +22,15 @@ const POSIX_FILE_ROOT_PREFIXES = [
"/root/",
] as const;
export interface MarkdownFileLinkMeta {
filePath: string;
targetPath: string;
displayPath: string;
basename: string;
line?: number;
column?: number;
}
function safeDecode(value: string): string {
try {
return decodeURIComponent(value);
@@ -143,3 +153,31 @@ export function resolveMarkdownFileLinkTarget(
if (!cwd) return null;
return resolvePathLinkTarget(pathWithPosition, cwd);
}
function basenameOfPath(path: string): string {
const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
}
export function resolveMarkdownFileLinkMeta(
href: string | undefined,
cwd?: string,
): MarkdownFileLinkMeta | null {
const targetPath = resolveMarkdownFileLinkTarget(href, cwd);
if (!targetPath) return null;
const { path, line, column } = splitPathAndPosition(targetPath);
const parsedLine = line ? Number.parseInt(line, 10) : Number.NaN;
const parsedColumn = column ? Number.parseInt(column, 10) : Number.NaN;
const lineNumber = Number.isFinite(parsedLine) ? parsedLine : undefined;
const columnNumber = Number.isFinite(parsedColumn) ? parsedColumn : undefined;
return {
filePath: path,
targetPath,
displayPath: formatWorkspaceRelativePath(targetPath, cwd),
basename: basenameOfPath(path),
...(lineNumber !== undefined ? { line: lineNumber } : {}),
...(columnNumber !== undefined ? { column: columnNumber } : {}),
};
}
+1 -1
View File
@@ -137,7 +137,7 @@ function inferHomeFromCwd(cwd: string): string | undefined {
return undefined;
}
function splitPathAndPosition(value: string): {
export function splitPathAndPosition(value: string): {
path: string;
line: string | undefined;
column: string | undefined;