vuepress-plugin-next-search
Version:
适配vuepressV2的全文搜索(full-text-search)的插件
54 lines (51 loc) • 1.86 kB
text/typescript
import { Plugin } from '@vuepress/core'
import { LocaleOptions } from './client/core/types'
import { path } from '@vuepress/utils'
import * as chokidar from 'chokidar'
import { prepareSearchIndex } from './client/core/prepareSearchIndex'
import { useSassPalettePlugin } from 'vuepress-plugin-sass-palette'
const nextSearchPlugin: (options: LocaleOptions) => Plugin = (options) => {
return (app) => {
useSassPalettePlugin(app, {
id: 'ns',
defaultPalette: 'vuepress-plugin-next-search/src/client/styles/palette.scss',
defaultConfig: 'vuepress-plugin-next-search/src/client/styles/palette.scss',
})
const nextSearchOptions: LocaleOptions = {
fullText: options.fullText ?? true,
placeholder: options.placeholder ?? '搜索',
frontmatter: {
category: options.frontmatter?.category ?? '分类',
tag: options.frontmatter?.tag ?? '标签',
},
locales: options.locales,
}
return {
name: 'vuepress-plugin-next-search',
clientConfigFile: path.resolve(__dirname, './client/core/clientConfig.ts'),
define: {
__NEXT_SEARCH_OPTIONS__: nextSearchOptions,
},
onPrepared(app) {
prepareSearchIndex({ app }).then(() => {})
},
onWatched: (app, watchers) => {
const searchIndexWatcher = chokidar.watch('internal/pageData/*', {
cwd: app.dir.temp(),
ignoreInitial: true,
})
searchIndexWatcher.on('add', () => {
prepareSearchIndex({ app }).then(() => {})
})
searchIndexWatcher.on('change', () => {
prepareSearchIndex({ app }).then(() => {})
})
searchIndexWatcher.on('unlink', () => {
prepareSearchIndex({ app }).then(() => {})
})
watchers.push(searchIndexWatcher)
},
}
}
}
export { nextSearchPlugin }