MCPcopy
hub / github.com/callumalpass/tasknotes / parseLinkToPath

Function parseLinkToPath

src/utils/linkUtils.ts:19–84  ·  view source on GitHub ↗
(linkText: string)

Source from the content-addressed store, hash-verified

17 * @returns The extracted path, or the original string if it's not a recognized link format
18 */
19export function parseLinkToPath(linkText: string): string {
20 if (!linkText) return linkText;
21
22 const trimmed = linkText.trim();
23
24 // Handle plain angle-bracket autolink style: <path/to/note.md>
25 if (trimmed.startsWith("<") && trimmed.endsWith(">")) {
26 let inner = trimmed.slice(1, -1).trim();
27 const hasMdExt = /\.md$/i.test(inner);
28 try {
29 inner = decodeURIComponent(inner);
30 } catch (error) {
31 tasknotesLogger.debug("Failed to decode URI component:", {
32 category: "internal",
33 operation: "decode-uri-component",
34 details: { value: inner },
35 error: error,
36 });
37 }
38
39 const parsedPath = parseLinkPath(inner);
40 return hasMdExt ? inner : parsedPath || inner;
41 }
42
43 // Handle wikilinks: [[path]] or [[path|alias]]
44 if (trimmed.startsWith("[[") && trimmed.endsWith("]]")) {
45 const inner = trimmed.slice(2, -2).trim();
46
47 // Manually strip alias if present.
48 const pipeIndex = inner.indexOf("|");
49 const pathOnly = pipeIndex !== -1 ? inner.substring(0, pipeIndex) : inner;
50 return parseLinkPath(pathOnly);
51 }
52
53 // Handle markdown links: [text](path)
54 const markdownMatch = trimmed.match(/^\[([^\]]*)\]\(([^)]+)\)$/);
55 if (markdownMatch) {
56 let linkPath = markdownMatch[2].trim();
57
58 // Strip angle brackets used to allow special characters/spaces in markdown links
59 if (linkPath.startsWith("<") && linkPath.endsWith(">")) {
60 linkPath = linkPath.slice(1, -1).trim();
61 }
62
63 const hasMdExt = /\.md$/i.test(linkPath);
64
65 // URL decode the link path - crucial for paths with spaces like Car%20Maintenance.md
66 try {
67 linkPath = decodeURIComponent(linkPath);
68 } catch (error) {
69 // If decoding fails, use the original path
70 tasknotesLogger.debug("Failed to decode URI component:", {
71 category: "internal",
72 operation: "decode-uri-component",
73 details: { value: linkPath },
74 error: error,
75 });
76 }

Calls 2

parseLinkPathFunction · 0.85
debugMethod · 0.80

Tested by

no test coverage detected