Skip to content

[DO NOT MERGE] [Issue #486] Local storage is now stored per hostname (for subdomains) #496

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
wants to merge 1 commit into from
Closed
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
46 changes: 36 additions & 10 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,27 @@ function getAllPaths(router) {
return paths
}

function getAllLocalStorageData() {
const strData = localStorage.getItem('docsify.search');
return strData ? JSON.parse(strData) : {};
}

function getLocalStorageData() {
const data = getAllLocalStorageData();
return data[window.location.hostname] || {};
}

function setLocalStorageData(hostnameData) {
const data = getAllLocalStorageData();
data[window.location.hostname] = hostnameData;
localStorage.setItem('docsify.search', JSON.stringify(data));
}

function saveData(maxAge) {
localStorage.setItem('docsify.search.expires', Date.now() + maxAge)
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
setLocalStorageData({
expiration: Date.now() + maxAge,
index: INDEXS,
});
}

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

const isAuto = config.paths === 'auto'
const isExpired = localStorage.getItem('docsify.search.expires') < Date.now()
const lsData = getLocalStorageData();
const isExpired = (!lsData.expiration
|| (lsData.expiration < Date.now()));

INDEXS = JSON.parse(localStorage.getItem('docsify.search.index'))
INDEXS = lsData.index || {};

if (isExpired) {
INDEXS = {}
} else if (!isAuto) {
return
}

const paths = isAuto ? getAllPaths(vm.router) : config.paths
let paths = isAuto ? getAllPaths(vm.router) : config.paths
paths = paths.filter(path => !(path in INDEXS))

const len = paths.length
let count = 0
let indexChanged = false

paths.forEach(path => {
if (INDEXS[path]) {
return count++
const updateCount = () => {
if ((len === ++count) && indexChanged) {
saveData(config.maxAge)
}
}

paths.forEach(path => {
helper
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
.then(result => {
indexChanged = true
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
updateCount()
}, updateCount)
})
}