diff --git a/plugins/plugin-search/src/node/prepareSearchIndex.ts b/plugins/plugin-search/src/node/prepareSearchIndex.ts index a3ef91ac..63854925 100644 --- a/plugins/plugin-search/src/node/prepareSearchIndex.ts +++ b/plugins/plugin-search/src/node/prepareSearchIndex.ts @@ -31,6 +31,11 @@ export interface SearchIndexOptions { isSearchable: SearchPluginOptions['isSearchable'] } +interface UpdateSearchIndexOptions extends Omit { + /** 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 { - 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 { - 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 diff --git a/plugins/plugin-search/src/node/searchPlugin.ts b/plugins/plugin-search/src/node/searchPlugin.ts index 1c9c71ba..75ceef39 100644 --- a/plugins/plugin-search/src/node/searchPlugin.ts +++ b/plugins/plugin-search/src/node/searchPlugin.ts @@ -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) - // }, }) }