Skip to content

Commit 1c48c20

Browse files
authored
Merge pull request #54 from EINDEX/feat/fuzzy-search
2 parents 381e2f6 + fa04089 commit 1c48c20

File tree

3 files changed

+101
-20
lines changed

3 files changed

+101
-20
lines changed

src/pages/background/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
102102

103103
const badgeSearch = async (url: string | undefined, tabId: number) => {
104104
if (!url) return;
105-
const cleanUrl = removeUrlHash(url);
106-
const searchRes = await logseqClient.blockSearch(cleanUrl);
105+
const searchURL = new URL(url)
106+
const searchRes = await logseqClient.urlSearch(searchURL);
107107
const resultCount = searchRes.count ? searchRes.count!.toString() : '';
108108
await setExtensionBadge(resultCount, tabId);
109109
};

src/pages/logseq/client.ts

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,21 @@ export default class LogseqClient {
241241
): Promise<LogseqResponseType<LogseqSearchResult>> => {
242242
const { name: graphName } = await this.getCurrentGraph();
243243
const res = await this.find(query);
244-
const blocks = (await Promise.all(
245-
res.map(async (item) => {
246-
const content = this.format(item.content, graphName, query)
247-
if(!content) return null;
248-
return {
249-
html: content,
250-
uuid: item.uuid,
251-
page: await this.getPage({
252-
id: item.page.id,
253-
} as LogseqPageIdenity),
254-
} as LogseqBlockType;
255-
}),
256-
)).filter((b)=>b);
244+
const blocks = (
245+
await Promise.all(
246+
res.map(async (item) => {
247+
const content = this.format(item.content, graphName, query);
248+
if (!content) return null;
249+
return {
250+
html: content,
251+
uuid: item.uuid,
252+
page: await this.getPage({
253+
id: item.page.id,
254+
} as LogseqPageIdenity),
255+
} as LogseqBlockType;
256+
}),
257+
)
258+
).filter((b) => b);
257259
return {
258260
status: 200,
259261
msg: 'success',
@@ -343,4 +345,81 @@ export default class LogseqClient {
343345
return this.findLogseqInternal(query);
344346
});
345347
};
348+
349+
private margeSearchResult = (...searchResult: LogseqSearchResult[]) => {
350+
const result = {
351+
blocks: [],
352+
pages: [],
353+
graph: '',
354+
} as LogseqSearchResult;
355+
const blockSet = new Set();
356+
const pageSet = new Set();
357+
searchResult.forEach((search) => {
358+
search.blocks.forEach((block) => {
359+
if (!blockSet.has(block.uuid)) {
360+
blockSet.add(block.uuid);
361+
result.blocks.push(block);
362+
}
363+
});
364+
search.pages.forEach((page) => {
365+
if (!pageSet.has(page.name)) {
366+
pageSet.add(page.name);
367+
result.pages.push(page);
368+
}
369+
});
370+
});
371+
return result;
372+
};
373+
374+
public urlSearch = async (
375+
url: URL,
376+
options: { fuzzy?: boolean } = { fuzzy: false },
377+
): Promise<LogseqResponseType<LogseqSearchResult | null>> => {
378+
return await this.catchIssues(async () => {
379+
const results = [];
380+
381+
if (url.hash) {
382+
results.push(
383+
(
384+
await this.findLogseqInternal(
385+
url.host + url.pathname + url.search + url.hash,
386+
)
387+
).response,
388+
);
389+
}
390+
if (url.search) {
391+
results.push(
392+
(await this.findLogseqInternal(url.host + url.pathname + url.search))
393+
.response,
394+
);
395+
}
396+
if (url.pathname) {
397+
results.push(
398+
(await this.findLogseqInternal(url.host + url.pathname)).response,
399+
);
400+
}
401+
if (url.host && options.fuzzy) {
402+
results.push({
403+
blocks: [
404+
{
405+
html: '↓ fuzzy search ↓',
406+
uuid: null,
407+
page: null,
408+
},
409+
],
410+
pages: [],
411+
graph: '',
412+
} as LogseqSearchResult);
413+
results.push((await this.findLogseqInternal(url.host)).response);
414+
}
415+
const result = this.margeSearchResult(...results);
416+
const resp: LogseqResponseType<LogseqSearchResult> = {
417+
status: 200,
418+
msg: 'success',
419+
response: result,
420+
count: result.blocks.length + result.pages.length,
421+
};
422+
return resp;
423+
});
424+
};
346425
}

src/pages/popup/Popup.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function Popup() {
2020

2121
const mountOpenPageMethod = () => {
2222
const innerFunction = () => {
23-
if(isLoading) return;
23+
if (isLoading) return;
2424
document.querySelectorAll('a').forEach((e) => {
2525
if (e.onclick === null) {
2626
e.onclick = () => {
@@ -33,7 +33,7 @@ export default function Popup() {
3333
};
3434
}
3535
if (!isLoading) {
36-
clearInterval(interval);
36+
clearInterval(interval);
3737

3838
}
3939
});
@@ -49,9 +49,11 @@ export default function Popup() {
4949
let queryOptions = { active: true, lastFocusedWindow: true };
5050
let [tab] = await Browser.tabs.query(queryOptions);
5151
setIsLoading(true);
52-
if (!tab.url) return;
53-
const url = removeUrlHash(tab.url);
54-
const result = await client.blockSearch(url);
52+
if (!tab || !tab.url) return;
53+
54+
const tabURL = new URL(tab.url);
55+
const result = await client.urlSearch(tabURL, {fuzzy: true});
56+
5557
if (result.status !== 200) return;
5658
setLogseqSearchResult(result.response!);
5759
mountOpenPageMethod();

0 commit comments

Comments
 (0)