Skip to content

Commit

Permalink
fix: 🐛 Fix RegEx for openInGitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Dec 27, 2021
1 parent 2c216d0 commit ca59a2d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/openInGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { shell } from "electron";
export default async function openInGitHub(editor: Editor, file: TFile, manager: GitManager) {
const remoteUrl = await manager.getConfig('remote.origin.url') as string;
const branch = (await manager.branchInfo()).current;
const [url, user, repo] = remoteUrl.match(/(?>^https:\/\/github\.com\/(.*)\/(.*)\.git$)|(?>^git@github\.com:(.*)\/(.*)\.git$)/);
//If Github is used
if (url) {
await shell.openExternal(`https://github.com/${user}/${repo}/blob/${branch}/${file.path}`);
const [isGitHub, user, repo] = remoteUrl.match(/(?:^https:\/\/github\.com\/(.*)\/(.*)\.git$)|(?:^git@github\.com:(.*)\/(.*)\.git$)/);
if (!!isGitHub) {
const from = editor.getCursor("from").line + 1;
const to = editor.getCursor("to").line + 1;
if(from === to) {
await shell.openExternal(`https://github.com/${user}/${repo}/blob/${branch}/${file.path}?plain=1#L${from}`);
} else {
await shell.openExternal(`https://github.com/${user}/${repo}/blob/${branch}/${file.path}?plain=1#L${from}-L${to}`);
}
} else {
new Notice('It seems like you are not using GitHub');
}
}
}

0 comments on commit ca59a2d

Please sign in to comment.