@@ -48,6 +48,23 @@ function applyBasicMdxFixes(content) {
4848 . replace ( / < s u m m a r y [ ^ > ] * > / gi, '<summary>' ) ;
4949}
5050
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+
5168/**
5269 * Fix all images to point to GitHub raw URLs
5370 */
@@ -78,17 +95,14 @@ export function transformRepo(content, { repoUrl, branch, sourcePath = '' }) {
7895 return fixImages ( applyBasicMdxFixes ( content ) , repoUrl , branch , sourceDir )
7996 // All relative links go to source repository (inline format)
8097 . 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 } )` ;
85101 } )
86102 // All relative links go to source repository (reference format)
87103 . 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 } ` ;
92106 } ) ;
93107}
94108
0 commit comments