From 0f796a79ec3d7389cd442cac82fef445af52d550 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 26 May 2024 18:03:15 +0530 Subject: [PATCH] Extract function to get link to entry in Obsidian vault for reuse --- src/interface/obsidian/src/search_modal.ts | 21 ++++----------------- src/interface/obsidian/src/utils.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/interface/obsidian/src/search_modal.ts b/src/interface/obsidian/src/search_modal.ts index c1b09db9..7d791204 100644 --- a/src/interface/obsidian/src/search_modal.ts +++ b/src/interface/obsidian/src/search_modal.ts @@ -1,6 +1,6 @@ import { App, SuggestModal, request, MarkdownRenderer, Instruction, Platform } from 'obsidian'; import { KhojSetting } from 'src/settings'; -import { createNoteAndCloseModal } from 'src/utils'; +import { createNoteAndCloseModal, getLinkToEntry } from 'src/utils'; export interface SearchResult { entry: string; @@ -132,21 +132,8 @@ export class KhojSearchModal extends SuggestModal { const mdFiles = this.app.vault.getMarkdownFiles(); const pdfFiles = this.app.vault.getFiles().filter(file => file.extension === 'pdf'); - // Find the vault file matching file of chosen search result - let file_match = mdFiles.concat(pdfFiles) - // Sort by descending length of path - // This finds longest path match when multiple files have same name - .sort((a, b) => b.path.length - a.path.length) - // The first match is the best file match across OS - // e.g Khoj server on Linux, Obsidian vault on Android - .find(file => result.file.replace(/\\/g, "/").endsWith(file.path)) - - // Open vault file at heading of chosen search result - if (file_match) { - let resultHeading = file_match.extension !== 'pdf' ? result.entry.split('\n', 1)[0] : ''; - let linkToEntry = resultHeading.startsWith('#') ? `${file_match.path}${resultHeading}` : file_match.path; - this.app.workspace.openLinkText(linkToEntry, ''); - console.log(`Link: ${linkToEntry}, File: ${file_match.path}, Heading: ${resultHeading}`); - } + // Find, Open vault file at heading of chosen search result + let linkToEntry = getLinkToEntry(mdFiles.concat(pdfFiles), result.file, result.entry); + if (linkToEntry) this.app.workspace.openLinkText(linkToEntry, ''); } } diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 53df076f..cfdbc431 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -347,3 +347,22 @@ export function pasteTextAtCursor(text: string | undefined) { editor.replaceRange(text, cursor); } } + +export function getLinkToEntry(sourceFiles: TFile[], chosenFile: string, chosenEntry: string): string | undefined { + // Find the vault file matching file of chosen file, entry + let fileMatch = sourceFiles + // Sort by descending length of path + // This finds longest path match when multiple files have same name + .sort((a, b) => b.path.length - a.path.length) + // The first match is the best file match across OS + // e.g Khoj server on Linux, Obsidian vault on Android + .find(file => chosenFile.replace(/\\/g, "/").endsWith(file.path)) + + // Return link to vault file at heading of chosen search result + if (fileMatch) { + let resultHeading = fileMatch.extension !== 'pdf' ? chosenEntry.split('\n', 1)[0] : ''; + let linkToEntry = resultHeading.startsWith('#') ? `${fileMatch.path}${resultHeading}` : fileMatch.path; + console.log(`Link: ${linkToEntry}, File: ${fileMatch.path}, Heading: ${resultHeading}`); + return linkToEntry; + } +}