mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-06 13:22:12 +00:00
Split code in onChooseSuggestion method to make it more readable
Split find file, jump to file code to make onChooseSuggestion more readable - Use find, instead of using return in forEach to get first match - Move the jump to file+heading code out from forEach
This commit is contained in:
@@ -101,21 +101,21 @@ export class KhojModal extends SuggestModal<SearchResult> {
|
|||||||
// Get all markdown files in vault
|
// Get all markdown files in vault
|
||||||
const mdFiles = this.app.vault.getMarkdownFiles();
|
const mdFiles = this.app.vault.getMarkdownFiles();
|
||||||
|
|
||||||
// Find the vault file matching file of result. Open file at result heading
|
// Find the vault file matching file of chosen search result
|
||||||
mdFiles
|
let file_match = mdFiles
|
||||||
// Sort by descending length of path
|
// Sort by descending length of path
|
||||||
// This finds longest path match when multiple files have same name
|
// This finds longest path match when multiple files have same name
|
||||||
.sort((a, b) => b.path.length - a.path.length)
|
.sort((a, b) => b.path.length - a.path.length)
|
||||||
.forEach((file) => {
|
// The first match is the best file match across OS
|
||||||
// Find best file match across operating systems
|
// e.g Khoj server on Linux, Obsidian vault on Android
|
||||||
// E.g Khoj Server on Linux, Obsidian Vault on Android
|
.find(file => result.file.endsWith(file.path))
|
||||||
if (result.file.endsWith(file.path)) {
|
|
||||||
|
// Open vault file at heading of chosen search result
|
||||||
|
if (file_match){
|
||||||
let resultHeading = result.entry.split('\n', 1)[0];
|
let resultHeading = result.entry.split('\n', 1)[0];
|
||||||
let linkToEntry = `${file.path}${resultHeading}`
|
let linkToEntry = `${file_match.path}${resultHeading}`
|
||||||
this.app.workspace.openLinkText(linkToEntry, '');
|
this.app.workspace.openLinkText(linkToEntry, '');
|
||||||
console.log(`Link: ${linkToEntry}, File: ${file.path}, Heading: ${resultHeading}`);
|
console.log(`Link: ${linkToEntry}, File: ${file_match.path}, Heading: ${resultHeading}`);
|
||||||
return
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user