Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: save and update gists #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,49 @@ export default class SaveAsGist extends Plugin {
}
});

this.addCommand({
id: "save-as-new-updateable-gist",
name: "Save current file as a new private, updatable Gist",
editorCallback: async (editor: Editor, view: MarkdownView) => {
const noteFile = view.file; // Currently Open Note
const fileName = noteFile.name;
if (!fileName) {
return; // Nothing Open
}

// Read the currently open note file.
const body = editor.getValue();

const gistMetadata = await this._saveAsGist(fileName, body);
await this._prependGistMetadata(fileName, gistMetadata);
}
});

this.addCommand({
id: "update-existing-gist",
name: "Update current file in existing private Gist",
editorCallback: async (editor: Editor, view: MarkdownView) => {
const noteFile = view.file; // Currently Open Note
const fileName = noteFile.name;

if (!fileName) {
return; // Nothing Open
}

const gistId = this.app.metadataCache.getCache(noteFile.path).frontmatter.gist_id
if (!gistId) {
new Notice('gist_id missing in file frontmatter');
return;
}

// Read the currently open note file.
const body = editor.getValue();

const gistMetadata = await this._saveAsGist(fileName, body);
await this._prependGistMetadata(fileName, gistMetadata);
}
});

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SaveAsGistSettingTab(this.app, this));
}
Expand Down Expand Up @@ -89,14 +132,54 @@ export default class SaveAsGist extends Plugin {
await navigator.clipboard.writeText(url);

new Notice(`Gist created ${url} - URL copied to your clipboard`);

let splitUrl = url.split("/");
let gistId = splitUrl[splitUrl.length - 1]

return {gistId: gistId, gistUrl: url}
} catch (err) {
new Notice('There was an error creating your gist, check your token and connection');
throw err;
}

}
}

async _prependGistMetadata(tFile, gistMetadata) {
this.app.fileManager.processFrontMatter(tFile,
(frontmatter) => {
frontmatter.gist_id = gistMetadata.gistId;
frontmatter.gist_url = gistMetadata.gistUrl
});
}

async _updateAsGist(gistId, fileName, body) {
const token = this.settings.githubApiToken;
if (!token) {
new import_obsidian.Notice("GitHub token not found, check your settings");
return;
}

try {
const octokit = new Octokit2({
auth: token
});
const result = yield octokit.rest.gists.update({
gist_id: gistId,
files: {
[fileName]: {
content: body
}
}
});

const url = result.data.html_url;
yield navigator.clipboard.writeText(url);
new Notice(`Gist updated ${url} - URL copied to your clipboard`);
} catch (err) {
new Notice("There was an error creating your gist, check your token and connection");
throw err;
}}
}

class SaveAsGistSettingTab extends PluginSettingTab {
plugin: SaveAsGist;
Expand Down