From a5b069d0c289f13465d728202a13c94abf01b065 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Fri, 6 Jan 2023 16:19:15 +0100 Subject: [PATCH] #141 - add first excerpt for each result in the public api --- src/tools/api.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/tools/api.ts b/src/tools/api.ts index 7539c93..577e90e 100644 --- a/src/tools/api.ts +++ b/src/tools/api.ts @@ -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 { +async function search( + q: string, + options: Partial<{ excerpt: boolean }> = {} +): Promise { const query = new Query(q) const raw = await searchEngine.getSuggestions(query) return mapResults(raw) } -export default {search} +export default { search }