mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
refactor(plugin-search): improve search index update (#884)
This commit is contained in:
parent
dbc6f0be0f
commit
d8b79e89e8
@ -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
|
||||
|
||||
@ -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)
|
||||
// },
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user