@@ -42,7 +42,7 @@ export function convertToGitHubContentUrl(dataFileUrl: string): string {
4242}
4343
4444/**
45- * Converts github content url to user friendly github repository url for display
45+ * Converts github content url to user- friendly github repository url for display
4646 * and linking to external public data sources hosted on github.
4747 *
4848 * @param dataFileUrl Github content data file url from a github repository.
@@ -51,15 +51,25 @@ export function convertToGitHubContentUrl(dataFileUrl: string): string {
5151export function convertToGitHubRepositoryUrl ( dataFileUrl : string ) : string {
5252 let gitHubContentUrl : string = dataFileUrl ;
5353 if ( dataFileUrl . startsWith ( 'https://raw.githubusercontent.com/' ) ) {
54- // rewrite github content url to use github repository url for display
55- gitHubContentUrl = dataFileUrl . replace ( 'https://raw.githubusercontent.com/' , 'https://github.com/' ) ;
5654
57- // add blob part
58- // TODO: fix this with regex or branch name tocken extraction later
59- gitHubContentUrl = gitHubContentUrl . replace ( '/main/' , '/blob/main/' ) ;
60- gitHubContentUrl = gitHubContentUrl . replace ( '/master/' , '/blob/master/' ) ;
61- gitHubContentUrl = gitHubContentUrl . replace ( '/gh-pages/' , '/blob/gh-pages/' ) ;
55+ // rewrite github content url to user-friendly github repository url for display
56+ const gitHubContentUri : Uri = Uri . parse ( dataFileUrl ) ;
57+
58+ // parse github content path tokens
59+ const contentPathTokens : Array < string > = gitHubContentUri . path . split ( '/' ) ;
60+
61+ if ( contentPathTokens . length > 4 ) {
62+ // extract github user, repository, and branch name
63+ const user : string = contentPathTokens [ 1 ] ;
64+ const repository : string = contentPathTokens [ 2 ] ;
65+ const branch : string = contentPathTokens [ 3 ] ;
66+
67+ // rewrite github content url to github.com url
68+ gitHubContentUrl = dataFileUrl . replace ( `https://raw.githubusercontent.com/${ user } /${ repository } /${ branch } /` ,
69+ `https://github.com/${ user } /${ repository } /blob/${ branch } /` ) ;
70+ }
6271 }
72+
6373 return gitHubContentUrl ;
6474}
6575
0 commit comments