Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/src/markdown-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function resolveInlineCodeFileLinkMeta(
cwd?: string,
baseDir: string | undefined = cwd,
): MarkdownFileLinkMeta | null {
const candidate = inlineCodeFilePathCandidate(codeText);
const candidate = inlineCodeFilePathCandidate(codeText, cwd);
if (candidate === null) return null;

return resolveMarkdownFileLinkMeta(
Expand Down
19 changes: 19 additions & 0 deletions packages/shared/src/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,30 @@ describe("inlineCodeFilePathCandidate", () => {
["origin/main", null],
["z-ai/glm-5.3", null],
["share/man/ls.1", "share/man/ls.1"],
["usr/lib/libfoo.so.1", "usr/lib/libfoo.so.1"],
["vendor/jquery-3.6.0.min.js", "vendor/jquery-3.6.0.min.js"],
["./models/glm-5.3", "./models/glm-5.3"],
["/D:/Programme/t3code/file.ts", "/D:/Programme/t3code/file.ts"],
["/D%3A/Programme/other/file.ts", "/D%3A/Programme/other/file.ts"],
["/%74mp/image.png", "/%74mp/image.png"],
["/tmp/image.png", "/tmp/image.png"],
["/images/ZOOM/provider/te115_2.jpg", null],
["/api/v1/users.json", null],
["src/main.ts/", null],
["./patches/", "./patches/"],
])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
});

it("keeps absolute paths inside a workspace outside the usual roots", () => {
expect(inlineCodeFilePathCandidate("/code/app/src/index.ts", "/code/app")).toBe(
"/code/app/src/index.ts",
);
expect(inlineCodeFilePathCandidate("/images/logo.png", "/code/app")).toBeNull();
expect(inlineCodeFilePathCandidate("/code/my%20app/src/main.ts", "/code/my app")).toBe(
"/code/my%20app/src/main.ts",
);
});
});

describe("parseMarkdownFileLink", () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/src/markdownLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function looksLikeHostname(segment: string, hasPosition: boolean): boolean {
return !hasPosition && COUNTRY_HOSTNAME_TLDS.has(lastLabel);
}

export function inlineCodeFilePathCandidate(codeText: string): string | null {
export function inlineCodeFilePathCandidate(codeText: string, cwd?: string): string | null {
const trimmed = codeText.trim();
if (trimmed.length === 0 || INLINE_CODE_DISQUALIFIER_PATTERN.test(trimmed)) return null;

Expand All @@ -197,6 +197,16 @@ export function inlineCodeFilePathCandidate(codeText: string): string | null {
if (VERSION_SUFFIX_PATTERN.test(basename)) return null;
if (!hasPosition && !FILE_EXTENSION_PATTERN.test(basename)) return null;
}
// `/images/logo.jpg` in code is usually a URL path, not a file.
const decodedCandidate = safeDecodeURIComponent(candidate);
if (
candidate.startsWith("/") &&
!isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(decodedCandidate)) &&
!POSIX_FILE_ROOT_PREFIXES.some((prefix) => decodedCandidate.startsWith(prefix)) &&
workspaceRelativeFilePath(decodedCandidate, cwd) === null
) {
return null;
}
return candidate;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/threadFindText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ function markdownThreadFindText(
const tree = processor.runSync(processor.parse(markdown));
const paths = [
...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)),
...extractInlineCodeSpans(markdown).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),
...extractInlineCodeSpans(markdown).flatMap(
(span) => inlineCodeFilePathCandidate(span, cwd) ?? [],
),
].flatMap((href) => {
const target = resolveMarkdownFileLinkTarget(href, cwd);
return target ? [splitFilePathPosition(target).path] : [];
Expand Down Expand Up @@ -155,6 +157,7 @@ function markdownThreadFindText(
) {
candidate = inlineCodeFilePathCandidate(
(node.children ?? []).map((child) => child.value ?? "").join(""),
cwd,
);
}
const target = candidate ? resolveMarkdownFileLinkTarget(candidate, cwd) : null;
Expand Down
Loading