Skip to content

Commit 3c34232

Browse files
refine github content to repo Url rewrite for github data packages and github data source views (#102 & #108)
1 parent 00e3d50 commit 3c34232

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/commands/listDataResources.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async function listDataResources(dataPackageUri: Uri): Promise<void> {
6161

6262
// construct github repository resource Url
6363
let resourceUrl: string = fileUtils.convertToGitHubRepositoryUrl(resource.source);
64-
64+
6565
if (dataPackageUri.scheme === 'file') {
6666
// create local resource path using data package Uri as resource base path
6767
const resourceUri: Uri = Uri.joinPath(dataPackageUri, `../${resource.source}`);

src/utils/fileUtils.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
5151
export 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

Comments
 (0)