Skip to content

Commit

Permalink
feat: use rows and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Jul 21, 2024
1 parent 0398b7a commit 35e3c26
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import Dexie from 'dexie';

let INDEXES = {};

const db = new Dexie('DocsifySearchDB');
const db = new Dexie('docsify');
db.version(1).stores({
search: 'key, value',
search: 'slug, title, body, path, indexKey',
expires: '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(INDEXES) });
await db.search.bulkPut(
Object.values(INDEXES).flatMap(innerData => Object.values(innerData)),
);
await db.expires.put({ key: expireKey, value: Date.now() + maxAge });
}

async function getData(key) {
const item = await db.search.get(key);
return item ? item.value : null;
async function getData(key, isExpireKey = false) {
if (isExpireKey) {
const item = await db.expires.get(key);
return item ? item.value : 0;
}

const item = await db.search.where({ indexKey: key }).toArray();
return item ? item : null;
}

const LOCAL_STORAGE = {
Expand Down Expand Up @@ -89,7 +97,7 @@ function getListData(token) {
return token.text;
}

export function genIndex(path, content = '', router, depth) {
export function genIndex(path, content = '', router, depth, indexKey) {
const tokens = window.marked.lexer(content);
const slugify = window.Docsify.slugify;
const index = {};
Expand All @@ -112,14 +120,22 @@ export function genIndex(path, content = '', router, depth) {
title = getAndRemoveDocsifyIgnoreConfig(str).content;
}

index[slug] = { slug, title: title, body: '' };
index[slug] = {
slug,
title: title,
body: '',
path: path,
indexKey: indexKey,
};
} else {
if (tokenIndex === 0) {
slug = router.toURL(path);
index[slug] = {
slug,
title: path !== '/' ? path.slice(1) : 'Home Page',
body: token.text || '',
path: path,
indexKey: indexKey,
};
}

Expand All @@ -140,6 +156,9 @@ export function genIndex(path, content = '', router, depth) {

index[slug].body = token.text || '';
}

index[slug].path = path;
index[slug].indexKey = indexKey;
}
});
slugify.clear();
Expand All @@ -159,21 +178,14 @@ export function ignoreDiacriticalMarks(keyword) {
*/
export function search(query) {
const matchingResults = [];
let data = [];
Object.keys(INDEXES).forEach(key => {
data = [
...data,
...Object.keys(INDEXES[key]).map(page => INDEXES[key][page]),
];
});

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

for (const post of data) {
for (const post of INDEXES) {
let matchesScore = 0;
let resultStr = '';
let handlePostTitle = '';
Expand Down Expand Up @@ -280,9 +292,9 @@ export async function init(config, vm) {
const expireKey = resolveExpireKey(config.namespace) + namespaceSuffix;
const indexKey = resolveIndexKey(config.namespace) + namespaceSuffix;

const isExpired = (await getData(expireKey)) < Date.now();
const isExpired = (await getData(expireKey, true)) < Date.now();

INDEXES = JSON.parse(await getData(indexKey));
INDEXES = await getData(indexKey);

if (isExpired) {
INDEXES = {};
Expand All @@ -294,13 +306,22 @@ export async function init(config, vm) {
let count = 0;

paths.forEach(path => {
if (INDEXES[path]) {
const pathExists = Array.isArray(INDEXES)
? INDEXES.some(obj => obj.path === path)
: false;
if (pathExists) {
return count++;
}

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

0 comments on commit 35e3c26

Please sign in to comment.