generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
98 lines (83 loc) · 2.59 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
import { App, Editor, MarkdownView, Plugin, Notice, PluginSettingTab, Setting } from 'obsidian';
interface ButtondownPluginSettings {
APIKey: string;
}
const DEFAULT_SETTINGS: ButtondownPluginSettings = {
APIKey: ''
}
export default class ButtondownPlugin extends Plugin {
settings: ButtondownPluginSettings;
async saveDraft(title: string, body: string): Promise<void> {
if (!this.settings.APIKey) {
new Notice("Please set your API key in the settings!");
return;
}
try {
const result = await fetch("https://api.buttondown.email/v1/emails", {
method: "POST",
headers: new Headers({
Authorization: `Token ${this.settings.APIKey}`,
"Content-Type": "application/json",
}),
body: JSON.stringify({
"body": body,
"subject": title,
"status": "draft",
}),
});
if (result.ok) {
new Notice("Sent draft to Buttondown");
} else {
console.error("Error - something went wrong: ", result);
new Notice("Something went wrong sending draft to Buttondown. Please check the console for more info");
}
} catch (e) {
console.error("Error - something went wrong: ", e);
new Notice("Something went wrong sending draft to Buttondown. Please check the console for more info");
}
}
async onload() {
console.log("Loading buttondown plugin");
await this.loadSettings();
this.addCommand({
id: 'note-to-buttondown-draft',
name: 'Create a new Buttondown draft from this note',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.saveDraft(view.file.basename, editor.getValue())
}
});
this.addSettingTab(new SampleSettingTab(this.app, this))
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: ButtondownPlugin;
constructor(app: App, plugin: ButtondownPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Buttondown Settings' });
new Setting(containerEl)
.setName('API key')
.setDesc('Find it at https://buttondown.email/settings#api')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.APIKey)
.onChange(async (value) => {
value.replace(/-/, "");
this.plugin.settings.APIKey = value;
await this.plugin.saveSettings();
})
);
}
}