diff --git a/package.json b/package.json index 445a7e5f6..80964b3f4 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@vuepress/plugin-redirect": "2.0.0-rc.39", "@vuepress/plugin-shiki": "2.0.0-rc.39", "@vueuse/core": "10.11.0", + "algoliasearch": "^5.15.0", "element-plus": "2.4.3", "eslint": "8.55.0", "eslint-plugin-vue": "9.19.2", diff --git a/src/.vuepress/components/PageFooter.vue b/src/.vuepress/components/PageFooter.vue index 54b7b5581..df58dbacd 100644 --- a/src/.vuepress/components/PageFooter.vue +++ b/src/.vuepress/components/PageFooter.vue @@ -28,22 +28,11 @@ diff --git a/src/.vuepress/components/docsearch/client/components/Docsearch.ts b/src/.vuepress/components/docsearch/client/components/Docsearch.ts index e02b4fa21..86936a2de 100644 --- a/src/.vuepress/components/docsearch/client/components/Docsearch.ts +++ b/src/.vuepress/components/docsearch/client/components/Docsearch.ts @@ -1,5 +1,6 @@ /* eslint-disable no-underscore-dangle */ /* eslint-disable @typescript-eslint/naming-convention */ +import type { SearchParamsObject } from 'algoliasearch'; import type { PropType } from 'vue'; import { computed, defineComponent, h, onMounted, ref, watch, @@ -18,6 +19,8 @@ import { preconnectToAlgolia, } from '../utils/index.js'; +import { getDocVersion } from '../../../../utils/version.js'; + declare const __DOCSEARCH_INJECT_STYLES__: boolean; const defaultBranch = 'latest'; @@ -50,26 +53,14 @@ export const Docsearch = defineComponent({ const hasInitialized = ref(false); const hasTriggered = ref(false); - const getDocVersion = (branch = 'latest', path = '') => { - if (path.indexOf('UserGuide/Master') > -1 || path.indexOf('UserGuide') === -1) { - return branch; - } - const branchRex = /UserGuide\/V(\d+\.\d+\.x)/; - if (branchRex.test(path)) { - const tag = branchRex.exec(path)![1]; - return `rel/${tag.replace('.x', '')}`; - } - return branch; - }; - const version = computed(() => getDocVersion(defaultBranch, pageData.value.path)); // resolve docsearch options for current locale const options = computed(() => { - const { locales = {}, ...options } = props.options; + const { locales = {}, ...rest } = props.options; return { ...docSearchOptions.value, - ...options, + ...rest, ...locales[routeLocale.value], }; }); @@ -79,14 +70,17 @@ export const Docsearch = defineComponent({ */ const initialize = async (): Promise => { const { default: docsearch } = await import('@docsearch/js'); + + const { indexName, searchParameters } = options.value; docsearch({ ...docsearchShim, ...options.value, container: `#${props.containerId}`, searchParameters: { - ...options.value.searchParameters, + ...searchParameters, + indexName, facetFilters: getFacetFilters( - options.value.searchParameters?.facetFilters, + (searchParameters as SearchParamsObject | undefined)?.facetFilters, lang.value, version.value, ), diff --git a/src/.vuepress/components/docsearch/client/composables/useDocsearchShim.ts b/src/.vuepress/components/docsearch/client/composables/useDocsearchShim.ts index a8f33b03c..d4c4c6874 100644 --- a/src/.vuepress/components/docsearch/client/composables/useDocsearchShim.ts +++ b/src/.vuepress/components/docsearch/client/composables/useDocsearchShim.ts @@ -57,7 +57,7 @@ export const useDocsearchShim = (): Partial => { navigator: { // when pressing Enter without metaKey navigate: ({ itemUrl }) => { - router.push(itemUrl); + router.push(itemUrl.replace(__VUEPRESS_BASE__, '/')); }, }, diff --git a/src/.vuepress/utils/version.ts b/src/.vuepress/utils/version.ts new file mode 100644 index 000000000..73126cc89 --- /dev/null +++ b/src/.vuepress/utils/version.ts @@ -0,0 +1,41 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +const getDocVersion = (defaultValue = 'latest', path = '') => { + if (path.indexOf('UserGuide/Master') > -1 || path.indexOf('UserGuide') === -1) { + return defaultValue; + } + /** + * 路径 /zh/UserGuide/V1.3.0-2/QuickStart/QuickStart_apache.html, 匹配 V1.3.0-2 + * 路径 /zh/UserGuide/V1.2.x/QuickStart/QuickStart_apache.html, 匹配 V1.2.x + * 路径 /zh/UserGuide/latest/QuickStart/QuickStart_apache.html, 匹配 latest + * + * 匹配路径中的版本号,UserGuide 后面的版本号为当前文档的版本号, 版本号不一定为数字,可能为 latest或其它,因此只用 / 作为分隔符 + */ + // eslint-disable-next-line no-useless-escape + const versionRex = /UserGuide\/([^\/]+)/; + + if (versionRex.test(path)) { + const tag = versionRex.exec(path)![1]; + return tag; + } + return defaultValue; +}; + +export { getDocVersion };