generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
127 lines (99 loc) · 3.13 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting} from 'obsidian';
import {Octokit} from '@octokit/rest';
// Remember to rename these classes and interfaces!
interface SaveAsGistSettings {
githubApiToken?: string;
}
const DEFAULT_SETTINGS: SaveAsGistSettings = {
githubApiToken: null
}
export default class SaveAsGist extends Plugin {
settings: SaveAsGistSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: 'save-as-new-gist',
name: 'Save current file as a new private 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();
await this._saveAsGist(fileName, body);
}
});
this.addCommand({
id: 'save-as-new-gist-selection',
name: 'Save current selection as a new 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 body = editor.getSelection();
await this._saveAsGist(fileName, body);
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SaveAsGistSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async _saveAsGist(fileName: string, body: string) {
const token = this.settings.githubApiToken;
if (!token) {
new Notice('GitHub token not found, check your settings');
return;
}
try {
const octokit = new Octokit({
auth: token
});
const result = await octokit.rest.gists.create({
files: {
[fileName]: {
content: body
}
}
})
const url = result.data.html_url;
await navigator.clipboard.writeText(url);
new Notice(`Gist created ${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;
constructor(app: App, plugin: SaveAsGist) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Save As Gist'});
new Setting(containerEl)
.setName('Github API token')
.setDesc('create a token here https://github.com/settings/tokens/new (only gist permission required)')
.addText(text => text
.setPlaceholder('Enter your Github API token')
.setValue(this.plugin.settings.githubApiToken)
.onChange(async (value) => {
this.plugin.settings.githubApiToken = value;
await this.plugin.saveSettings();
}));
}
}