Skip to content

Commit

Permalink
fix: init flexsearch plugin: remove unsupported getExtraFields
Browse files Browse the repository at this point in the history
  • Loading branch information
svalaskevicius committed Mar 27, 2024
1 parent c0b632c commit 33f9b55
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 39 deletions.
25 changes: 0 additions & 25 deletions docs/plugins/flexsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,6 @@ export default {
}
```

### getExtraFields

- Type: `(page: Page) => string[]`

- Default: `() => []`

- Details:

A function to add extra fields to the search index of a page.

By default, this plugin will use page title and headers as the search index. This option could help you to add more searchable fields.

- Example:

```ts
export default {
plugins: [
searchPlugin({
// allow searching the `tags` frontmatter
getExtraFields: (page) => page.frontmatter.tags ?? [],
}),
],
}
```

## Styles

You can customize the style of the search box via CSS variables:
Expand Down
16 changes: 4 additions & 12 deletions plugins/plugin-flexsearch/src/node/flexsearchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,13 @@ export interface SearchPluginOptions {
* A function to determine whether a page should be included in the search index
*/
isSearchable?: (page: Page) => boolean

/**
* A function to add extra fields to the search index of a page
*/
getExtraFields?: (page: Page) => string[]
}

export const flexsearchPlugin = ({
locales = {},
hotKeys = ['s', '/'],
maxSuggestions = 5,
isSearchable = () => true,
getExtraFields = () => [],
}: SearchPluginOptions = {}): Plugin => ({
name: '@vuepress/plugin-flexsearch',

Expand All @@ -65,25 +59,23 @@ export const flexsearchPlugin = ({
},

onPrepared: async (app) => {
await prepareSearchIndex({ app, isSearchable, getExtraFields })
await prepareSearchIndex({ app, isSearchable })
},

onWatched: (app, watchers) => {
// here we only watch the page data files
// if the extra fields generated by `getExtraFields` are not included
// in the page data, the changes may not be watched
const searchIndexWatcher = chokidar.watch('pages/**/*.js', {
cwd: app.dir.temp(),
ignoreInitial: true,
})
searchIndexWatcher.on('add', () => {
prepareSearchIndex({ app, isSearchable, getExtraFields })
prepareSearchIndex({ app, isSearchable })
})
searchIndexWatcher.on('change', () => {
prepareSearchIndex({ app, isSearchable, getExtraFields })
prepareSearchIndex({ app, isSearchable })
})
searchIndexWatcher.on('unlink', () => {
prepareSearchIndex({ app, isSearchable, getExtraFields })
prepareSearchIndex({ app, isSearchable })
})
watchers.push(searchIndexWatcher)
},
Expand Down
2 changes: 0 additions & 2 deletions plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ const prepContent = (html: string): string => {
export const prepareSearchIndex = async ({
app,
isSearchable,
getExtraFields,
}: {
app: App
isSearchable: Required<SearchPluginOptions>['isSearchable']
getExtraFields: Required<SearchPluginOptions>['getExtraFields']
}): Promise<string> => {
// generate search index
const pages = app.pages.filter(isSearchable)
Expand Down

0 comments on commit 33f9b55

Please sign in to comment.