mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 21:29:11 +00:00
Jump to Search Result from Khoj Modal even on Obsidian Android
Uses longest file path match to find markdown file in vault corresponding to file of search result returned by Khoj Allow jumping to search result from khoj plugin modal on Android too
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { App, SuggestModal, Notice, request, MarkdownRenderer, Instruction, Platform } from 'obsidian';
|
import { App, SuggestModal, Notice, request, MarkdownRenderer, Instruction, Platform } from 'obsidian';
|
||||||
import { KhojSetting } from 'src/settings';
|
import { KhojSetting } from 'src/settings';
|
||||||
import { getVaultAbsolutePath } from 'src/utils';
|
|
||||||
|
|
||||||
export interface SearchResult {
|
export interface SearchResult {
|
||||||
entry: string;
|
entry: string;
|
||||||
@@ -36,7 +35,7 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||||||
purpose: 'to open',
|
purpose: 'to open',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: Platform.isMacOS ? 'cmd ↵': 'ctrl ↵',
|
command: Platform.isMacOS ? 'cmd ↵' : 'ctrl ↵',
|
||||||
purpose: 'to rerank',
|
purpose: 'to rerank',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -73,23 +72,24 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onChooseSuggestion(result: SearchResult, _: MouseEvent | KeyboardEvent) {
|
async onChooseSuggestion(result: SearchResult, _: MouseEvent | KeyboardEvent) {
|
||||||
|
// Get all markdown files in vault
|
||||||
const mdFiles = this.app.vault.getMarkdownFiles();
|
const mdFiles = this.app.vault.getMarkdownFiles();
|
||||||
const vaultPath = getVaultAbsolutePath();
|
|
||||||
|
|
||||||
if (!vaultPath) {
|
|
||||||
new Notice('Khoj: Cannot open file. Make sure we are in a file system vault');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the vault file matching file of result. Open file at result heading
|
// Find the vault file matching file of result. Open file at result heading
|
||||||
mdFiles.forEach((file) => {
|
mdFiles
|
||||||
if (`${vaultPath}/${file.path}` === result.file) {
|
// Sort by descending length of path
|
||||||
let resultHeading = result.entry.split('\n', 1)[0];
|
// This finds longest path match when multiple files have same name
|
||||||
let linkToEntry = `${file.path}${resultHeading}`
|
.sort((a, b) => b.path.length - a.path.length)
|
||||||
this.app.workspace.openLinkText(linkToEntry, '');
|
.forEach((file) => {
|
||||||
console.log(`Link: ${linkToEntry}, File: ${file.path}, Heading: ${resultHeading}`);
|
// Find best file match across operating systems
|
||||||
return
|
// E.g Khoj Server on Linux, Obsidian Vault on Android
|
||||||
}
|
if (result.file.endsWith(file.path)) {
|
||||||
});
|
let resultHeading = result.entry.split('\n', 1)[0];
|
||||||
|
let linkToEntry = `${file.path}${resultHeading}`
|
||||||
|
this.app.workspace.openLinkText(linkToEntry, '');
|
||||||
|
console.log(`Link: ${linkToEntry}, File: ${file.path}, Heading: ${resultHeading}`);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user