#141 - add first excerpt for each result in the public api

This commit is contained in:
Simon Cambier
2023-01-06 16:19:15 +01:00
parent b06153f5d9
commit a5b069d0c2

View File

@@ -1,6 +1,7 @@
import type { ResultNote } from '../globals'
import { Query } from '../search/query'
import { searchEngine } from '../search/omnisearch'
import { makeExcerpt } from './utils'
type ResultNoteApi = {
score: number
@@ -8,6 +9,7 @@ type ResultNoteApi = {
basename: string
foundWords: string[]
matches: SearchMatchApi[]
excerpt: string
}
export type SearchMatchApi = {
@@ -17,7 +19,10 @@ export type SearchMatchApi = {
function mapResults(results: ResultNote[]): ResultNoteApi[] {
return results.map(result => {
const { score, path, basename, foundWords, matches } = result
const { score, path, basename, foundWords, matches, content } = result
const excerpt = makeExcerpt(content, matches[0]?.offset ?? -1)
return {
score,
path,
@@ -29,14 +34,18 @@ function mapResults(results: ResultNote[]): ResultNoteApi[] {
offset: match.offset,
}
}),
excerpt,
}
})
}
async function search(q: string): Promise<ResultNoteApi[]> {
async function search(
q: string,
options: Partial<{ excerpt: boolean }> = {}
): Promise<ResultNoteApi[]> {
const query = new Query(q)
const raw = await searchEngine.getSuggestions(query)
return mapResults(raw)
}
export default {search}
export default { search }