-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
43 lines (38 loc) · 1.11 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
import { Notice, Plugin, requestUrl } from 'obsidian';
export default class StashpadDocsPlugin extends Plugin {
async onload() {
this.addRibbonIcon('external-link', 'Share to Stashpad Docs', () => {
this.shareToStashpadDocs();
});
this.addCommand({
id: 'create-doc',
name: 'Create doc',
callback: () => {
this.shareToStashpadDocs();
},
})
}
async shareToStashpadDocs() {
const notice = new Notice('Creating a new Stashpad Doc...');
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile || !activeFile.name) return;
const text = await this.app.vault.read(activeFile);
const title = activeFile.name.split('.md')[0];
const content = `# ${title}\n${text}`;
const response = await requestUrl({
url: 'https://api.stashpad.live/v1/docs',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
content,
}),
});
const responseJson = response.json
if (!responseJson.ok) {
notice.setMessage('An error occurred.');
return;
}
notice.setMessage('New Stashpad Doc created.');
window.open(responseJson.uri);
}
}