|
|
|
@ -1,5 +1,17 @@ |
|
|
|
|
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) { |
|
|
|
|
const entityMap = { |
|
|
|
|
'&': '&', |
|
|
|
@ -33,9 +45,9 @@ function getAllPaths(router) { |
|
|
|
|
return paths |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function saveData(maxAge) { |
|
|
|
|
localStorage.setItem('docsify.search.expires', Date.now() + maxAge) |
|
|
|
|
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS)) |
|
|
|
|
function saveData(maxAge, expireKey, indexKey) { |
|
|
|
|
localStorage.setItem(expireKey, Date.now() + maxAge) |
|
|
|
|
localStorage.setItem(indexKey, JSON.stringify(INDEXS)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function genIndex(path, content = '', router, depth) { |
|
|
|
@ -149,9 +161,13 @@ export function search(query) { |
|
|
|
|
|
|
|
|
|
export function init(config, vm) { |
|
|
|
|
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) { |
|
|
|
|
INDEXS = {} |
|
|
|
@ -172,7 +188,7 @@ export function init(config, vm) { |
|
|
|
|
.get(vm.router.getFile(path), false, vm.config.requestHeaders) |
|
|
|
|
.then(result => { |
|
|
|
|
INDEXS[path] = genIndex(path, result, vm.router, config.depth) |
|
|
|
|
len === ++count && saveData(config.maxAge) |
|
|
|
|
len === ++count && saveData(config.maxAge, expireKey, indexKey) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|