Skip to content

Commit a75c67a

Browse files
committed
fix(web): classify relative spaced folder links
1 parent ee5ed3d commit a75c67a

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
2+
import { createElement } from "react";
3+
import { renderToStaticMarkup } from "react-dom/server";
4+
import ReactMarkdown from "react-markdown";
25

36
import {
47
extractMarkdownLinkHrefs,
@@ -9,6 +12,25 @@ import {
912
shouldOpenMarkdownFileLinkInEditor,
1013
} from "./markdown-links";
1114

15+
function renderMarkdownLinkHref(markdown: string): string | undefined {
16+
let renderedHref: string | undefined;
17+
renderToStaticMarkup(
18+
createElement(
19+
ReactMarkdown,
20+
{
21+
components: {
22+
a({ href }) {
23+
renderedHref = href;
24+
return createElement("a", { href });
25+
},
26+
},
27+
},
28+
markdown,
29+
),
30+
);
31+
return renderedHref;
32+
}
33+
1234
describe("extractMarkdownLinkHrefs", () => {
1335
it("extracts angle-bracketed paths containing spaces", () => {
1436
expect(
@@ -141,6 +163,17 @@ describe("resolveMarkdownFileLinkTarget", () => {
141163
});
142164
});
143165

166+
it("resolves relative spaced folders from the markdown renderer", () => {
167+
const href = renderMarkdownLinkHref("[folder](<docs/My Folder>)");
168+
169+
expect(href).toBe("docs/My%20Folder");
170+
expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
171+
targetPath: "/repo/project/docs/My Folder",
172+
workspaceRelativePath: "docs/My Folder",
173+
basename: "My Folder",
174+
});
175+
});
176+
144177
it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
145178
expect(
146179
resolveMarkdownFileLinkMeta(

‎apps/web/src/markdown-links.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
99
const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
1010
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
1111
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
12-
const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+(?::\d+){0,2}$/;
12+
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}$/;
1314
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
1415
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
1516
const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/;

0 commit comments

Comments
 (0)