Skip to content

Commit

Permalink
fix(search): ready event is not fired on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Apr 17, 2023
1 parent 67f1f91 commit e37e5cb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
4 changes: 0 additions & 4 deletions docs/reference/default-theme-search.md
Expand Up @@ -20,10 +20,6 @@ Example result:

![screenshot of the search modal](/search.png)

::: info
On dev server, the search index is generated on the fly and contains only the recently edited files. So don't worry if you don't see any results when you first open the search modal. It should work fine in production.
:::

Alternatively, you can use [Algolia DocSearch](#algolia-search) or some community plugins like <https://www.npmjs.com/package/vitepress-plugin-search> or <https://www.npmjs.com/package/vitepress-plugin-pagefind>.

### i18n {#local-search-i18n}
Expand Down
22 changes: 5 additions & 17 deletions src/node/plugins/localSearchPlugin.ts
Expand Up @@ -72,7 +72,7 @@ export async function localSearchPlugin(

let server: ViteDevServer | undefined

async function onIndexUpdated() {
function onIndexUpdated() {
if (server) {
server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH)
// HMR
Expand Down Expand Up @@ -146,22 +146,10 @@ export async function localSearchPlugin(
return {
name: 'vitepress:local-search',

configureServer(_server) {
async configureServer(_server) {
server = _server

server.watcher.on('ready', async () => {
const watched = server!.watcher.getWatched()
const files = Object.keys(watched).reduce((acc, dir) => {
acc.push(
...watched[dir]
.map((file) => dir + '/' + file)
.filter((file) => file.endsWith('.md'))
)
return acc
}, [] as string[])
await indexAllFiles(files)
onIndexUpdated()
})
await scanForBuild()
onIndexUpdated()
},

resolveId(id) {
Expand Down Expand Up @@ -203,7 +191,7 @@ export async function localSearchPlugin(
}
const index = getIndexForPath(ctx.file)
const sections = splitPageIntoSections(
await md.render(await fs.readFile(ctx.file, 'utf-8'))
md.render(await fs.readFile(ctx.file, 'utf-8'))
)
for (const section of sections) {
const id = `${fileId}#${section.anchor}`
Expand Down

0 comments on commit e37e5cb

Please sign in to comment.