Skip to content

Commit

Permalink
feat(search): use dexie.js instead of localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Jul 14, 2024
1 parent 90c0b02 commit caedbd5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"*.js": "eslint --fix"
},
"dependencies": {
"dexie": "^4.0.8",
"medium-zoom": "^1.1.0",
"opencollective-postinstall": "^2.0.2",
"prismjs": "^1.29.0",
Expand Down
33 changes: 23 additions & 10 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@ import {
getAndRemoveConfig,
getAndRemoveDocisfyIgnoreConfig,
} from '../../core/render/utils.js';
import Dexie from 'dexie';

let INDEXS = {};

const db = new Dexie("DocsifySearchDB");
db.version(1).stores({
search: "key, value"
});

async function saveData(maxAge, expireKey, indexKey) {
await db.search.put({ key: expireKey, value: Date.now() + maxAge });
await db.search.put({ key: indexKey, value: JSON.stringify(INDEXS) });
}

async function getData(key) {
const item = await db.search.get(key);
return item ? item.value : null;
}

const LOCAL_STORAGE = {
EXPIRE_KEY: 'docsify.search.expires',
INDEX_KEY: 'docsify.search.index',
Expand Down Expand Up @@ -73,11 +89,6 @@ function getListData(token) {
return token.text;
}

function saveData(maxAge, expireKey, indexKey) {
localStorage.setItem(expireKey, Date.now() + maxAge);
localStorage.setItem(indexKey, JSON.stringify(INDEXS));
}

export function genIndex(path, content = '', router, depth) {
const tokens = window.marked.lexer(content);
const slugify = window.Docsify.slugify;
Expand Down Expand Up @@ -240,7 +251,7 @@ export function search(query) {
return matchingResults.sort((r1, r2) => r2.score - r1.score);
}

export function init(config, vm) {
export async function init(config, vm) {
const isAuto = config.paths === 'auto';
const paths = isAuto ? getAllPaths(vm.router) : config.paths;

Expand Down Expand Up @@ -274,9 +285,9 @@ export function init(config, vm) {
const expireKey = resolveExpireKey(config.namespace) + namespaceSuffix;
const indexKey = resolveIndexKey(config.namespace) + namespaceSuffix;

const isExpired = localStorage.getItem(expireKey) < Date.now();
const isExpired = await getData(expireKey) < Date.now();

INDEXS = JSON.parse(localStorage.getItem(indexKey));
INDEXS = JSON.parse(await getData(indexKey));

if (isExpired) {
INDEXS = {};
Expand All @@ -293,9 +304,11 @@ export function init(config, vm) {
}

Docsify.get(vm.router.getFile(path), false, vm.config.requestHeaders).then(
result => {
async result => {
INDEXS[path] = genIndex(path, result, vm.router, config.depth);
len === ++count && saveData(config.maxAge, expireKey, indexKey);
if (len === ++count) {
await saveData(config.maxAge, expireKey, indexKey);
}
},
);
});
Expand Down

0 comments on commit caedbd5

Please sign in to comment.