Skip to content

Commit 28beff8

Browse files
cheng-kangQingWei-Li
authored andcommitted
feat(search-plugin): add namespace option (#706)
* feat(search-plugin): add namespace option * docs(search-plugin): update doc for option namespace
1 parent 049726e commit 28beff8

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

docs/plugins.md

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ By default, the hyperlink on the current page is recognized and the content is s
4040
depth: 2,
4141
4242
hideOtherSidebarContent: false, // whether or not to hide other sidebar content
43+
44+
// To avoid search index collision
45+
// between multiple websites under the same domain
46+
namespace: 'website-1',
4347
}
4448
}
4549
</script>

src/plugins/search/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const CONFIG = {
77
paths: 'auto',
88
depth: 2,
99
maxAge: 86400000, // 1 day
10-
hideOtherSidebarContent: false
10+
hideOtherSidebarContent: false,
11+
namespace: undefined
1112
}
1213

1314
const install = function (hook, vm) {
@@ -23,6 +24,7 @@ const install = function (hook, vm) {
2324
CONFIG.noData = opts.noData || CONFIG.noData
2425
CONFIG.depth = opts.depth || CONFIG.depth
2526
CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent
27+
CONFIG.namespace = opts.namespace || CONFIG.namespace
2628
}
2729

2830
const isAuto = CONFIG.paths === 'auto'

src/plugins/search/search.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
let INDEXS = {}
22

3+
const LOCAL_STORAGE = {
4+
EXPIRE_KEY: 'docsify.search.expires',
5+
INDEX_KEY: 'docsify.search.index'
6+
}
7+
8+
function resolveExpireKey(namespace) {
9+
return namespace ? `${LOCAL_STORAGE.EXPIRE_KEY}/${namespace}` : LOCAL_STORAGE.EXPIRE_KEY
10+
}
11+
function resolveIndexKey(namespace) {
12+
return namespace ? `${LOCAL_STORAGE.INDEX_KEY}/${namespace}` : LOCAL_STORAGE.INDEX_KEY
13+
}
14+
315
function escapeHtml(string) {
416
const entityMap = {
517
'&': '&amp;',
@@ -33,9 +45,9 @@ function getAllPaths(router) {
3345
return paths
3446
}
3547

36-
function saveData(maxAge) {
37-
localStorage.setItem('docsify.search.expires', Date.now() + maxAge)
38-
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
48+
function saveData(maxAge, expireKey, indexKey) {
49+
localStorage.setItem(expireKey, Date.now() + maxAge)
50+
localStorage.setItem(indexKey, JSON.stringify(INDEXS))
3951
}
4052

4153
export function genIndex(path, content = '', router, depth) {
@@ -149,9 +161,13 @@ export function search(query) {
149161

150162
export function init(config, vm) {
151163
const isAuto = config.paths === 'auto'
152-
const isExpired = localStorage.getItem('docsify.search.expires') < Date.now()
153164

154-
INDEXS = JSON.parse(localStorage.getItem('docsify.search.index'))
165+
const expireKey = resolveExpireKey(config.namespace)
166+
const indexKey = resolveIndexKey(config.namespace)
167+
168+
const isExpired = localStorage.getItem(expireKey) < Date.now()
169+
170+
INDEXS = JSON.parse(localStorage.getItem(indexKey))
155171

156172
if (isExpired) {
157173
INDEXS = {}
@@ -172,7 +188,7 @@ export function init(config, vm) {
172188
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
173189
.then(result => {
174190
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
175-
len === ++count && saveData(config.maxAge)
191+
len === ++count && saveData(config.maxAge, expireKey, indexKey)
176192
})
177193
})
178194
}

0 commit comments

Comments
 (0)