Skip to content

Commit 35a7261

Browse files
authored
Merge branch 'develop' into search-filter-md-pure
2 parents deb2dc2 + 42f2548 commit 35a7261

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"*.js": "eslint --fix"
3737
},
3838
"dependencies": {
39+
"dexie": "^4.0.8",
3940
"medium-zoom": "^1.1.0",
4041
"opencollective-postinstall": "^2.0.2",
4142
"prismjs": "^1.29.0",

src/plugins/search/search.js

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,33 @@ import {
33
getAndRemoveDocsifyIgnoreConfig,
44
} from '../../core/render/utils.js';
55
import { markdownToTxt } from './markdown-to-txt.js';
6+
import Dexie from 'dexie';
7+
8+
let INDEXES = {};
9+
10+
const db = new Dexie('docsify');
11+
db.version(1).stores({
12+
search: 'slug, title, body, path, indexKey',
13+
expires: 'key, value',
14+
});
15+
16+
async function saveData(maxAge, expireKey) {
17+
INDEXES = Object.values(INDEXES).flatMap(innerData =>
18+
Object.values(innerData),
19+
);
20+
await db.search.bulkPut(INDEXES);
21+
await db.expires.put({ key: expireKey, value: Date.now() + maxAge });
22+
}
623

7-
let INDEXS = {};
24+
async function getData(key, isExpireKey = false) {
25+
if (isExpireKey) {
26+
const item = await db.expires.get(key);
27+
return item ? item.value : 0;
28+
}
29+
30+
const item = await db.search.where({ indexKey: key }).toArray();
31+
return item ? item : null;
32+
}
833

934
const LOCAL_STORAGE = {
1035
EXPIRE_KEY: 'docsify.search.expires',
@@ -85,12 +110,7 @@ function getListData(token) {
85110
return token.text;
86111
}
87112

88-
function saveData(maxAge, expireKey, indexKey) {
89-
localStorage.setItem(expireKey, Date.now() + maxAge);
90-
localStorage.setItem(indexKey, JSON.stringify(INDEXS));
91-
}
92-
93-
export function genIndex(path, content = '', router, depth) {
113+
export function genIndex(path, content = '', router, depth, indexKey) {
94114
const tokens = window.marked.lexer(content);
95115
const slugify = window.Docsify.slugify;
96116
const index = {};
@@ -113,14 +133,22 @@ export function genIndex(path, content = '', router, depth) {
113133
title = getAndRemoveDocsifyIgnoreConfig(str).content;
114134
}
115135

116-
index[slug] = { slug, title: title, body: '' };
136+
index[slug] = {
137+
slug,
138+
title: title,
139+
body: '',
140+
path: path,
141+
indexKey: indexKey,
142+
};
117143
} else {
118144
if (tokenIndex === 0) {
119145
slug = router.toURL(path);
120146
index[slug] = {
121147
slug,
122148
title: path !== '/' ? path.slice(1) : 'Home Page',
123149
body: token.text || '',
150+
path: path,
151+
indexKey: indexKey,
124152
};
125153
}
126154

@@ -141,6 +169,9 @@ export function genIndex(path, content = '', router, depth) {
141169

142170
index[slug].body = token.text || '';
143171
}
172+
173+
index[slug].path = path;
174+
index[slug].indexKey = indexKey;
144175
}
145176
});
146177
slugify.clear();
@@ -160,21 +191,14 @@ export function ignoreDiacriticalMarks(keyword) {
160191
*/
161192
export function search(query) {
162193
const matchingResults = [];
163-
let data = [];
164-
Object.keys(INDEXS).forEach(key => {
165-
data = [
166-
...data,
167-
...Object.keys(INDEXS[key]).map(page => INDEXS[key][page]),
168-
];
169-
});
170194

171195
query = query.trim();
172196
let keywords = query.split(/[\s\-\\/]+/);
173197
if (keywords.length !== 1) {
174198
keywords = [query, ...keywords];
175199
}
176200

177-
for (const post of data) {
201+
for (const post of INDEXES) {
178202
let matchesScore = 0;
179203
let resultStr = '';
180204
let handlePostTitle = '';
@@ -242,7 +266,7 @@ export function search(query) {
242266
return matchingResults.sort((r1, r2) => r2.score - r1.score);
243267
}
244268

245-
export function init(config, vm) {
269+
export async function init(config, vm) {
246270
const isAuto = config.paths === 'auto';
247271
const paths = isAuto ? getAllPaths(vm.router) : config.paths;
248272

@@ -276,12 +300,12 @@ export function init(config, vm) {
276300
const expireKey = resolveExpireKey(config.namespace) + namespaceSuffix;
277301
const indexKey = resolveIndexKey(config.namespace) + namespaceSuffix;
278302

279-
const isExpired = localStorage.getItem(expireKey) < Date.now();
303+
const isExpired = (await getData(expireKey, true)) < Date.now();
280304

281-
INDEXS = JSON.parse(localStorage.getItem(indexKey));
305+
INDEXES = await getData(indexKey);
282306

283307
if (isExpired) {
284-
INDEXS = {};
308+
INDEXES = {};
285309
} else if (!isAuto) {
286310
return;
287311
}
@@ -290,14 +314,25 @@ export function init(config, vm) {
290314
let count = 0;
291315

292316
paths.forEach(path => {
293-
if (INDEXS[path]) {
317+
const pathExists = Array.isArray(INDEXES)
318+
? INDEXES.some(obj => obj.path === path)
319+
: false;
320+
if (pathExists) {
294321
return count++;
295322
}
296323

297324
Docsify.get(vm.router.getFile(path), false, vm.config.requestHeaders).then(
298-
result => {
299-
INDEXS[path] = genIndex(path, result, vm.router, config.depth);
300-
len === ++count && saveData(config.maxAge, expireKey, indexKey);
325+
async result => {
326+
INDEXES[path] = genIndex(
327+
path,
328+
result,
329+
vm.router,
330+
config.depth,
331+
indexKey,
332+
);
333+
if (len === ++count) {
334+
await saveData(config.maxAge, expireKey);
335+
}
301336
},
302337
);
303338
});

test/integration/example.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
2020
const docsifyInitConfig = {
2121
config: {
2222
name: 'Docsify Name',
23-
themeColor: 'red',
2423
},
2524
markdown: {
2625
coverpage: `
@@ -58,8 +57,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
5857
scriptURLs: [
5958
// docsifyInit() route
6059
'data-test-scripturls.js',
61-
// Server route
62-
'/dist/plugins/search.js',
6360
],
6461
style: `
6562
body {
@@ -76,7 +73,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
7673

7774
// Verify config options
7875
expect(typeof window.$docsify).toBe('object');
79-
expect(window.$docsify).toHaveProperty('themeColor', 'red');
8076
expect(document.querySelector('.app-name').textContent).toContain(
8177
'Docsify Name',
8278
);
@@ -101,7 +97,6 @@ describe('Creating a Docsify site (integration tests in Jest)', function () {
10197

10298
// Verify docsifyInitConfig.scriptURLs were executed
10399
expect(document.body.hasAttribute('data-test-scripturls')).toBe(true);
104-
expect(document.querySelector('.search input[type="search"]')).toBeTruthy();
105100

106101
// Verify docsifyInitConfig.script was added to the DOM
107102
expect(

0 commit comments

Comments
 (0)