Skip to content

feat: introduce a text compress for search function #2454

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 2 commits into from
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
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
9 changes: 9 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": {
"lz-string": "^1.5.0",
"medium-zoom": "^1.1.0",
"opencollective-postinstall": "^2.0.2",
"prismjs": "^1.29.0",
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const CONFIG = {
namespace: undefined,
pathNamespaces: undefined,
keyBindings: ['/', 'meta+k', 'ctrl+k'],
largeContent: false,
};

const install = function (hook, vm) {
Expand All @@ -33,6 +34,7 @@ const install = function (hook, vm) {
CONFIG.namespace = opts.namespace || CONFIG.namespace;
CONFIG.pathNamespaces = opts.pathNamespaces || CONFIG.pathNamespaces;
CONFIG.keyBindings = opts.keyBindings || CONFIG.keyBindings;
CONFIG.largeContent = opts.largeContent || CONFIG.largeContent;
}

const isAuto = CONFIG.paths === 'auto';
Expand Down
27 changes: 23 additions & 4 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
getAndRemoveDocisfyIgnoreConfig,
} from '../../core/render/utils.js';

import LZString from 'lz-string';

let INDEXS = {};

const LOCAL_STORAGE = {
Expand Down Expand Up @@ -73,9 +75,25 @@ function getListData(token) {
return token.text;
}

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

if (largeContent) {
const compressed = LZString.compressToUTF16(JSON.stringify(INDEXS));
localStorage.setItem(indexKey, compressed);
} else {
localStorage.setItem(indexKey, JSON.stringify(INDEXS));
}
}

function getData(indexKey, largeContent) {
if (largeContent) {
const compressedData = localStorage.getItem(indexKey);
return (
compressedData && JSON.parse(LZString.decompressFromUTF16(compressedData))
);
}
return JSON.parse(localStorage.getItem(indexKey));
}

export function genIndex(path, content = '', router, depth) {
Expand Down Expand Up @@ -276,7 +294,7 @@ export function init(config, vm) {

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

INDEXS = JSON.parse(localStorage.getItem(indexKey));
INDEXS = getData(indexKey, config.largeContent);

if (isExpired) {
INDEXS = {};
Expand All @@ -295,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);
len === ++count &&
saveData(config.maxAge, expireKey, indexKey, config.largeContent);
},
);
});
Expand Down
Loading