Skip to content

Commit

Permalink
Merge pull request #1307 from palkan/feat/search-path-namespaces
Browse files Browse the repository at this point in the history
FEAT: add pathNamespaces option to search plugin
  • Loading branch information
anikethsaha committed Jul 26, 2020
2 parents 78775b6 + d179dde commit aec03d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/plugins.md
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'],
// 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
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
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
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

1 comment on commit aec03d4

@vercel
Copy link

@vercel vercel bot commented on aec03d4 Jul 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.