@@ -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}
0 commit comments