diff --git a/docs/configuration.md b/docs/configuration.md index 371ed35f6..1b2d04878 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -12,7 +12,7 @@ You can configure Docsify by defining `window.$docsify` as an object: ``` -The config can also be defined as a function, in which case the first arg is the Docsify `vm` instance. The function should return a config object. This can be useful for referencing `vm` in places like the markdown configuration: +The config can also be defined as a function, in which case the first argument is the Docsify `vm` instance. The function should return a config object. This can be useful for referencing `vm` in places like the markdown configuration: ```html 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++;