Skip to content

Commit

Permalink
feat: ✨ Add "Open File in GitHub" Command, fix #149
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Dec 26, 2021
1 parent 80ea17e commit 2c216d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ esbuild.build({
},
entryPoints: ['src/main.ts'],
bundle: true,
external: ['obsidian'],
external: ['obsidian', 'electron'],
format: 'cjs',
watch: !prod,
target: 'es2016',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"devDependencies": {
"@types/node": "^14.14.2",
"electron": "^16.0.5",
"esbuild": "^0.12.26",
"esbuild-svelte": "^0.5.4",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
Expand Down
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChangedFilesModal } from "src/ui/modals/changedFilesModal";
import { CustomMessageModal } from "src/ui/modals/customMessageModal";
import { DEFAULT_SETTINGS, VIEW_CONFIG } from "./constants";
import { GitManager } from "./gitManager";
import openInGitHub from "./openInGitHub";
import { SimpleGit } from "./simpleGit";
import { ObsidianGitSettings, PluginState } from "./types";
import addIcons from "./ui/icons";
Expand Down Expand Up @@ -61,6 +62,12 @@ export default class ObsidianGit extends Plugin {
},
});

this.addCommand({
id: 'view-file-in-github',
name: 'Open File in GitHub',
editorCallback: (editor, {file}) => openInGitHub(editor, file, this.gitManager),
});

this.addCommand({
id: "pull",
name: "Pull",
Expand Down
15 changes: 15 additions & 0 deletions src/openInGitHub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Editor, Notice, TFile } from "obsidian";
import { GitManager } from "./gitManager";
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}`);
} else {
new Notice('It seems like you are not using GitHub');
}
}

0 comments on commit 2c216d0

Please sign in to comment.