#141 - add first excerpt for each result in the public api
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import type { ResultNote } from '../globals'
|
import type { ResultNote } from '../globals'
|
||||||
import { Query } from '../search/query'
|
import { Query } from '../search/query'
|
||||||
import { searchEngine } from '../search/omnisearch'
|
import { searchEngine } from '../search/omnisearch'
|
||||||
|
import { makeExcerpt } from './utils'
|
||||||
|
|
||||||
type ResultNoteApi = {
|
type ResultNoteApi = {
|
||||||
score: number
|
score: number
|
||||||
@@ -8,6 +9,7 @@ type ResultNoteApi = {
|
|||||||
basename: string
|
basename: string
|
||||||
foundWords: string[]
|
foundWords: string[]
|
||||||
matches: SearchMatchApi[]
|
matches: SearchMatchApi[]
|
||||||
|
excerpt: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SearchMatchApi = {
|
export type SearchMatchApi = {
|
||||||
@@ -17,7 +19,10 @@ export type SearchMatchApi = {
|
|||||||
|
|
||||||
function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
||||||
return results.map(result => {
|
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 {
|
return {
|
||||||
score,
|
score,
|
||||||
path,
|
path,
|
||||||
@@ -29,14 +34,18 @@ function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
|||||||
offset: match.offset,
|
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 query = new Query(q)
|
||||||
const raw = await searchEngine.getSuggestions(query)
|
const raw = await searchEngine.getSuggestions(query)
|
||||||
return mapResults(raw)
|
return mapResults(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {search}
|
export default { search }
|
||||||
|
|||||||
Reference in New Issue
Block a user