Skip to content

Commit

Permalink
#25 - quoted expressions for Vault search ok
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed Apr 26, 2022
1 parent fc9c677 commit cb7a11c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/components/ModalInFile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ onDestroy(() => {
eventBus.enable("vault")
})
$: {
$: (async () => {
if (searchQuery) {
note = getSuggestions(searchQuery, { singleFilePath })[0] ?? null
note = (await getSuggestions(searchQuery, { singleFilePath }))[0] ?? null
lastSearch = searchQuery
}
selectedIndex = 0
scrollIntoView()
}
})()
$: {
if (note) {
Expand Down
6 changes: 3 additions & 3 deletions src/components/ModalVault.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ let searchQuery: string
let resultNotes: ResultNote[] = []
$: selectedNote = resultNotes[selectedIndex]
$: {
$: (async() => {
if (searchQuery) {
resultNotes = getSuggestions(searchQuery)
resultNotes = await getSuggestions(searchQuery)
lastSearch = searchQuery
}
selectedIndex = 0
scrollIntoView()
}
})()
onMount(() => {
searchQuery = lastSearch
Expand Down
29 changes: 23 additions & 6 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
type ResultNote,
type SearchMatch,
} from './globals'
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
import {
extractHeadingsFromCache,
splitQuotes,
stringsToRegex,
stripMarkdownCharacters,
wait,
} from './utils'

let minisearchInstance: MiniSearch<IndexedNote>

Expand Down Expand Up @@ -57,9 +63,9 @@ export async function initGlobalSearchIndex(): Promise<void> {
* @param query
* @returns
*/
function search(query: string): SearchResult[] {
async function search(query: string): Promise<SearchResult[]> {
if (!query) return []
return minisearchInstance.search(query, {
let results = minisearchInstance.search(query, {
prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false),
combineWith: 'AND',
Expand All @@ -70,6 +76,17 @@ function search(query: string): SearchResult[] {
headings3: 1.1,
},
})
const quoted = splitQuotes(query.toLowerCase())

if (quoted.length) {
results = results.filter(r => {
const content = stripMarkdownCharacters(
indexedNotes[r.id]?.content ?? '',
).toLowerCase()
return quoted.every(q => content.includes(q))
})
}
return results
}

/**
Expand All @@ -96,12 +113,12 @@ export function getMatches(text: string, reg: RegExp): SearchMatch[] {
* @param options
* @returns
*/
export function getSuggestions(
export async function getSuggestions(
query: string,
options?: Partial<{ singleFilePath: string | null }>,
): ResultNote[] {
): Promise<ResultNote[]> {
// Get the raw results
let results = search(query)
let results = await search(query)
if (!results.length) return []

// Either keep the 50 first results,
Expand Down
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,42 @@ export function makeExcerpt(content: string, offset: number): string {
}
return escapeHTML(content)
}

/**
* splits a string in words or "expressions in quotes"
* @param str
* @returns
*/
export function splitQuotes(str: string): string[] {
return str.match(/"(.*?)"/g)?.map(s => s.replace(/"/g, '')) ?? []
}

function mapAsync<T, U>(
array: T[],
callbackfn: (value: T, index: number, array: T[]) => Promise<U>,
): Promise<U[]> {
return Promise.all(array.map(callbackfn))
}

/**
* https://stackoverflow.com/a/53508547
* @param arr
* @param callback
* @returns
*/
export async function filterAsync<T>(
array: T[],
callbackfn: (value: T, index: number, array: T[]) => Promise<boolean>,
): Promise<T[]> {
const filterMap = await mapAsync(array, callbackfn)
return array.filter((value, index) => filterMap[index])
}

/**
* A simple function to strip bold and italic markdown chars from a string
* @param text
* @returns
*/
export function stripMarkdownCharacters(text: string): string {
return text.replace(/(\*|\_)+(.+?)(\*|\_)+/g, (match, p1, p2) => p2)
}

0 comments on commit cb7a11c

Please sign in to comment.