Skip to content

Commit 9ac0dde

Browse files
committed
fix(shared): recognize relative markdown folder links with a trailing slash
RELATIVE_FILE_PATH_PATTERN required a segment after the last slash, so an explicit link such as patches/ was not a file link and the click did nothing. The final segment is now optional. Inline code still ignores a trailing slash unless the path is explicitly relative or absolute.
1 parent e287582 commit 9ac0dde

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎packages/shared/src/markdownLinks.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ describe("isMarkdownFileLinkLabel", () => {
2929
["./src/example.ts", "/repo/src/example.ts", true],
3030
["/repo/src/example.ts", "/repo/src/example.ts", true],
3131
["src/", "/home/me/project/src/", true],
32+
["patches/", "patches/", true],
33+
["patches", "patches/", true],
3234
["EXAMPLE.TS", "C:/repo/src/example.ts:12", true],
3335
["file name.ts", "file:///repo/file%20name.ts", true],
3436
["", "/repo/src/example.ts", true],
@@ -59,6 +61,11 @@ describe("inlineCodeFilePathCandidate", () => {
5961
["usr/lib/libfoo.so.1", "usr/lib/libfoo.so.1"],
6062
["vendor/jquery-3.6.0.min.js", "vendor/jquery-3.6.0.min.js"],
6163
["./models/glm-5.3", "./models/glm-5.3"],
64+
["patches/", null],
65+
["apps/web/src/", null],
66+
["conf.d/", null],
67+
["src/main.ts/", null],
68+
["./patches/", "./patches/"],
6269
])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
6370
expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
6471
});
@@ -127,6 +134,11 @@ describe("parseMarkdownFileLink", () => {
127134
["AGENTS.md", "AGENTS.md"],
128135
["script.ts:10", "script.ts"],
129136
["/tmp/clip%23one.mp4#t=2", "/tmp/clip#one.mp4"],
137+
["patches/", "patches/"],
138+
["./patches/", "./patches/"],
139+
["apps/web/src/", "apps/web/src/"],
140+
["docs/My Folder/", "docs/My Folder/"],
141+
["docs/My%20Folder/", "docs/My Folder/"],
130142
])("recognizes %s as a file", (href, path) => {
131143
expect(parseMarkdownFileLink(href)?.path).toBe(path);
132144
});
@@ -142,6 +154,7 @@ describe("parseMarkdownFileLink", () => {
142154
"/chat/settings#L3",
143155
"/app#L1",
144156
"readme",
157+
"patches",
145158
"TODO:12",
146159
])("does not treat %s as a file", (href) => {
147160
expect(parseMarkdownFileLink(href)).toBeNull();
@@ -269,6 +282,22 @@ describe("resolveMarkdownFileLinkTarget", () => {
269282
);
270283
});
271284

285+
it("resolves a relative folder with a trailing slash against cwd", () => {
286+
expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
287+
"/repo/project/patches/",
288+
);
289+
expect(resolveMarkdownFileLinkTarget("./patches/", "/repo/project")).toBe(
290+
"/repo/project/./patches/",
291+
);
292+
expect(resolveMarkdownFileLinkTarget("docs/My Folder/", "/repo/project")).toBe(
293+
"/repo/project/docs/My Folder/",
294+
);
295+
expect(resolveMarkdownFileLinkTarget("apps/web/src/", "/repo/project")).toBe(
296+
"/repo/project/apps/web/src/",
297+
);
298+
expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
299+
});
300+
272301
it("does not treat filename line references as external schemes", () => {
273302
expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
274303
"/Users/julius/project/script.ts:10",

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99

1010
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
1111

12+
// The final segment is optional so an explicit folder link may end in `/`
13+
// (`patches/`, `docs/My Folder/`). A bare name with no slash still fails.
1214
const RELATIVE_FILE_PATH_PATTERN =
13-
/^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2}$/;
15+
/^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2})?$/;
1416

1517
const RELATIVE_FILE_NAME_PATTERN =
1618
/^[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
@@ -176,6 +178,9 @@ export function inlineCodeFilePathCandidate(codeText: string): string | null {
176178
isWindowsAbsolutePath(candidate);
177179
if (!hasExplicitPathShape) {
178180
const withoutPosition = candidate.replace(POSITION_SUFFIX_PATTERN, "");
181+
// Markdown destinations accept `patches/`. Inline code still needs a
182+
// file-shaped final segment, so a trailing slash stays plain code.
183+
if (withoutPosition.endsWith("/")) return null;
179184
const firstSegment = withoutPosition.split("/")[0] ?? withoutPosition;
180185
if (looksLikeHostname(firstSegment, hasPosition)) return null;
181186
const basename =

0 commit comments

Comments
 (0)