feat(search-plugin): add namespace option (#706)
* feat(search-plugin): add namespace option * docs(search-plugin): update doc for option namespace
This commit is contained in:
parent
049726e11e
commit
28beff80f7
3 changed files with 29 additions and 7 deletions
|
|
@ -40,6 +40,10 @@ By default, the hyperlink on the current page is recognized and the content is s
|
||||||
depth: 2,
|
depth: 2,
|
||||||
|
|
||||||
hideOtherSidebarContent: false, // whether or not to hide other sidebar content
|
hideOtherSidebarContent: false, // whether or not to hide other sidebar content
|
||||||
|
|
||||||
|
// To avoid search index collision
|
||||||
|
// between multiple websites under the same domain
|
||||||
|
namespace: 'website-1',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,8 @@ const CONFIG = {
|
||||||
paths: 'auto',
|
paths: 'auto',
|
||||||
depth: 2,
|
depth: 2,
|
||||||
maxAge: 86400000, // 1 day
|
maxAge: 86400000, // 1 day
|
||||||
hideOtherSidebarContent: false
|
hideOtherSidebarContent: false,
|
||||||
|
namespace: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const install = function (hook, vm) {
|
const install = function (hook, vm) {
|
||||||
|
|
@ -23,6 +24,7 @@ const install = function (hook, vm) {
|
||||||
CONFIG.noData = opts.noData || CONFIG.noData
|
CONFIG.noData = opts.noData || CONFIG.noData
|
||||||
CONFIG.depth = opts.depth || CONFIG.depth
|
CONFIG.depth = opts.depth || CONFIG.depth
|
||||||
CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent
|
CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent
|
||||||
|
CONFIG.namespace = opts.namespace || CONFIG.namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAuto = CONFIG.paths === 'auto'
|
const isAuto = CONFIG.paths === 'auto'
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,17 @@
|
||||||
let INDEXS = {}
|
let INDEXS = {}
|
||||||
|
|
||||||
|
const LOCAL_STORAGE = {
|
||||||
|
EXPIRE_KEY: 'docsify.search.expires',
|
||||||
|
INDEX_KEY: 'docsify.search.index'
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveExpireKey(namespace) {
|
||||||
|
return namespace ? `${LOCAL_STORAGE.EXPIRE_KEY}/${namespace}` : LOCAL_STORAGE.EXPIRE_KEY
|
||||||
|
}
|
||||||
|
function resolveIndexKey(namespace) {
|
||||||
|
return namespace ? `${LOCAL_STORAGE.INDEX_KEY}/${namespace}` : LOCAL_STORAGE.INDEX_KEY
|
||||||
|
}
|
||||||
|
|
||||||
function escapeHtml(string) {
|
function escapeHtml(string) {
|
||||||
const entityMap = {
|
const entityMap = {
|
||||||
'&': '&',
|
'&': '&',
|
||||||
|
|
@ -33,9 +45,9 @@ function getAllPaths(router) {
|
||||||
return paths
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveData(maxAge) {
|
function saveData(maxAge, expireKey, indexKey) {
|
||||||
localStorage.setItem('docsify.search.expires', Date.now() + maxAge)
|
localStorage.setItem(expireKey, Date.now() + maxAge)
|
||||||
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
|
localStorage.setItem(indexKey, JSON.stringify(INDEXS))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function genIndex(path, content = '', router, depth) {
|
export function genIndex(path, content = '', router, depth) {
|
||||||
|
|
@ -149,9 +161,13 @@ export function search(query) {
|
||||||
|
|
||||||
export function init(config, vm) {
|
export function init(config, vm) {
|
||||||
const isAuto = config.paths === 'auto'
|
const isAuto = config.paths === 'auto'
|
||||||
const isExpired = localStorage.getItem('docsify.search.expires') < Date.now()
|
|
||||||
|
|
||||||
INDEXS = JSON.parse(localStorage.getItem('docsify.search.index'))
|
const expireKey = resolveExpireKey(config.namespace)
|
||||||
|
const indexKey = resolveIndexKey(config.namespace)
|
||||||
|
|
||||||
|
const isExpired = localStorage.getItem(expireKey) < Date.now()
|
||||||
|
|
||||||
|
INDEXS = JSON.parse(localStorage.getItem(indexKey))
|
||||||
|
|
||||||
if (isExpired) {
|
if (isExpired) {
|
||||||
INDEXS = {}
|
INDEXS = {}
|
||||||
|
|
@ -172,7 +188,7 @@ export function init(config, vm) {
|
||||||
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
|
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
|
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
|
||||||
len === ++count && saveData(config.maxAge)
|
len === ++count && saveData(config.maxAge, expireKey, indexKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue