fix(web): command-click spaced folder links (#6439)
Co-authored-by: shivam <[email protected]>
This commit is contained in:
1 parent
ce91284f83
commit
f34b9d31b3
3 files changed
+121
-14
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user