@@ -13,14 +13,13 @@ interface Item {
13
13
14
14
// Can be expanded with things like "term" in the future
15
15
type SearchType = "basic" | "tags"
16
-
17
- // Current searchType
18
16
let searchType : SearchType = "basic"
19
- // Current search term // TODO: exact match
20
17
let currentSearchTerm : string = ""
21
- // index for search
22
18
let index : FlexSearch . Document < Item > | undefined = undefined
19
+ const p = new DOMParser ( )
20
+ const encoder = ( str : string ) => str . toLowerCase ( ) . split ( / ( [ ^ a - z ] | [ ^ \x00 - \x7F ] ) / )
23
21
22
+ const fetchContentCache : Map < FullSlug , Element [ ] > = new Map ( )
24
23
const contextWindowWords = 30
25
24
const numSearchResults = 8
26
25
const numTagResults = 5
@@ -79,7 +78,6 @@ function highlight(searchTerm: string, text: string, trim?: boolean) {
79
78
}
80
79
81
80
function highlightHTML ( searchTerm : string , el : HTMLElement ) {
82
- // try to highlight longest tokens first
83
81
const p = new DOMParser ( )
84
82
const tokenizedTerms = tokenizeTerm ( searchTerm )
85
83
const html = p . parseFromString ( el . innerHTML , "text/html" )
@@ -117,12 +115,6 @@ function highlightHTML(searchTerm: string, el: HTMLElement) {
117
115
return html . body
118
116
}
119
117
120
- const p = new DOMParser ( )
121
- const encoder = ( str : string ) => str . toLowerCase ( ) . split ( / ( [ ^ a - z ] | [ ^ \x00 - \x7F ] ) / )
122
- let prevShortcutHandler : ( ( e : HTMLElementEventMap [ "keydown" ] ) => void ) | undefined = undefined
123
-
124
- const fetchContentCache : Map < FullSlug , Element [ ] > = new Map ( )
125
-
126
118
document . addEventListener ( "nav" , async ( e : CustomEventMap [ "nav" ] ) => {
127
119
const currentSlug = e . detail . url
128
120
@@ -496,16 +488,12 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
496
488
await displayResults ( finalResults )
497
489
}
498
490
499
- if ( prevShortcutHandler ) {
500
- document . removeEventListener ( "keydown" , prevShortcutHandler )
501
- }
502
-
503
491
document . addEventListener ( "keydown" , shortcutHandler )
504
- prevShortcutHandler = shortcutHandler
505
- searchIcon ?. removeEventListener ( "click" , ( ) => showSearch ( "basic" ) )
492
+ window . addCleanup ( ( ) => document . removeEventListener ( "keydown" , shortcutHandler ) )
506
493
searchIcon ?. addEventListener ( "click" , ( ) => showSearch ( "basic" ) )
507
- searchBar ?. removeEventListener ( "input " , onType )
494
+ window . addCleanup ( ( ) => searchIcon ?. removeEventListener ( "click " , ( ) => showSearch ( "basic" ) ) )
508
495
searchBar ?. addEventListener ( "input" , onType )
496
+ window . addCleanup ( ( ) => searchBar ?. removeEventListener ( "input" , onType ) )
509
497
510
498
// setup index if it hasn't been already
511
499
if ( ! index ) {
@@ -546,13 +534,12 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
546
534
async function fillDocument ( index : FlexSearch . Document < Item , false > , data : any ) {
547
535
let id = 0
548
536
for ( const [ slug , fileData ] of Object . entries < ContentDetails > ( data ) ) {
549
- await index . addAsync ( id , {
537
+ await index . addAsync ( id ++ , {
550
538
id,
551
539
slug : slug as FullSlug ,
552
540
title : fileData . title ,
553
541
content : fileData . content ,
554
542
tags : fileData . tags ,
555
543
} )
556
- id ++
557
544
}
558
545
}
0 commit comments