Merge pull request #935 from timlrx/update-kbar-docs

Update customize-kbar-search.md
This commit is contained in:
Timothy 2024-06-08 23:19:51 +08:00 committed by GitHub
commit cdb30e757f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,7 +45,7 @@ export const SearchProvider = ({ children }) => {
keywords: post?.summary || '',
section: 'Blog',
subtitle: post.tags.join(', '),
perform: () => router.push(post.path),
perform: () => router.push('/' + post.path),
}))
},
}}
@ -55,3 +55,37 @@ export const SearchProvider = ({ children }) => {
)
}
```
You can even choose to do a full text search over the entire generated blog content though this would come at the expense of a larger search index file by modifying the `createSearchIndex` function in `contentlayer.config.ts` to:
```tsx
function createSearchIndex(allBlogs) {
if (
siteMetadata?.search?.provider === 'kbar' &&
siteMetadata.search.kbarConfig.searchDocumentsPath
) {
writeFileSync(
`public/${siteMetadata.search.kbarConfig.searchDocumentsPath}`,
JSON.stringify((sortPosts(allBlogs)))
)
console.log('Local search index generated...')
}
}
```
Note the change from `JSON.stringify(allCoreContent(sortPosts(allBlogs)))` to `JSON.stringify((sortPosts(allBlogs)))`.
Next, in the modified `SearchProvider`, dump the raw content to the `keywords` field in the `onSearchDocumentsLoad` prop:
```tsx
onSearchDocumentsLoad(json) {
return json.map((post: Blog) => ({
id: post.path,
name: post.title,
keywords: post.body.raw,
section: 'Blog',
subtitle: post.tags.join(', '),
perform: () => router.push('/' + post.path),
}))
}
```