mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
40a1c302f9
[web] de-ng learn wiki page GitOrigin-RevId: defb1c1c90fe17e843f36253e81c2455b7dddfb1
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
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)
|
|
}
|
|
|
|
export function formatWikiHit(hit) {
|
|
const pageUnderscored = hit.pageName.replace(/\s/g, '_')
|
|
const pageSlug = encodeURIComponent(pageUnderscored)
|
|
const pagePath = hit.kb ? 'how-to' : 'latex'
|
|
|
|
let pageAnchor = ''
|
|
let pageName = hit._highlightResult.pageName.value
|
|
if (hit.sectionName) {
|
|
pageAnchor = `#${hit.sectionName.replace(/\s/g, '_')}`
|
|
pageName += ' - ' + hit.sectionName
|
|
}
|
|
|
|
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}`
|
|
return { url, pageName, content }
|
|
}
|