Skip to content

Commit 5568470

Browse files
committed
feat(search): add pathNamespaces option
This option allows to dynamically choose the index path depending on the path prefixes in auto mode. Thus, different path namespace could avoid index intersection (e.g., when having multiple locales)
1 parent 78775b6 commit 5568470

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

docs/plugins.md

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ By default, the hyperlink on the current page is recognized and the content is s
4444
// To avoid search index collision
4545
// between multiple websites under the same domain
4646
namespace: 'website-1',
47+
48+
// Use different indexes for path prefixes (namespaces).
49+
// NOTE: Only works in 'auto' mode.
50+
//
51+
// When initialiazing an index, we look for the first path from the sidebar.
52+
// If it matches the prefix from the list, we switch to the corresponding index.
53+
pathNamespaces: ['/zh-cn', '/ru-ru', '/ru-ru/v1'],
54+
55+
// You can provide a regexp to match prefixes. In this case,
56+
// the matching substring will be used to identify the index
57+
pathNamespaces: /^(\/(zh-cn|ru-ru))?(\/(v1|v2))?/
4758
}
4859
}
4960
</script>

src/plugins/search/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const CONFIG = {
1010
maxAge: 86400000, // 1 day
1111
hideOtherSidebarContent: false,
1212
namespace: undefined,
13+
pathNamespaces: undefined,
1314
};
1415

1516
const install = function(hook, vm) {
@@ -27,6 +28,7 @@ const install = function(hook, vm) {
2728
CONFIG.hideOtherSidebarContent =
2829
opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent;
2930
CONFIG.namespace = opts.namespace || CONFIG.namespace;
31+
CONFIG.pathNamespaces = opts.pathNamespaces || CONFIG.pathNamespaces;
3032
}
3133

3234
const isAuto = CONFIG.paths === 'auto';

src/plugins/search/search.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,29 @@ export function search(query) {
198198

199199
export function init(config, vm) {
200200
const isAuto = config.paths === 'auto';
201+
const paths = isAuto ? getAllPaths(vm.router) : config.paths;
202+
203+
let namespaceSuffix = '';
204+
205+
// only in auto mode
206+
if (isAuto && config.pathNamespaces) {
207+
const path = paths[0];
208+
209+
if (Array.isArray(config.pathNamespaces)) {
210+
namespaceSuffix =
211+
config.pathNamespaces.find(prefix => path.startsWith(prefix)) ||
212+
namespaceSuffix;
213+
} else if (config.pathNamespaces instanceof RegExp) {
214+
const matches = path.match(config.pathNamespaces);
215+
216+
if (matches) {
217+
namespaceSuffix = matches[0];
218+
}
219+
}
220+
}
201221

202-
const expireKey = resolveExpireKey(config.namespace);
203-
const indexKey = resolveIndexKey(config.namespace);
222+
const expireKey = resolveExpireKey(config.namespace) + namespaceSuffix;
223+
const indexKey = resolveIndexKey(config.namespacem) + namespaceSuffix;
204224

205225
const isExpired = localStorage.getItem(expireKey) < Date.now();
206226

@@ -212,15 +232,9 @@ export function init(config, vm) {
212232
return;
213233
}
214234

215-
const paths = isAuto ? getAllPaths(vm.router) : config.paths;
216235
const len = paths.length;
217236
let count = 0;
218237

219-
// Fix search error when exist translations documents
220-
if (INDEXS !== null && !INDEXS[paths[0]]) {
221-
INDEXS = {};
222-
}
223-
224238
paths.forEach(path => {
225239
if (INDEXS[path]) {
226240
return count++;

0 commit comments

Comments
 (0)