From 03397bba34eda97a504c4860a2ba3aaa0794589c Mon Sep 17 00:00:00 2001 From: koy Date: Thu, 13 Jun 2024 19:54:55 +0800 Subject: [PATCH 1/2] feat: support large content compress in search storage --- package-lock.json | 9 +++++++++ package.json | 1 + src/plugins/search/index.js | 2 ++ src/plugins/search/search.js | 22 +++++++++++++++++++--- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9f4b7fe5..cea2ad122 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { + "lz-string": "^1.5.0", "medium-zoom": "^1.1.0", "opencollective-postinstall": "^2.0.2", "prismjs": "^1.29.0", @@ -10974,6 +10975,14 @@ "yallist": "^3.0.2" } }, + "node_modules/lz-string": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/lz-string/-/lz-string-1.5.0.tgz", + "integrity": "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==", + "bin": { + "lz-string": "bin/bin.js" + } + }, "node_modules/magic-string": { "version": "0.30.10", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.10.tgz", diff --git a/package.json b/package.json index 5ac8fc5c1..aecc6578a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/plugins/search/index.js b/src/plugins/search/index.js index d66f2f129..9ae490789 100644 --- a/src/plugins/search/index.js +++ b/src/plugins/search/index.js @@ -14,6 +14,7 @@ const CONFIG = { namespace: undefined, pathNamespaces: undefined, keyBindings: ['/', 'meta+k', 'ctrl+k'], + largeContent: false }; const install = function (hook, vm) { @@ -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'; diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 95aa83a8b..268999982 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -3,6 +3,8 @@ import { getAndRemoveDocisfyIgnoreConfig, } from '../../core/render/utils.js'; +import LZString from 'lz-string'; + let INDEXS = {}; const LOCAL_STORAGE = { @@ -73,9 +75,23 @@ function getListData(token) { return token.text; } -function saveData(maxAge, expireKey, indexKey) { +function saveData(maxAge, expireKey, indexKey, largeContent) { localStorage.setItem(expireKey, Date.now() + maxAge); + + 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) { @@ -276,7 +292,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 = {}; @@ -295,7 +311,7 @@ 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); }, ); }); From d6f7bef82e8624b1aebf13d1fb5e6862c362ee24 Mon Sep 17 00:00:00 2001 From: koy Date: Thu, 13 Jun 2024 19:59:14 +0800 Subject: [PATCH 2/2] feat: support large content compress in search storage --- docs/plugins.md | 2 ++ src/plugins/search/index.js | 2 +- src/plugins/search/search.js | 15 +++++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index 4b69ad5f9..8123ee0d1 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -9,6 +9,8 @@ By default, the hyperlink on the current page is recognized and the content is s