generated from obsidianmd/obsidian-sample-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1651d67
commit 083a45b
Showing
2 changed files
with
71 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Word } from "../CustomDictionaryService"; | ||
import { | ||
capitalizeFirstLetter, | ||
lowerStartsWith, | ||
lowerStartsWithoutSpace, | ||
startsWithoutSpace, | ||
} from "../util/strings"; | ||
|
||
interface Judgement { | ||
word: Word; | ||
value?: string; | ||
alias: boolean; | ||
} | ||
|
||
function judge( | ||
word: Word, | ||
query: string, | ||
queryStartWithUpper: boolean | ||
): Judgement { | ||
if (word.value === query) { | ||
return { word: word, alias: false }; | ||
} | ||
|
||
if ( | ||
word.value.startsWith("[[") | ||
? lowerStartsWithoutSpace(word.value.replace("[[", ""), query) | ||
: startsWithoutSpace(word.value, query) | ||
) { | ||
return { word: word, value: word.value, alias: false }; | ||
} | ||
|
||
if ( | ||
queryStartWithUpper && | ||
startsWithoutSpace(capitalizeFirstLetter(word.value), query) | ||
) { | ||
word.value = capitalizeFirstLetter(word.value); | ||
return { word: word, value: word.value, alias: false }; | ||
} | ||
|
||
const matchedAlias = word.aliases?.find((a) => | ||
lowerStartsWithoutSpace(a, query) | ||
); | ||
if (matchedAlias) { | ||
return { word: word, value: matchedAlias, alias: true }; | ||
} | ||
|
||
return { word: word, alias: false }; | ||
} | ||
|
||
export function suggestWords( | ||
words: Word[], | ||
query: string, | ||
max: number | ||
): Word[] { | ||
const queryStartWithUpper = capitalizeFirstLetter(query) === query; | ||
return Array.from(words) | ||
.map((x) => judge(x, query, queryStartWithUpper)) | ||
.filter((x) => x.value !== undefined) | ||
.sort((a, b) => { | ||
const aliasP = (Number(a.alias) - Number(b.alias)) * 10000; | ||
const startP = | ||
(Number(lowerStartsWith(b.value!, query)) - | ||
Number(lowerStartsWith(a.value!, query))) * | ||
1000; | ||
const lengthP = a.value!.length - b.value!.length; | ||
return aliasP + startP + lengthP; | ||
}) | ||
.map((x) => x.word) | ||
.slice(0, max); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters