Skip to content

Commit

Permalink
feat: support large content compress in search storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Jun 13, 2024
1 parent 03397bb commit d6f7bef
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ By default, the hyperlink on the current page is recognized and the content is s
<script>
window.$docsify = {
search: 'auto', // default
// Enable text compression to reduce the size, especially if your contents are very large.
largeContent: false, // default
search: [
'/', // => /README.md
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CONFIG = {
namespace: undefined,
pathNamespaces: undefined,
keyBindings: ['/', 'meta+k', 'ctrl+k'],
largeContent: false
largeContent: false,
};

const install = function (hook, vm) {
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ function saveData(maxAge, expireKey, indexKey, largeContent) {
if (largeContent) {
const compressed = LZString.compressToUTF16(JSON.stringify(INDEXS));
localStorage.setItem(indexKey, compressed);
}else{
localStorage.setItem(indexKey, JSON.stringify(INDEXS));
} else {
localStorage.setItem(indexKey, JSON.stringify(INDEXS));
}
}

function getData(indexKey, largeContent ) {
function getData(indexKey, largeContent) {
if (largeContent) {
const compressedData = localStorage.getItem(indexKey);
return compressedData && JSON.parse(LZString.decompressFromUTF16(compressedData));
return (
compressedData && JSON.parse(LZString.decompressFromUTF16(compressedData))
);
}
return JSON.parse(localStorage.getItem(indexKey));
}
Expand Down Expand Up @@ -292,7 +294,7 @@ export function init(config, vm) {

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

INDEXS = getData(indexKey, config.largeContent)
INDEXS = getData(indexKey, config.largeContent);

if (isExpired) {
INDEXS = {};
Expand All @@ -311,7 +313,8 @@ export function init(config, vm) {
Docsify.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, expireKey, indexKey, config.largeContent);
len === ++count &&
saveData(config.maxAge, expireKey, indexKey, config.largeContent);
},
);
});
Expand Down

0 comments on commit d6f7bef

Please sign in to comment.