@@ -13,37 +13,36 @@ export function useContentSearch() {
1313 } )
1414 )
1515
16- function search ( keyword : string ) {
16+ const searchData = computed ( ( ) => {
1717 const shorts = shortSearch . value || [ ]
1818 const posts = postSearch . value || [ ]
1919
2020 const mapType = ( o : Record < string , any > , type : string ) => ( { ...o , type } )
21- const mixed = (
22- [ ] as {
23- id : string
24- title : string
25- titles : string
26- level : number
27- content : string
28- type : string
29- } [ ]
30- ) . concat (
31- // @ts -expect-error
32- shorts
21+
22+ return [
23+ ...shorts
3324 . filter ( short => short . level === 1 )
3425 . map ( short => mapType ( short , 'short' ) ) ,
35- posts . filter ( post => post . level === 1 ) . map ( post => mapType ( post , 'post' ) )
36- )
37- const fuse = new Fuse ( mixed , {
26+ ...posts . filter ( post => post . level === 1 ) . map ( post => mapType ( post , 'post' ) )
27+ ]
28+ } )
29+
30+ // Fuse 인스턴스 메모이제이션
31+ const fuse = computed ( ( ) => {
32+ return new Fuse ( searchData . value , {
3833 keys : [ 'title' , 'content' ] ,
3934 threshold : 0.2 ,
4035 includeMatches : true ,
4136 includeScore : true ,
4237 shouldSort : true ,
43- sortFn : ( a , b ) => b . score - a . score
38+ sortFn : ( a , b ) => ( b . score || 0 ) - ( a . score || 0 )
4439 } )
40+ } )
4541
46- return fuse . search ( keyword )
42+ // 검색 실행 함수
43+ function search ( keyword : string ) {
44+ if ( ! keyword ) return [ ]
45+ return fuse . value . search ( keyword )
4746 }
4847
4948 return {
0 commit comments