generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
158 lines (137 loc) Β· 4.76 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { App, Plugin, PluginSettingTab, Setting, MarkdownView, Notice } from 'obsidian';
interface StenographyPluginSettings {
apiKey: string;
}
const DEFAULT_SETTINGS: StenographyPluginSettings = {
apiKey: ''
}
export default class StenographyPlugin extends Plugin {
settings: StenographyPluginSettings;
/*
This code is adding a new ribbon icon to the editor. The code will run when the user clicks on it.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
async onload() {
console.log('loading stenography plugin');
await this.loadSettings();
this.addRibbonIcon('code-glyph', 'Stenography', async () => {
await this.stenographyWorkflow();
});
this.addCommand({
id: 'run-stenograpy',
name: 'Run Stenography',
callback: async () => {
await this.stenographyWorkflow();
}
});
this.addSettingTab(new StenographyPluginTab(this.app, this));
}
/*
This code is a simple extension that adds the ability to convert selected text into stenography.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
async stenographyWorkflow() {
try {
if (this.app.workspace.getActiveViewOfType(MarkdownView)) {
const editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
const selectedText = editor.getSelection(); // Get selected text
if (selectedText && selectedText.length > 0) {
const res = await this.fetchStenography(selectedText)
editor.replaceSelection(`${res.res}\n\n\`\`\`${res.language}\n${selectedText}\n\`\`\`\n\n`)
}
} else {
throw new Error('MarkdownView is null');
}
} catch (err) {
new Notice(err.message);
}
}
/*
This code is fetching the markdown from Stenography.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
async fetchStenography(code: string): Promise <any> {
const statusHTML = this.addStatusBarItem()
statusHTML.setText('Loading from Stenography...');
try {
const res = await fetch('https://stenography-worker.stenography.workers.dev',
{
method: 'POST',
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
body: JSON.stringify({
code: code,
api_key: this.settings.apiKey,
audience: 'pm',
populate: false,
stackoverflow: false
})
})
const data = await res.json();
const markdown = data.pm
const language = data.metadata.language || ''
if (markdown && Object.keys(markdown).length === 0 && Object.getPrototypeOf(markdown) === Object.prototype) {
return { res: `Stenography response empty!`, language: '' }
}
return { res: markdown, language: language }
} catch(err) {
if (err.message.includes('Failed to fetch')) { return { res: `Error loading from Stenography! API key error, did you set it in settings?`, language: '' } }
else return { res: `Error loading from Stenography! Error: ${err}`, language: '' }
} finally {
statusHTML.remove();
}
}
/*
This code is a JavaScript function that will be called when the browser unloads.
It's used to log messages in the console, which can be seen by opening Developer Tools and clicking on Console tab.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
onunload() {
console.log('unloading stenography plugin');
}
/*
This code is loading the settings from a file.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
/*
This code is saving the settings object to a file.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
async saveSettings() {
await this.saveData(this.settings);
}
}
class StenographyPluginTab extends PluginSettingTab {
plugin: StenographyPlugin;
/*
This code is creating a new instance of the Stenography class.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
constructor(app: App, plugin: StenographyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
/*
This code is creating a new Setting object. The first parameter to the constructor is the container element that will hold this setting.
- generated by stenography autopilot [ ππ©ββοΈ ]
*/
display(): void {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Settings for Stenography.' });
// for api key
new Setting(containerEl)
.setName('API Key')
.setDesc('Get your API key here: https://stenography.dev/dashboard')
.addText(text => text
.setPlaceholder('xxxx-xxxx-xxxx-xxxx')
.setValue('')
.onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
new Notice('Saved API key!');
}));
}
}