MCPcopy
hub / github.com/opactorai/Claudable / toRelativePath

Function toRelativePath

lib/utils/path.ts:16–98  ·  view source on GitHub ↗
(absolutePath: string)

Source from the content-addressed store, hash-verified

14 * // Returns: '/src/app/page.tsx'
15 */
16export function toRelativePath(absolutePath: string): string {
17 if (!absolutePath) return absolutePath;
18
19 // If the string looks like plain text (contains whitespace), return as-is
20 if (/\s/.test(absolutePath)) {
21 return absolutePath;
22 }
23
24 // Check if this is an absolute path
25 const isAbsolutePath =
26 absolutePath.startsWith('/') ||
27 absolutePath.startsWith('\\') ||
28 /^[A-Za-z]:[\\\/]/.test(absolutePath); // Windows path like C:\
29
30 if (!isAbsolutePath) {
31 // Handle relative paths - check for user project directory pattern
32 // Pattern: data/projects/project-{id}/...
33 const userProjectPattern = /^data\/projects\/project-[^\/]+\/(.*)/;
34 const match = absolutePath.match(userProjectPattern);
35 if (match && match[1]) {
36 // Extract the path after the project directory
37 return `/${match[1]}`;
38 }
39
40 // Other relative paths: just add leading slash
41 return absolutePath.startsWith('/') ? absolutePath : `/${absolutePath}`;
42 }
43
44 // Get the project root from environment variable (injected by next.config.js)
45 const projectRoot = process.env.NEXT_PUBLIC_PROJECT_ROOT;
46
47 if (projectRoot) {
48 // Normalize both paths to use forward slashes for comparison
49 const normalizedPath = absolutePath.replace(/\\/g, '/');
50 const normalizedRoot = projectRoot.replace(/\\/g, '/');
51
52 if (normalizedPath.startsWith(normalizedRoot)) {
53 // Remove the project root and return with leading slash
54 let relativePath = normalizedPath.substring(normalizedRoot.length);
55
56 // Check if this is a user project path
57 const userProjectPattern = /^\/data\/projects\/project-[^\/]+\/(.*)/;
58 const projectMatch = relativePath.match(userProjectPattern);
59 if (projectMatch && projectMatch[1]) {
60 return `/${projectMatch[1]}`;
61 }
62
63 return relativePath.startsWith('/') ? relativePath : `/${relativePath}`;
64 }
65 }
66
67 // Fallback: Try to find common project directory patterns
68 const projectPatterns = [
69 '/Claudable-v2/',
70 '\\Claudable-v2\\',
71 ];
72
73 for (const pattern of projectPatterns) {

Callers 3

shortenPathFunction · 0.90
ChatLogFunction · 0.90
ToolResultItemFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected