Skip to content

Commit

Permalink
Add command for opening the current note's gist on GitHub.com
Browse files Browse the repository at this point in the history
  • Loading branch information
timrogers committed Jul 1, 2024
1 parent f6b7dd7 commit e4256a4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ interface CopyGistUrlEditorCallbackParams {
plugin: ShareAsGistPlugin;
}

interface OpenGistEditorCallbackParams {
app: App;
plugin: ShareAsGistPlugin;
}

interface DocumentChangedAutoSaveCallbackParams {
app: App;
plugin: ShareAsGistPlugin;
Expand Down Expand Up @@ -108,6 +113,51 @@ const copyGistUrlEditorCallback =
}
};

const openGistEditorCallback =
(opts: OpenGistEditorCallbackParams) => async () => {
const { app, plugin } = opts;

const { enableUpdatingGistsAfterCreation } =
await getLatestSettings(plugin);

if (!enableUpdatingGistsAfterCreation) {
return new Notice(
"You need to enable 'Update gists after creation' in Settings to use this command.",
);
}

const view = app.workspace.getActiveViewOfType(MarkdownView);

if (!view) {
return new Notice('No active file');
}

const editor = view.editor;
const originalContent = editor.getValue();

const existingSharedGists = getSharedGistsForFile(originalContent);

if (existingSharedGists.length === 0) {
return new Notice(
'You must share this note as a gist before you can open its gist.',
);
}

if (existingSharedGists.length > 1) {
new SelectExistingGistModal(
app,
existingSharedGists,
false,
async (sharedGist) => {
window.open(sharedGist.url, '_blank');
},
).open();
} else {
const sharedGist = existingSharedGists[0];
window.open(sharedGist.url, '_blank');
}
};

const shareGistEditorCallback =
(opts: ShareGistEditorCallbackParams) => async () => {
const { isPublic, app, plugin } = opts;
Expand Down Expand Up @@ -301,6 +351,15 @@ export default class ShareAsGistPlugin extends Plugin {
}),
});

this.addCommand({
id: 'open-gist-url',
name: 'Open gist on GitHub.com',
callback: openGistEditorCallback({
plugin: this,
app: this.app,
}),
});

this.addModifyCallback();

// This adds a settings tab so the user can configure various aspects of the plugin
Expand Down

0 comments on commit e4256a4

Please sign in to comment.