generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
198 lines (154 loc) · 5.15 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
import { CreateCompletionResponseChoicesInner, Configuration, OpenAIApi } from 'openai';
import { MyPluginSettingsTab } from 'settings';
require('dotenv').config()
interface MyPluginSettings {
OPEN_AI_KEY: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
OPEN_AI_KEY: '',
};
export default class MyPlugin extends Plugin {
openai: OpenAIApi;
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new MyPluginSettingsTab(this.app, this));
this.addCommand({
id: 'find-chapters',
name: 'Find Topic Chapters',
callback: async () => {
const activeMarkdown_name = this.app.workspace.getActiveFile()?.basename;
const activeMarkdown = this.app.workspace.getActiveFile()
new Notice(`${activeMarkdown_name}`)
if (activeMarkdown_name){
const fileContent = await this.FindChapters(activeMarkdown_name);
if (activeMarkdown instanceof TFile){
await this.app.vault.modify(activeMarkdown, fileContent);
}
}
}
});
this.addCommand({
id: 'openai-prompt',
name: 'OpenAI Prompt',
callback: () =>{
new OpenAIPrompt(this.app, this.openai).open();
}
});
this.addCommand({
id: "create-pages",
name: "Create Pages with List",
callback: async () =>{
const activeFileName = this.app.workspace.getActiveFile();
if (activeFileName instanceof TFile){
const content = await this.app.vault.read(activeFileName);
const modifiedContent = this.modifyFileContent(content);
new Notice(`${modifiedContent}`)
await this.app.vault.modify(activeFileName, modifiedContent)
}
}
});
this.openai = new OpenAIApi(
new Configuration({
apiKey: this.settings.OPEN_AI_KEY,
})
);
}
onunload() {
console.log('unloading plugin');
}
async FindChapters(topic: string) {
const completion = await this.openai.createCompletion({
model: "text-davinci-003",
prompt: "Answer the following question: What are the main chapters of "+ topic + "?{}",
temperature: .7,
max_tokens: 150,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ['{}'],
});
return completion?.data?.choices?.[0]?.text?.trim() || '';
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
private modifyFileContent(content: string): string{
const lines = content.split('\n');
const regex = /^\d+\.\s+/;
const result = lines
.filter(line => regex.test(line))
.map(line => line.replace(regex, ''))
.join('\n');
const listArray = result.split('\n');
const modifiedList = listArray.map((item) =>`[[${item.trim()}]]`);
return modifiedList.join('\n');
}
}
class OpenAIPrompt extends Modal {
topicInput: HTMLInputElement;
constructor(app: App, private openai: OpenAIApi) {
super(app);
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h2', { text: 'OpenAI Prompt' });
new Notice('Enter a prompt.');
const formEl = contentEl.createEl('form');
formEl.addEventListener('submit', async (event) => {
event.preventDefault();
const topic = this.topicInput.value;
if (!topic) {
new Notice('Please enter a prompt.');
return;
}
const file = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!file) return;
const editor = file.editor;
const cursor = editor.getCursor();
if (!cursor) return;
const line = cursor?.line;
const ch = cursor?.ch;
const position = editor.posToOffset({ line, ch });
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) return;
const content = await this.app.vault.read(activeFile);
const newContent = await this.promptGPT(topic);
const insertedContent = this.insertTextAtPosition(content, `- ${newContent}`, position)
await this.app.vault.modify(activeFile, insertedContent);
new Notice('File modified!');
this.close();
});
this.topicInput = formEl.createEl('input', {
attr: { type: 'text' },
});
const submitButton = formEl.createEl('button', { text: 'Submit' });
submitButton.addEventListener('click', () => {
formEl.dispatchEvent(new Event('submit'));
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
async promptGPT(prompt: string){
const output = await this.openai.createCompletion({
model: "text-davinci-003",
prompt: prompt + "{}",
temperature: .7,
max_tokens: 150,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ['{}'],
});
return output.data.choices[0].text?.trim().replace(/\n/g, "\n-");
}
private insertTextAtPosition(text:string, insert: string, position: number){
return text.slice(0, position) + insert + text.slice(position);
}
}