|
|
@@ -2,12 +2,44 @@ import _ from 'lodash'
|
|
|
import AlgoliaSearch from 'algoliasearch'
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
|
|
-let wikiIdx
|
|
|
-export async function searchWiki(...args) {
|
|
|
+interface WikiHit {
|
|
|
+ pageName: string
|
|
|
+ sectionName?: string
|
|
|
+ kb?: boolean
|
|
|
+ _highlightResult: {
|
|
|
+ pageName: {
|
|
|
+ value: string
|
|
|
+ }
|
|
|
+ content: {
|
|
|
+ value: string
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export interface FormattedWikiHit {
|
|
|
+ url: string
|
|
|
+ pageName: string
|
|
|
+ rawPageName: string
|
|
|
+ sectionName?: string
|
|
|
+ content: string
|
|
|
+}
|
|
|
+
|
|
|
+interface AlgoliaSearchResponse {
|
|
|
+ hits: WikiHit[]
|
|
|
+ nbHits: number
|
|
|
+ nbPages: number
|
|
|
+}
|
|
|
+
|
|
|
+let wikiIdx: AlgoliaSearch.Index | undefined
|
|
|
+
|
|
|
+export async function searchWiki(
|
|
|
+ query: string,
|
|
|
+ options?: AlgoliaSearch.QueryParameters
|
|
|
+): Promise<AlgoliaSearchResponse> {
|
|
|
if (!wikiIdx) {
|
|
|
const algoliaConfig = getMeta('ol-algolia')
|
|
|
const wikiIndex = _.get(algoliaConfig, 'indexes.wiki')
|
|
|
- if (wikiIndex) {
|
|
|
+ if (wikiIndex && algoliaConfig) {
|
|
|
const client = AlgoliaSearch(algoliaConfig.appId, algoliaConfig.apiKey)
|
|
|
wikiIdx = client.initIndex(wikiIndex)
|
|
|
}
|
|
|
@@ -15,10 +47,10 @@ export async function searchWiki(...args) {
|
|
|
if (!wikiIdx) {
|
|
|
return { hits: [], nbHits: 0, nbPages: 0 }
|
|
|
}
|
|
|
- return wikiIdx.search(...args)
|
|
|
+ return wikiIdx.search({ query, ...options }) as Promise<AlgoliaSearchResponse>
|
|
|
}
|
|
|
|
|
|
-export function formatWikiHit(hit) {
|
|
|
+export function formatWikiHit(hit: WikiHit): FormattedWikiHit {
|
|
|
const pageUnderscored = hit.pageName.replace(/\s/g, '_')
|
|
|
const pageSlug = encodeURIComponent(pageUnderscored)
|
|
|
const pagePath = hit.kb ? 'how-to' : 'latex'
|