feat(dev): Re-implemented search
This commit is contained in:
parent
483daa10d7
commit
b3b5d225b8
10 changed files with 230 additions and 236 deletions
|
@ -1,10 +1,8 @@
|
|||
/*
|
||||
* This will update (replace really) the Algolia index with the
|
||||
* current website contents. Or at least the markdown and Strapi
|
||||
* content
|
||||
* current website contents. Or at least the markdown content.
|
||||
*
|
||||
* It expects the following environment vars to be set in a
|
||||
* .env file in the 'sites/dev' folder:
|
||||
* It expects the following environment vars to be set:
|
||||
*
|
||||
* ALGOLIA_API_WRITE_KEY -> Needs permission to index/create/delete
|
||||
*
|
||||
|
@ -23,15 +21,18 @@ import remarkRehype from 'remark-rehype'
|
|||
import rehypeSanitize from 'rehype-sanitize'
|
||||
import rehypeStringify from 'rehype-stringify'
|
||||
import yaml from 'yaml'
|
||||
import { getMdxFileList } from '../../shared/prebuild/mdx.mjs'
|
||||
import config from '../algolia.config.mjs'
|
||||
import { getMdxFileList } from '../../shared/prebuild/docs.mjs'
|
||||
import { siteConfig } from '../site.config.mjs'
|
||||
import { compile } from 'html-to-text'
|
||||
dotenv.config()
|
||||
|
||||
const convert = compile()
|
||||
|
||||
/*
|
||||
* Initialize Algolia client
|
||||
*/
|
||||
const client = algoliasearch(config.algolia.app, process.env.ALGOLIA_API_WRITE_KEY)
|
||||
const index = client.initIndex(config.algolia.index)
|
||||
const client = algoliasearch(siteConfig.algolia.app, process.env.ALGOLIA_API_WRITE_KEY)
|
||||
const index = client.initIndex(siteConfig.algolia.index)
|
||||
|
||||
/*
|
||||
* Loads markdown from disk and compiles it into HTML for indexing
|
||||
|
@ -65,9 +66,21 @@ const markdownLoader = async (file) => {
|
|||
*/
|
||||
const clearIndex = async () => {
|
||||
console.log(`🗑️ Clearing index`)
|
||||
await index.clearObjects()
|
||||
try {
|
||||
await index.clearObjects()
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}
|
||||
|
||||
const splitContent = (content) =>
|
||||
content.body.split('<h2>').map((chunk, i) => ({
|
||||
...content,
|
||||
objectID: `${content.objectID}-${i}`,
|
||||
title: content.title + ' / ' + chunk.split('</h2>')[0],
|
||||
body: convert(chunk.split('</h2>').slice(1).join('</h2>')),
|
||||
}))
|
||||
|
||||
/*
|
||||
* Get and index markdown content
|
||||
*/
|
||||
|
@ -82,9 +95,17 @@ const indexMarkdownContent = async () => {
|
|||
const list = await getMdxFileList(mdxRoot, 'en')
|
||||
const pages = []
|
||||
|
||||
// max page size
|
||||
const MAX = 1500
|
||||
|
||||
for (const file of list) {
|
||||
const content = await markdownLoader(file)
|
||||
pages.push(content)
|
||||
if (content.body.length < MAX)
|
||||
pages.push({
|
||||
...content,
|
||||
body: convert(content.body),
|
||||
})
|
||||
else pages.push(...splitContent(content))
|
||||
}
|
||||
// Index markdown to Algolia
|
||||
await index.clearObjects()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue