Updated the weighting logic + doing some preparation for #6

Headings now have more weight. Also set TS strict mode
This commit is contained in:
Simon Cambier
2022-04-14 20:41:27 +02:00
parent a57582a389
commit 98c277e541
5 changed files with 117 additions and 48 deletions

View File

@@ -1,5 +1,12 @@
import markdownToTxt from 'markdown-to-txt'
import { regexLineSplit, regexWikilink, regexYaml } from './globals'
import { CachedMetadata } from 'obsidian'
import {
isSearchMatch,
regexLineSplit,
regexWikilink,
regexYaml,
SearchMatch,
} from './globals'
export function highlighter(str: string): string {
return '<span class="search-result-file-matched-text">' + str + '</span>'
@@ -64,3 +71,25 @@ export function wait(ms: number): Promise<void> {
export function escapeRegex(str: string): string {
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}
/**
* Returns the positions of all occurences of `val` inside of `text`
* https://stackoverflow.com/a/58828841
* @param text
* @param val
* @returns
*/
export function getAllIndexes(text: string, val: RegExp): SearchMatch[] {
return [...text.matchAll(val)]
.map(o => ({ match: o[0], index: o.index }))
.filter(isSearchMatch)
}
export function extractHeadingsFromCache(
cache: CachedMetadata,
level: number,
): string[] {
return (
cache.headings?.filter(h => h.level === level).map(h => h.heading) ?? []
)
}