-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
275 lines (241 loc) · 8.37 KB
/
main.ts
File metadata and controls
275 lines (241 loc) · 8.37 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { Plugin, TFile } from "obsidian";
import { MeilisearchService } from "./src/services/meilisearch";
import { IndexingService } from "./src/services/indexing";
import { SearchModal } from "./src/modals/SearchModal";
import { MeilisearchSettingTab } from "./src/settings/ui";
import { MeilisearchSettings, IndexingProgress } from "./src/types";
import { DEFAULT_SETTINGS } from "./src/settings";
import { showError } from "./src/utils/notifications";
export default class MeilisearchPlugin extends Plugin {
settings: MeilisearchSettings;
meilisearchService: MeilisearchService;
indexingService: IndexingService;
indexingProgress: IndexingProgress = {
total: 0,
processed: 0,
status: "idle",
};
async onload() {
await this.loadSettings();
this.meilisearchService = new MeilisearchService(this.settings);
try {
await this.initializeMeilisearch();
} catch {
return;
}
this.indexingService = new IndexingService(this.app, this.meilisearchService, (progress: IndexingProgress) => {
this.indexingProgress = progress;
});
this.addCommands();
this.addSettingTab(new MeilisearchSettingTab(this.app, this));
this.app.workspace.onLayoutReady(async () => {
await this.indexingService.loadMetadata();
if (this.settings.autoIndexOnStartup) {
await this.autoIndex();
}
this.registerFileHandlers(); // for real-time indexing
});
}
onunload() {
// Cleanup if necessary
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
if (this.meilisearchService) {
this.meilisearchService.updateSettings(this.settings);
}
}
/**
* Initialize Meilisearch connection
*/
private async initializeMeilisearch(): Promise<void> {
let success = false;
try {
success = await this.meilisearchService.initialize();
} catch (error) {
console.error("Failed to initialize Meilisearch:", error);
showError(`Failed to initialize Meilisearch: ${error.message}`);
throw error;
}
if (!success) {
throw new Error("Failed to initialize Meilisearch");
}
}
/**
* Register plugin commands
*/
private addCommands(): void {
this.addCommand({
id: "meilisearch-search",
name: "Search",
callback: () => {
this.openSearchModal();
},
});
this.addCommand({
id: "meilisearch-force-reindex",
name: "Force re-index",
callback: async () => {
try {
await this.forceReindex();
} catch (error) {
showError(`Force re-index failed: ${error.message}`);
}
},
});
this.addCommand({
id: "meilisearch-test-connection",
name: "Test connection",
callback: async () => {
try {
const success = await this.testConnection();
if (!success) {
showError("Failed to connect to Meilisearch");
}
} catch (error) {
showError(`Connection test failed: ${error.message}`);
}
},
});
}
/**
* Register file event handlers for real-time indexing
*/
private registerFileHandlers(): void {
this.registerEvent(
this.app.vault.on("create", async (file) => {
if (this.meilisearchService.isInitialized() && file instanceof TFile && file.extension === "md") {
setTimeout(async () => {
// ensure the file is fully written
try {
const content = await this.app.vault.read(file);
await this.indexFile(file, content);
} catch (error) {
console.error("Failed to index new file:", error);
}
}, 500);
}
}),
);
this.registerEvent(
this.app.vault.on("modify", async (file) => {
if (this.meilisearchService.isInitialized() && file instanceof TFile && file.extension === "md") {
try {
const content = await this.app.vault.read(file);
await this.indexFile(file, content);
} catch (error) {
console.error("Failed to index modified file:", error);
}
}
}),
);
this.registerEvent(
this.app.vault.on("delete", async (file) => {
if (this.meilisearchService.isInitialized() && file instanceof TFile && file.extension === "md") {
try {
await this.removeFromIndex(file);
} catch (error) {
console.error("Failed to remove file from index:", error);
}
}
}),
);
}
/**
* Auto-index on startup
*/
private async autoIndex(): Promise<void> {
try {
await this.indexingService.incrementalIndex();
} catch (error) {
console.error("Auto-indexing failed:", error);
showError(`Auto-indexing failed: ${error.message}`);
}
}
/**
* Index a single file
*/
private async indexFile(file: TFile, content: string): Promise<void> {
try {
const { parseDocument } = await import("./src/services/parser");
const document = await parseDocument(file, content);
await this.meilisearchService.indexDocuments([document]);
this.indexingService.updateFileMetadata(file.path, {
path: file.path,
hash: document.hash,
meilisearchId: document.id,
indexedAt: Date.now(),
});
await this.indexingService.saveMetadata();
} catch (error) {
console.error(`Failed to index file ${file.path}:`, error);
throw error;
}
}
/**
* Remove a file from the index
*/
private async removeFromIndex(file: TFile): Promise<void> {
try {
await this.meilisearchService.deleteDocuments([file.path]);
this.indexingService.removeFileMetadata(file.path);
await this.indexingService.saveMetadata();
} catch (error) {
console.error(`Failed to remove file from index ${file.path}:`, error);
throw error;
}
}
/**
* Open the search modal
*/
openSearchModal(): void {
if (!this.meilisearchService.isInitialized()) {
showError("Meilisearch is not initialized");
return;
}
new SearchModal(this, this.meilisearchService).open();
}
/**
* Force re-index all files
*/
async forceReindex(): Promise<void> {
if (!this.meilisearchService.isInitialized()) {
throw new Error("Meilisearch is not initialized");
}
try {
await this.indexingService.fullIndex();
} catch (error) {
console.error("Force re-index failed:", error);
throw error;
}
}
/**
* Test connection to Meilisearch
*/
async testConnection(): Promise<boolean> {
try {
return await this.meilisearchService.initialize();
} catch (error) {
console.error("Connection test failed:", error);
return false;
}
}
/**
* Get current indexing status
*/
getIndexingStatus(): string {
const { status, total, processed } = this.indexingProgress;
if (status === "idle") {
return "Idle";
} else if (status === "indexing") {
return `Indexing (${processed}/${total})`;
} else if (status === "error") {
return `Error: ${this.indexingProgress.error || "Unknown error"}`;
} else {
return status;
}
}
}