mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-05 13:21:18 +00:00
Add ability to search for similar notes in Khoj Obsidian
- Hide input field on init, Trigger search on opening modal in similar notes mode - Set input to current markdown file and get similar notes to it - Enable rerank when searching for similar notes - Filter out current note from similar note search results
This commit is contained in:
@@ -21,6 +21,17 @@ export default class Khoj extends Plugin {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add a similar notes command
|
||||||
|
this.addCommand({
|
||||||
|
id: 'similar',
|
||||||
|
name: 'Find Similar Notes',
|
||||||
|
checkCallback: (checking) => {
|
||||||
|
if (!checking && this.settings.connectedToBackend)
|
||||||
|
new KhojModal(this.app, this.settings, true).open();
|
||||||
|
return this.settings.connectedToBackend;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Create an icon in the left ribbon.
|
// Create an icon in the left ribbon.
|
||||||
this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => {
|
this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => {
|
||||||
// Called when the user clicks the icon.
|
// Called when the user clicks the icon.
|
||||||
|
|||||||
@@ -9,10 +9,15 @@ export interface SearchResult {
|
|||||||
export class KhojModal extends SuggestModal<SearchResult> {
|
export class KhojModal extends SuggestModal<SearchResult> {
|
||||||
setting: KhojSetting;
|
setting: KhojSetting;
|
||||||
rerank: boolean = false;
|
rerank: boolean = false;
|
||||||
|
find_similar_notes: boolean;
|
||||||
|
|
||||||
constructor(app: App, setting: KhojSetting) {
|
constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) {
|
||||||
super(app);
|
super(app);
|
||||||
this.setting = setting;
|
this.setting = setting;
|
||||||
|
this.find_similar_notes = find_similar_notes;
|
||||||
|
|
||||||
|
// Hide input element in Similar Notes mode
|
||||||
|
this.inputEl.hidden = this.find_similar_notes;
|
||||||
|
|
||||||
// Register Modal Keybindings to Rerank Results
|
// Register Modal Keybindings to Rerank Results
|
||||||
this.scope.register(['Mod'], 'Enter', async () => {
|
this.scope.register(['Mod'], 'Enter', async () => {
|
||||||
@@ -49,6 +54,25 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||||||
this.setPlaceholder('Search with Khoj 🦅...');
|
this.setPlaceholder('Search with Khoj 🦅...');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async onOpen() {
|
||||||
|
if (this.find_similar_notes) {
|
||||||
|
// If markdown file is currently active
|
||||||
|
let file = this.app.workspace.getActiveFile();
|
||||||
|
if (file && file.extension === 'md') {
|
||||||
|
// Enable rerank of search results
|
||||||
|
this.rerank = true
|
||||||
|
// Set contents of active markdown file to input element
|
||||||
|
this.inputEl.value = await this.app.vault.read(file);
|
||||||
|
// Trigger search to get and render similar notes from khoj backend
|
||||||
|
this.inputEl.dispatchEvent(new Event('input'));
|
||||||
|
this.rerank = false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.resultContainerEl.setText('Cannot find similar notes for non-markdown files');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getSuggestions(query: string): Promise<SearchResult[]> {
|
async getSuggestions(query: string): Promise<SearchResult[]> {
|
||||||
// Query Khoj backend for search results
|
// Query Khoj backend for search results
|
||||||
let encodedQuery = encodeURIComponent(query);
|
let encodedQuery = encodeURIComponent(query);
|
||||||
@@ -56,6 +80,7 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||||||
let results = await request(searchUrl)
|
let results = await request(searchUrl)
|
||||||
.then(response => JSON.parse(response))
|
.then(response => JSON.parse(response))
|
||||||
.then(data => data
|
.then(data => data
|
||||||
|
.filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path))
|
||||||
.map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; }));
|
.map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; }));
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|||||||
Reference in New Issue
Block a user