@@ -48,6 +48,23 @@ function applyBasicMdxFixes(content) {
48
48
. replace ( / < s u m m a r y [ ^ > ] * > / gi, '<summary>' ) ;
49
49
}
50
50
51
+ /**
52
+ * Resolve a path based on whether it's root-relative or regular relative
53
+ */
54
+ function resolvePath ( path , sourceDir , repoUrl , branch ) {
55
+ const cleanPath = path . replace ( / ^ \. \/ / , '' ) ;
56
+
57
+ // Handle root-relative paths (starting with /) - these are relative to repo root
58
+ if ( cleanPath . startsWith ( '/' ) ) {
59
+ const rootPath = cleanPath . substring ( 1 ) ; // Remove leading slash
60
+ return `${ repoUrl } /blob/${ branch } /${ rootPath } ` ;
61
+ }
62
+
63
+ // Handle regular relative paths - these are relative to the source file's directory
64
+ const fullPath = sourceDir ? `${ sourceDir } /${ cleanPath } ` : cleanPath ;
65
+ return `${ repoUrl } /blob/${ branch } /${ fullPath } ` ;
66
+ }
67
+
51
68
/**
52
69
* Fix all images to point to GitHub raw URLs
53
70
*/
@@ -78,17 +95,14 @@ export function transformRepo(content, { repoUrl, branch, sourcePath = '' }) {
78
95
return fixImages ( applyBasicMdxFixes ( content ) , repoUrl , branch , sourceDir )
79
96
// All relative links go to source repository (inline format)
80
97
. replace ( / \] \( (? ! h t t p | h t t p s | # | m a i l t o : ) ( [ ^ ) ] + ) \) / g, ( match , path ) => {
81
- const cleanPath = path . replace ( / ^ \] \( / , '' ) . replace ( / ^ \. \/ / , '' ) ;
82
- // Resolve relative path relative to the source file's directory
83
- const fullPath = sourceDir ? `${ sourceDir } /${ cleanPath } ` : cleanPath ;
84
- return `](${ repoUrl } /blob/${ branch } /${ fullPath } )` ;
98
+ const cleanPath = path . replace ( / ^ \] \( / , '' ) ;
99
+ const resolvedUrl = resolvePath ( cleanPath , sourceDir , repoUrl , branch ) ;
100
+ return `](${ resolvedUrl } )` ;
85
101
} )
86
102
// All relative links go to source repository (reference format)
87
103
. replace ( / ^ \[ ( [ ^ \] ] + ) \] : (? ! h t t p | h t t p s | # | m a i l t o : ) ( [ ^ \s ] + ) / gm, ( match , label , path ) => {
88
- const cleanPath = path . replace ( / ^ \. \/ / , '' ) ;
89
- // Resolve relative path relative to the source file's directory
90
- const fullPath = sourceDir ? `${ sourceDir } /${ cleanPath } ` : cleanPath ;
91
- return `[${ label } ]:${ repoUrl } /blob/${ branch } /${ fullPath } ` ;
104
+ const resolvedUrl = resolvePath ( path , sourceDir , repoUrl , branch ) ;
105
+ return `[${ label } ]:${ resolvedUrl } ` ;
92
106
} ) ;
93
107
}
94
108
0 commit comments