11import flexsearch from 'flexsearch' ;
2-
2+ import type { Block , Tree } from './types' ;
3+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
34// @ts -expect-error
45const Index = /** @type {import('flexsearch').Index } */ ( flexsearch . Index ) ?? flexsearch ;
56
67/** If the search is already initialized */
78export let inited = false ;
89
9- /** @type {import('flexsearch').Index<any>[] } */
10- let indexes ;
10+ let indexes : import ( 'flexsearch' ) . Index < any > [ ] ;
1111
1212/** @type {Map<string, import('./types').Block> } */
1313const map = new Map ( ) ;
1414
1515/** @type {Map<string, string> } */
1616const hrefs = new Map ( ) ;
1717
18- /**
19- * Initialize the search index
20- * @param {import('./types').Block[] } blocks
21- */
22- export function init ( blocks ) {
18+
19+ export function init ( blocks : Block [ ] ) {
2320 if ( inited ) return ;
2421
2522 // we have multiple indexes, so we can rank sections (migration guide comes last)
@@ -38,6 +35,9 @@ export function init(blocks) {
3835 // We'd probably want to test both implementations across browsers if memory usage becomes an issue
3936 // TODO: fix the type by updating flexsearch after
4037 // https://github.com/nextapps-de/flexsearch/pull/364 is merged and released
38+
39+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
40+ //@ts -ignore
4141 indexes [ block . rank ?? 0 ] . add ( block . href , `${ title } ${ block . content } ` ) ;
4242
4343 hrefs . set ( block . breadcrumbs . join ( '::' ) , block . href ) ;
@@ -51,17 +51,19 @@ export function init(blocks) {
5151 * @param {string } query
5252 * @returns {import('./types').Tree[] }
5353 */
54- export function search ( query ) {
54+ export function search ( query : string ) {
5555 const escaped = query . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
5656 const regex = new RegExp ( `(^|\\b)${ escaped } ` , 'i' ) ;
5757
5858 const blocks = indexes
5959 . flatMap ( ( index ) => index . search ( query ) )
60+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
61+ //@ts -ignore
6062 . map ( lookup )
6163 . map ( ( block , rank ) => ( { block : /** @type {import('./types').Block } */ ( block ) , rank } ) )
6264 . sort ( ( a , b ) => {
63- const a_title_matches = regex . test ( /** @type {string } */ ( a . block . breadcrumbs . at ( - 1 ) ) ) ;
64- const b_title_matches = regex . test ( /** @type {string } */ ( b . block . breadcrumbs . at ( - 1 ) ) ) ;
65+ const a_title_matches = regex . test ( /** @type {string } */ ( a . block . breadcrumbs . at ( - 1 ) ) ) ;
66+ const b_title_matches = regex . test ( /** @type {string } */ ( b . block . breadcrumbs . at ( - 1 ) ) ) ;
6567
6668 // massage the order a bit, so that title matches
6769 // are given higher priority
@@ -82,16 +84,11 @@ export function search(query) {
8284 * Get a block with details by its href
8385 * @param {string } href
8486 */
85- export function lookup ( href ) {
87+ export function lookup ( href : string ) {
8688 return map . get ( href ) ;
8789}
8890
89- /**
90- * @param {string[] } breadcrumbs
91- * @param {import('./types').Block[] } blocks
92- * @returns {import('./types').Tree }
93- */
94- function tree ( breadcrumbs , blocks ) {
91+ function tree ( breadcrumbs : string [ ] , blocks : Block [ ] ) : Tree {
9592 const depth = breadcrumbs . length ;
9693
9794 const node = blocks . find ( ( block ) => {
@@ -109,6 +106,8 @@ function tree(breadcrumbs, blocks) {
109106 return {
110107 breadcrumbs,
111108 href : /** @type {string } */ ( hrefs . get ( breadcrumbs . join ( '::' ) ) ) ,
109+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
110+ //@ts -ignore
112111 node : /** @type {import('./types').Block } */ ( node ) ,
113112 children : child_parts . map ( ( part ) => tree ( [ ...breadcrumbs , part ] , descendants ) )
114113 } ;
0 commit comments