refactor(plugin-search): improve search index update (#884)

This commit is contained in:
pengzhanbo 2026-04-03 01:58:25 +08:00 committed by GitHub
parent dbc6f0be0f
commit d8b79e89e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 39 deletions

View File

@ -31,6 +31,11 @@ export interface SearchIndexOptions {
isSearchable: SearchPluginOptions['isSearchable']
}
interface UpdateSearchIndexOptions extends Omit<SearchIndexOptions, 'app'> {
/** VuePress page instance / VuePress 页面实例 */
page: Page
}
/**
* Internal index object structure for MiniSearch.
*
@ -172,22 +177,18 @@ export async function prepareSearchIndex({
* @param options.searchOptions - MiniSearch configuration / MiniSearch
*/
export async function onSearchIndexUpdated(
filepath: string,
app: App,
{
app,
page,
isSearchable,
searchOptions,
}: SearchIndexOptions,
}: UpdateSearchIndexOptions,
): Promise<void> {
const pages = isSearchable ? app.pages.filter(isSearchable) : app.pages
if (pages.some(p => p.filePathRelative?.endsWith(filepath))) {
await indexFile(
app.pages.find(p => p.filePathRelative?.endsWith(filepath))!,
searchOptions,
isSearchable,
)
await writeTemp(app)
}
if (isSearchable && !isSearchable(page))
return
await indexFile(page, searchOptions, isSearchable)
await writeTemp(app)
}
/**
@ -202,16 +203,17 @@ export async function onSearchIndexUpdated(
* @param options.searchOptions - MiniSearch configuration / MiniSearch
*/
export async function onSearchIndexRemoved(
filepath: string,
app: App,
{
app,
page,
isSearchable,
searchOptions,
}: SearchIndexOptions,
}: UpdateSearchIndexOptions,
): Promise<void> {
const pages = isSearchable ? app.pages.filter(isSearchable) : app.pages
if (pages.some(p => p.filePathRelative?.endsWith(filepath))) {
const page = app.pages.find(p => p.filePathRelative?.endsWith(filepath))!
if (isSearchable && !isSearchable(page))
return
if (page.filePathRelative) {
const fileId = page.path
const locale = page.pathLocale
const lang = page.lang

View File

@ -71,34 +71,16 @@ export function searchPlugin({
}
},
onPageUpdated: (app, type, page) => {
onPageUpdated: async (app, type, page) => {
if (!page?.filePathRelative)
return
if (type === 'create' || type === 'update') {
onSearchIndexUpdated(page?.filePathRelative, { app, isSearchable, searchOptions })
await onSearchIndexUpdated(app, { page, isSearchable, searchOptions })
}
else if (type === 'delete') {
onSearchIndexRemoved(page?.filePathRelative, { app, isSearchable, searchOptions })
await onSearchIndexRemoved(app, { page, isSearchable, searchOptions })
}
},
// onWatched: (app, watchers) => {
// const searchIndexWatcher = chokidar.watch('pages', {
// cwd: app.dir.temp(),
// ignoreInitial: true,
// ignored: (filepath, stats) => Boolean(stats?.isFile()) && !filepath.endsWith('.js'),
// })
// searchIndexWatcher.on('add', (filepath) => {
// onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions })
// })
// searchIndexWatcher.on('change', (filepath) => {
// onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions })
// })
// searchIndexWatcher.on('unlink', (filepath) => {
// onSearchIndexRemoved(filepath, { app, isSearchable, searchOptions })
// })
// watchers.push(searchIndexWatcher)
// },
})
}