From 9fee5542cb4bd0b83ccad5d625cb4eca8f8abb25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=83=BD=E5=AE=81?= Date: Wed, 5 Jul 2023 14:51:17 +0800 Subject: [PATCH] feat(search): support `minisearch` customization (#2576) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- docs/reference/default-theme-search.md | 36 +++++++++++++++++++ .../components/VPLocalSearchBox.vue | 34 +++++++++++++----- src/node/plugins/localSearchPlugin.ts | 16 +++++---- types/default-theme.d.ts | 17 ++++++++- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/docs/reference/default-theme-search.md b/docs/reference/default-theme-search.md index 6f5925ca..13f5ded0 100644 --- a/docs/reference/default-theme-search.md +++ b/docs/reference/default-theme-search.md @@ -1,3 +1,7 @@ +--- +outline: deep +--- + # Search ## Local Search @@ -58,6 +62,38 @@ export default defineConfig({ }) ``` +### miniSearch options + +You can configure MiniSearch like this: + +```ts +import { defineConfig } from 'vitepress' + +export default defineConfig({ + themeConfig: { + search: { + provider: 'local', + options: { + miniSearch: { + /** + * @type {Pick} + */ + options: { /* ... */ }, + /** + * @type {import('minisearch').SearchOptions} + * @default + * { fuzzy: 0.2, prefix: true, boost: { title: 4, text: 2, titles: 1 } } + */ + searchOptions: { /* ... */ } + } + } + } + } +}) +``` + +Learn more in [MiniSearch docs](https://lucaong.github.io/minisearch/classes/_minisearch_.minisearch.html). + ## Algolia Search VitePress supports searching your docs site using [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Refer their getting started guide. In your `.vitepress/config.ts` you'll need to provide at least the following to make it work: diff --git a/src/client/theme-default/components/VPLocalSearchBox.vue b/src/client/theme-default/components/VPLocalSearchBox.vue index 5614e336..0e1eddac 100644 --- a/src/client/theme-default/components/VPLocalSearchBox.vue +++ b/src/client/theme-default/components/VPLocalSearchBox.vue @@ -80,8 +80,12 @@ const searchIndex = computedAsync(async () => searchOptions: { fuzzy: 0.2, prefix: true, - boost: { title: 4, text: 2, titles: 1 } - } + boost: { title: 4, text: 2, titles: 1 }, + ...(theme.value.search?.provider === 'local' && + theme.value.search.options?.miniSearch?.searchOptions) + }, + ...(theme.value.search?.provider === 'local' && + theme.value.search.options?.miniSearch?.options) } ) ) @@ -396,8 +400,16 @@ function formMarkRegex(terms: Set) {
-