Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(search): use dexie.js instead of localStorage#2464

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sy-records merged 8 commits intodevelopfromfeat/dexie
Jul 31, 2024
Merged
Changes from1 commit
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
PrevPrevious commit
NextNext commit
feat: use rows and columns
  • Loading branch information
@sy-records
sy-records committedJul 21, 2024
commitbbe3cfff173436b0beebd6c22b5d42267adfd87f
63 changes: 42 additions & 21 deletionssrc/plugins/search/search.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 ofdata) {
for (const post ofINDEXES) {
let matchesScore = 0;
let resultStr = '';
let handlePostTitle = '';
Expand DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp