2021-09-08 05:26:31 -04:00
|
|
|
import _ from 'lodash'
|
|
|
|
import AlgoliaSearch from 'algoliasearch'
|
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
|
|
|
|
let wikiIdx
|
|
|
|
export async function searchWiki(...args) {
|
|
|
|
if (!wikiIdx) {
|
|
|
|
const algoliaConfig = getMeta('ol-algolia')
|
|
|
|
const wikiIndex = _.get(algoliaConfig, 'indexes.wiki')
|
|
|
|
if (wikiIndex) {
|
|
|
|
const client = AlgoliaSearch(algoliaConfig.appId, algoliaConfig.apiKey)
|
|
|
|
wikiIdx = client.initIndex(wikiIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!wikiIdx) {
|
|
|
|
return { hits: [], nbHits: 0, nbPages: 0 }
|
|
|
|
}
|
|
|
|
return wikiIdx.search(...args)
|
|
|
|
}
|
2021-09-15 10:06:32 -04:00
|
|
|
|
|
|
|
export function formatWikiHit(hit) {
|
|
|
|
const pageUnderscored = hit.pageName.replace(/\s/g, '_')
|
|
|
|
const pageSlug = encodeURIComponent(pageUnderscored)
|
|
|
|
const pagePath = hit.kb ? 'how-to' : 'latex'
|
|
|
|
|
|
|
|
let pageAnchor = ''
|
2024-08-13 07:37:15 -04:00
|
|
|
const rawPageName = hit._highlightResult.pageName.value
|
|
|
|
const sectionName = hit.sectionName
|
|
|
|
let pageName = rawPageName
|
|
|
|
if (sectionName) {
|
|
|
|
pageAnchor = `#${sectionName.replace(/\s/g, '_')}`
|
|
|
|
pageName += ' - ' + sectionName
|
2021-09-15 10:06:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const body = hit._highlightResult.content.value
|
|
|
|
const content = body
|
|
|
|
.split('\n')
|
|
|
|
.filter(line => line.includes('<em>') && !line.includes('[edit]'))
|
|
|
|
.join('\n...\n')
|
|
|
|
|
|
|
|
const url = `/learn/${pagePath}/${pageSlug}${pageAnchor}`
|
2024-08-13 07:37:15 -04:00
|
|
|
return { url, pageName, rawPageName, sectionName, content }
|
2021-09-15 10:06:32 -04:00
|
|
|
}
|