diff --git a/docs/plugins.md b/docs/plugins.md index 51acefbb5..feca8b551 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -44,6 +44,17 @@ By default, the hyperlink on the current page is recognized and the content is s // To avoid search index collision // between multiple websites under the same domain namespace: 'website-1', + + // Use different indexes for path prefixes (namespaces). + // NOTE: Only works in 'auto' mode. + // + // When initialiazing an index, we look for the first path from the sidebar. + // If it matches the prefix from the list, we switch to the corresponding index. + pathNamespaces: ['/zh-cn', '/ru-ru', '/ru-ru/v1'], + + // You can provide a regexp to match prefixes. In this case, + // the matching substring will be used to identify the index + pathNamespaces: /^(\/(zh-cn|ru-ru))?(\/(v1|v2))?/ } } diff --git a/index.html b/index.html index 9c950964d..ddacdecda 100644 --- a/index.html +++ b/index.html @@ -51,7 +51,8 @@ '/de-de/': 'Suche', '/zh-cn/': '搜索', '/': 'Search' - } + }, + pathNamespaces: ['/zh-cn', '/de-de', '/ru-ru', '/es'] }, plugins: [ function (hook, vm) { diff --git a/src/plugins/search/index.js b/src/plugins/search/index.js index 54d8a79c7..e97c3b026 100644 --- a/src/plugins/search/index.js +++ b/src/plugins/search/index.js @@ -10,6 +10,7 @@ const CONFIG = { maxAge: 86400000, // 1 day hideOtherSidebarContent: false, namespace: undefined, + pathNamespaces: undefined, }; const install = function(hook, vm) { @@ -27,6 +28,7 @@ const install = function(hook, vm) { CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent; CONFIG.namespace = opts.namespace || CONFIG.namespace; + CONFIG.pathNamespaces = opts.pathNamespaces || CONFIG.pathNamespaces; } const isAuto = CONFIG.paths === 'auto'; diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 093904b0e..6a212c016 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -198,9 +198,29 @@ export function search(query) { export function init(config, vm) { const isAuto = config.paths === 'auto'; + const paths = isAuto ? getAllPaths(vm.router) : config.paths; + + let namespaceSuffix = ''; + + // only in auto mode + if (isAuto && config.pathNamespaces) { + const path = paths[0]; + + if (Array.isArray(config.pathNamespaces)) { + namespaceSuffix = + config.pathNamespaces.find(prefix => path.startsWith(prefix)) || + namespaceSuffix; + } else if (config.pathNamespaces instanceof RegExp) { + const matches = path.match(config.pathNamespaces); + + if (matches) { + namespaceSuffix = matches[0]; + } + } + } - const expireKey = resolveExpireKey(config.namespace); - const indexKey = resolveIndexKey(config.namespace); + const expireKey = resolveExpireKey(config.namespace) + namespaceSuffix; + const indexKey = resolveIndexKey(config.namespace) + namespaceSuffix; const isExpired = localStorage.getItem(expireKey) < Date.now(); @@ -212,15 +232,9 @@ export function init(config, vm) { return; } - const paths = isAuto ? getAllPaths(vm.router) : config.paths; const len = paths.length; let count = 0; - // Fix search error when exist translations documents - if (INDEXS !== null && !INDEXS[paths[0]]) { - INDEXS = {}; - } - paths.forEach(path => { if (INDEXS[path]) { return count++;