Skip to content

feat: search add config indexes key prefix #553

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const CONFIG = {
noData: 'No Results!',
paths: 'auto',
depth: 2,
maxAge: 86400000 // 1 day
maxAge: 86400000, // 1 day
indexPrefix: ''
}

const install = function (hook, vm) {
Expand All @@ -21,6 +22,7 @@ const install = function (hook, vm) {
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder
CONFIG.noData = opts.noData || CONFIG.noData
CONFIG.depth = opts.depth || CONFIG.depth
CONFIG.indexPrefix = opts.indexPrefix || CONFIG.indexPrefix
}

const isAuto = CONFIG.paths === 'auto'
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,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(indexPrefix, maxAge) {
localStorage.setItem(indexPrefix + 'docsify.search.expires', Date.now() + maxAge)
localStorage.setItem(indexPrefix + 'docsify.search.index', JSON.stringify(INDEXS))
}

export function genIndex(path, content = '', router, depth) {
Expand Down Expand Up @@ -152,9 +152,9 @@ export function init(config, vm) {
helper = Docsify

const isAuto = config.paths === 'auto'
const isExpired = localStorage.getItem('docsify.search.expires') < Date.now()
const isExpired = localStorage.getItem(config.indexPrefix + 'docsify.search.expires') < Date.now()

INDEXS = JSON.parse(localStorage.getItem('docsify.search.index'))
INDEXS = JSON.parse(localStorage.getItem(config.indexPrefix + 'docsify.search.index'))

if (isExpired) {
INDEXS = {}
Expand All @@ -175,7 +175,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.indexPrefix, config.maxAge)
})
})
}