Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(web): command-click spaced folder links (#6439)

Dara Adedejif34b9d31b3dd
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index c3e1c5288..33a8decb2 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -74,10 +74,12 @@ import {
} from "../markdown-clipboard";
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
import {
+ extractMarkdownLinkHrefs,
normalizeMarkdownLinkDestination,
resolveInlineCodeFileLinkMeta,
resolveMarkdownFileLinkMeta,
rewriteMarkdownFileUriHref,
+ shouldOpenMarkdownFileLinkInEditor,
type MarkdownFileLinkMeta,
} from "../markdown-links";
import { readLocalApi } from "../localApi";
@@ -825,7 +827,6 @@ interface MarkdownFileLinkProps {
className?: string | undefined;
}
-const MARKDOWN_LINK_HREF_PATTERN = /\[[^\]]*]\(([^)\s]+)(?:\s+["'][^"']*["'])?\)/g;
const MARKDOWN_FILE_LINK_CLASS_NAME =
"chat-markdown-file-link cursor-pointer transition-colors hover:bg-accent/70";
@@ -904,16 +905,6 @@ function extractInlineCodeSpans(text: string): string[] {
return spans;
}
-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 {
const normalizedHref = normalizeMarkdownLinkDestination(href);
return rewriteMarkdownFileUriHref(normalizedHref) ?? normalizedHref;
@@ -1307,6 +1298,10 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
+ if (shouldOpenMarkdownFileLinkInEditor(event)) {
+ handleOpenInEditor();
+ return;
+ }
if (onOpenInBrowser) {
handleOpenInBrowser();
return;
@@ -1629,7 +1624,10 @@ function ChatMarkdown({
},
a({ node, href, children, title: _title, ...props }) {
const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : "";
- const fileLinkMeta = normalizedHref ? markdownFileLinkMetaByHref.get(normalizedHref) : null;
+ const fileLinkMeta = normalizedHref
+ ? (markdownFileLinkMetaByHref.get(normalizedHref) ??
+ resolveMarkdownFileLinkMeta(normalizedHref, cwd))
+ : null;
if (!fileLinkMeta) {
const faviconHost = resolveExternalWebLinkHost(href);
const isSameDocumentLink = href?.startsWith("#") ?? false;
diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.ts
index 0beea8fea..af88a5e76 100644
--- a/apps/web/src/markdown-links.test.ts
+++ b/apps/web/src/markdown-links.test.ts
@@ -1,12 +1,74 @@
import { describe, expect, it } from "vite-plus/test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import ReactMarkdown from "react-markdown";
import {
+ extractMarkdownLinkHrefs,
resolveInlineCodeFileLinkMeta,
resolveMarkdownFileLinkMeta,
resolveMarkdownFileLinkTarget,
rewriteMarkdownFileUriHref,
+ shouldOpenMarkdownFileLinkInEditor,
} from "./markdown-links";
+function renderMarkdownLinkHref(markdown: string): string | undefined {
+ let renderedHref: string | undefined;
+ renderToStaticMarkup(
+ createElement(
+ ReactMarkdown,
+ {
+ components: {
+ a({ href }) {
+ renderedHref = href;
+ return createElement("a", { href });
+ },
+ },
+ },
+ markdown,
+ ),
+ );
+ return renderedHref;
+}
+
+describe("extractMarkdownLinkHrefs", () => {
+ it("extracts angle-bracketed paths containing spaces", () => {
+ expect(
+ extractMarkdownLinkHrefs(
+ "[Open the Bike Receipts folder](</Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts>)",
+ ),
+ ).toEqual(["/Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts"]);
+ });
+
+ it("preserves ordinary destinations and ignores link titles", () => {
+ expect(
+ extractMarkdownLinkHrefs(
+ '[source](apps/web/src/markdown-links.ts "implementation") and [docs](https://example.com)',
+ ),
+ ).toEqual(["apps/web/src/markdown-links.ts", "https://example.com"]);
+ });
+});
+
+describe("shouldOpenMarkdownFileLinkInEditor", () => {
+ it("uses command-click on macOS", () => {
+ expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "MacIntel")).toBe(
+ true,
+ );
+ expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "MacIntel")).toBe(
+ false,
+ );
+ });
+
+ it("uses control-click on other platforms", () => {
+ expect(
+ shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "Linux x86_64"),
+ ).toBe(true);
+ expect(
+ shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "Linux x86_64"),
+ ).toBe(false);
+ });
+});
+
describe("rewriteMarkdownFileUriHref", () => {
it("rewrites file uri hrefs into direct path hrefs", () => {
expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
@@ -88,6 +150,30 @@ describe("resolveMarkdownFileLinkTarget", () => {
});
});
+ it("resolves the encoded spaces emitted by the markdown renderer", () => {
+ expect(
+ resolveMarkdownFileLinkMeta(
+ "/Users/dara/Downloads/Lime%20Ride%20Artifacts/Bike%20Receipts",
+ "/Users/dara/Downloads/Lime Ride Artifacts",
+ ),
+ ).toMatchObject({
+ targetPath: "/Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts",
+ workspaceRelativePath: "Bike Receipts",
+ basename: "Bike Receipts",
+ });
+ });
+
+ it("resolves relative spaced folders from the markdown renderer", () => {
+ const href = renderMarkdownLinkHref("[folder](<docs/My Folder>)");
+
+ expect(href).toBe("docs/My%20Folder");
+ expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
+ targetPath: "/repo/project/docs/My Folder",
+ workspaceRelativePath: "docs/My Folder",
+ basename: "My Folder",
+ });
+ });
+
it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
expect(
resolveMarkdownFileLinkMeta(
diff --git a/apps/web/src/markdown-links.ts b/apps/web/src/markdown-links.ts
index e74bd1701..e1fb73eda 100644
--- a/apps/web/src/markdown-links.ts
+++ b/apps/web/src/markdown-links.ts
@@ -1,11 +1,16 @@
import { formatWorkspaceRelativePath } from "./filePathDisplay";
-import { resolvePathLinkTarget, splitPathAndPosition } from "./terminal-links";
+import {
+ isTerminalLinkActivation,
+ resolvePathLinkTarget,
+ splitPathAndPosition,
+} from "./terminal-links";
const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
-const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+(?::\d+){0,2}$/;
+const RELATIVE_FILE_PATH_PATTERN =
+ /^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2}$/;
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/;
@@ -37,6 +42,8 @@ const POSIX_FILE_ROOT_PREFIXES = [
"/workspace/",
"/workspaces/",
] as const;
+const MARKDOWN_LINK_HREF_PATTERN =
+ /\[[^\]]*]\(\s*(?:<([^>\n]+)>|([^\s)]+))(?:\s+["'][^"']*["'])?\s*\)/g;
export interface MarkdownFileLinkMeta {
filePath: string;
@@ -48,6 +55,22 @@ export interface MarkdownFileLinkMeta {
column?: number;
}
+export function extractMarkdownLinkHrefs(markdown: string): string[] {
+ const hrefs: string[] = [];
+ for (const match of markdown.matchAll(MARKDOWN_LINK_HREF_PATTERN)) {
+ const href = (match[1] ?? match[2])?.trim();
+ if (href) hrefs.push(href);
+ }
+ return hrefs;
+}
+
+export function shouldOpenMarkdownFileLinkInEditor(
+ event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
+ platform?: string,
+): boolean {
+ return isTerminalLinkActivation(event, platform);
+}
+
function safeDecode(value: string): string {
try {
return decodeURIComponent(value);