Skip to content

Commit 23a4c68

Browse files
feat(plugin-docsearch): support indexBase option (close #1223) (#1224)
Co-authored-by: meteorlxy <[email protected]>
1 parent e5a48d2 commit 23a4c68

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

docs/reference/plugin/docsearch.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ export default {
316316
- Also see:
317317
- [Guide > I18n](../../guide/i18n.md)
318318

319+
### indexBase
320+
321+
- Type: `string`
322+
323+
- Default: [base](../config.md#base)
324+
325+
- Details:
326+
327+
The base path of the search index.
328+
329+
If you are deploying your site to multiple domains, you don't need to submit all of them to DocSearch and generate search index separately. You could choose one of the domains as the _index domain_, and only submit the _index domain_ to Docsearch for crawling search index. Then, you could reuse the search index across all deployments.
330+
331+
However, if the [base](../config.md#base) of your deployments are different for different domains, you need to set the option to the [base](../config.md#base) of your _index domain_, so that other deployments could reuse the search index correctly.
332+
319333
### injectStyles
320334

321335
- Type: `boolean`
@@ -327,7 +341,7 @@ export default {
327341
Whether to inject the default styles of DocSearch or not.
328342

329343
If you think the default styles of DocSearch is not compatible with your site, you can try to override the default styles, or set this option to `false` to totally exclude the default styles.
330-
344+
331345
When this option is disabled, you need to import your own styles for DocSearch. Also notice that all styles customization in [Styles](#styles) section would be unavailable.
332346

333347
## Styles

docs/zh/reference/plugin/docsearch.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,20 @@ export default {
315315
- 参考:
316316
- [指南 > 多语言支持](../../guide/i18n.md)
317317

318+
### indexBase
319+
320+
- 类型: `string`
321+
322+
- 默认值: [base](../config.md#base)
323+
324+
- 详情:
325+
326+
搜索索引基础路径。
327+
328+
如果你需要把你的站点部署到不同的域名上,你不需要把它们全都提交到 Docsearch 上来分别生成搜索索引。你可以选择其中一个域名作为 _索引域名_ ,并且仅将 _索引域名_ 提交到 DocSearch 上来爬去搜索索引。然后,你就可以在不同的部署域名下复用索引。
329+
330+
如果你不同部署域名下的 [base](../config.md#base) 是不一样的,你就需要将这个配置设置成 _索引域名_[base](../config.md#base) ,这样其他的部署域名就可以正确复用索引了。
331+
318332
### injectStyles
319333

320334
- 类型: `boolean`

ecosystem/plugin-docsearch/src/client/composables/useDocsearchShim.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { resolveRoutePathFromUrl } from '@vuepress/shared'
33
import { debounce } from 'ts-debounce'
44
import { useRouter } from 'vue-router'
55

6+
declare const __DOCSEARCH_INDEX_BASE__: string
7+
68
const isSpecialClick = (event: MouseEvent): boolean =>
79
event.button === 1 ||
810
event.altKey ||
@@ -32,7 +34,9 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
3234
return
3335
}
3436
event.preventDefault()
35-
router.push(resolveRoutePathFromUrl(hit.url, __VUEPRESS_BASE__))
37+
router.push(
38+
resolveRoutePathFromUrl(hit.url, __DOCSEARCH_INDEX_BASE__)
39+
)
3640
},
3741
children,
3842
},
@@ -43,7 +47,7 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
4347
navigator: {
4448
// when pressing Enter without metaKey
4549
navigate: ({ itemUrl }) => {
46-
router.push(resolveRoutePathFromUrl(itemUrl, __VUEPRESS_BASE__))
50+
router.push(resolveRoutePathFromUrl(itemUrl, __DOCSEARCH_INDEX_BASE__))
4751
},
4852
},
4953

ecosystem/plugin-docsearch/src/node/docsearchPlugin.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@ import type { DocsearchOptions } from '../shared/index.js'
44

55
const __dirname = getDirname(import.meta.url)
66

7+
/**
8+
* Options for @vuepress/plugin-docsearch
9+
*/
710
export interface DocsearchPluginOptions extends DocsearchOptions {
11+
/**
12+
* Base path of the search index
13+
*/
14+
indexBase?: string
15+
16+
/**
17+
* Whether to inject docsearch default styles
18+
*/
819
injectStyles?: boolean
920
}
1021

1122
export const docsearchPlugin = ({
1223
injectStyles = true,
24+
indexBase,
1325
...options
1426
}: DocsearchPluginOptions): Plugin => ({
1527
name: '@vuepress/plugin-docsearch',
1628

1729
clientConfigFile: path.resolve(__dirname, '../client/config.js'),
1830

19-
define: {
20-
__DOCSEARCH_OPTIONS__: options,
31+
define: (app) => ({
2132
__DOCSEARCH_INJECT_STYLES__: injectStyles,
22-
},
33+
__DOCSEARCH_INDEX_BASE__: indexBase || app.options.base,
34+
__DOCSEARCH_OPTIONS__: options,
35+
}),
2336
})

0 commit comments

Comments
 (0)