-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-suggest.ts
35 lines (30 loc) · 999 Bytes
/
file-suggest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { TFile } from "obsidian";
import fuzzysort from "fuzzysort";
import { highlightSearch } from "src/utils/misc";
import { TextInputSuggest } from "src/utils/suggest";
export class FileSuggest extends TextInputSuggest<Fuzzysort.KeyResult<TFile>> {
getSuggestions(inputStr: string): Fuzzysort.KeyResult<TFile>[] {
let abstractFiles = this.app.vault.getAllLoadedFiles();
const lowerCaseInputStr = inputStr.toLowerCase();
abstractFiles = abstractFiles.filter((file) => {
return file.path.endsWith(".md");
});
abstractFiles = abstractFiles.map((file) => {
return {
...file,
path: file.path.slice(0, -3),
};
});
return fuzzysort.go(lowerCaseInputStr, abstractFiles, {
key: "path",
}) as any;
}
renderSuggestion(file: Fuzzysort.KeyResult<TFile>, el: HTMLElement): void {
highlightSearch(el, file);
}
selectSuggestion(file: Fuzzysort.KeyResult<TFile>): void {
this.inputEl.value = file.obj?.path;
this.inputEl.trigger("input");
this.close();
}
}