Skip to content

Commit cdb30e7

Browse files
authored
Merge pull request #935 from timlrx/update-kbar-docs
Update customize-kbar-search.md
2 parents ae747f9 + e618d08 commit cdb30e7

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

faq/customize-kbar-search.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const SearchProvider = ({ children }) => {
4545
keywords: post?.summary || '',
4646
section: 'Blog',
4747
subtitle: post.tags.join(', '),
48-
perform: () => router.push(post.path),
48+
perform: () => router.push('/' + post.path),
4949
}))
5050
},
5151
}}
@@ -55,3 +55,37 @@ export const SearchProvider = ({ children }) => {
5555
)
5656
}
5757
```
58+
59+
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:
60+
61+
```tsx
62+
function createSearchIndex(allBlogs) {
63+
if (
64+
siteMetadata?.search?.provider === 'kbar' &&
65+
siteMetadata.search.kbarConfig.searchDocumentsPath
66+
) {
67+
writeFileSync(
68+
`public/${siteMetadata.search.kbarConfig.searchDocumentsPath}`,
69+
JSON.stringify((sortPosts(allBlogs)))
70+
)
71+
console.log('Local search index generated...')
72+
}
73+
}
74+
```
75+
76+
Note the change from `JSON.stringify(allCoreContent(sortPosts(allBlogs)))` to `JSON.stringify((sortPosts(allBlogs)))`.
77+
78+
Next, in the modified `SearchProvider`, dump the raw content to the `keywords` field in the `onSearchDocumentsLoad` prop:
79+
80+
```tsx
81+
onSearchDocumentsLoad(json) {
82+
return json.map((post: Blog) => ({
83+
id: post.path,
84+
name: post.title,
85+
keywords: post.body.raw,
86+
section: 'Blog',
87+
subtitle: post.tags.join(', '),
88+
perform: () => router.push('/' + post.path),
89+
}))
90+
}
91+
```

0 commit comments

Comments
 (0)