From 39a18e2080bc14f7809be8f74f8725e40d6cf5e2 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 18:08:44 -0300 Subject: [PATCH] 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 --- src/interface/obsidian/src/main.ts | 11 +++++++++++ src/interface/obsidian/src/modal.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 1725b5ae..98f1cbb6 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -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. this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => { // Called when the user clicks the icon. diff --git a/src/interface/obsidian/src/modal.ts b/src/interface/obsidian/src/modal.ts index cb3e4a20..d44da44a 100644 --- a/src/interface/obsidian/src/modal.ts +++ b/src/interface/obsidian/src/modal.ts @@ -9,10 +9,15 @@ export interface SearchResult { export class KhojModal extends SuggestModal { setting: KhojSetting; rerank: boolean = false; + find_similar_notes: boolean; - constructor(app: App, setting: KhojSetting) { + constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) { super(app); 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 this.scope.register(['Mod'], 'Enter', async () => { @@ -49,6 +54,25 @@ export class KhojModal extends SuggestModal { 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 { // Query Khoj backend for search results let encodedQuery = encodeURIComponent(query); @@ -56,6 +80,7 @@ export class KhojModal extends SuggestModal { let results = await request(searchUrl) .then(response => JSON.parse(response)) .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; })); return results;