Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: add pathNamespaces option to search plugin #1307

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
sy-records marked this conversation as resolved.
Show resolved Hide resolved

// 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))?/
}
}
</script>
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
'/de-de/': 'Suche',
'/zh-cn/': '搜索',
'/': 'Search'
}
},
pathNamespaces: ['/zh-cn', '/de-de', '/ru-ru', '/es']
},
plugins: [
function (hook, vm) {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const CONFIG = {
maxAge: 86400000, // 1 day
hideOtherSidebarContent: false,
namespace: undefined,
pathNamespaces: undefined,
};

const install = function(hook, vm) {
Expand All @@ -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';
Expand Down
30 changes: 22 additions & 8 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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++;
Expand Down