1
- const root_href = document . head . querySelector ( "link[rel='root']" ) . getAttribute ( ' href' )
1
+ const root_href = document . head . querySelector ( "link[rel='root']" ) . getAttribute ( " href" )
2
2
3
-
4
- const minby = ( arr , fn ) => arr . reduce ( ( a , b ) => fn ( a ) < fn ( b ) ? a : b )
5
- const maxby = ( arr , fn ) => arr . reduce ( ( a , b ) => fn ( a ) > fn ( b ) ? a : b )
3
+ const minby = ( arr , fn ) => arr . reduce ( ( a , b ) => ( fn ( a ) < fn ( b ) ? a : b ) )
4
+ const maxby = ( arr , fn ) => arr . reduce ( ( a , b ) => ( fn ( a ) > fn ( b ) ? a : b ) )
6
5
const range = ( length ) => [ ...Array ( length ) . keys ( ) ]
7
6
8
7
const sortby = ( arr , fn ) => arr . sort ( ( a , b ) => fn ( a ) - fn ( b ) )
9
8
10
-
11
9
const setup_search_index = async ( ) => {
12
- const search_data_href = document . head . querySelector ( "link[rel='pp-search-data']" ) . getAttribute ( ' href' )
10
+ const search_data_href = document . head . querySelector ( "link[rel='pp-search-data']" ) . getAttribute ( " href" )
13
11
console . log ( search_data_href )
14
-
12
+
15
13
const search_data = await ( await fetch ( search_data_href ) ) . json ( )
16
14
window . search_data = search_data
17
-
15
+
18
16
console . log ( search_data )
19
17
20
18
// create a search bar powered by lunr
@@ -29,50 +27,46 @@ const setup_search_index = async () => {
29
27
// create a search index
30
28
const before = Date . now ( )
31
29
const search_index = window . lunr ( function ( ) {
32
- this . ref ( ' url' )
33
-
34
- this . field ( ' title' , { boost : 10 } )
35
- this . field ( ' tags' , { boost : 5 } )
36
- this . field ( ' text' )
37
- this . metadataWhitelist = [ ' position' ]
30
+ this . ref ( " url" )
31
+
32
+ this . field ( " title" , { boost : 10 } )
33
+ this . field ( " tags" , { boost : 5 } )
34
+ this . field ( " text" )
35
+ this . metadataWhitelist = [ " position" ]
38
36
search_data . forEach ( function ( doc ) {
39
37
this . add ( doc )
40
38
} , this )
41
39
} )
42
40
const after = Date . now ( )
43
41
console . info ( `lunr: Indexing ${ search_data . length } documents took ${ after - before } ms` )
44
42
window . search_index = search_index
45
-
46
- return { search_data, search_index}
47
- }
48
43
44
+ return { search_data, search_index }
45
+ }
49
46
50
47
const excerpt_length = 200
51
48
const excerpt_padding = 50
52
49
53
-
54
50
const init_search = async ( ) => {
55
- const query = new URLSearchParams ( window . location . search ) . get ( 'q' )
56
- console . warn ( { query} )
57
-
51
+ const query = new URLSearchParams ( window . location . search ) . get ( "q" )
52
+ console . warn ( { query } )
53
+
58
54
document . querySelector ( ".search-bar.big input" ) . value = query
59
-
60
- const { search_data, search_index} = await setup_search_index ( )
61
-
55
+
56
+ const { search_data, search_index } = await setup_search_index ( )
57
+
62
58
if ( query ) {
63
59
const results = search_index . search ( query )
64
60
console . log ( results )
65
-
66
- const search_results = document . getElementById ( 'search-results' )
67
-
68
- if ( results . length !== 0 ) {
69
-
70
-
71
- search_results . innerHTML = ''
72
- results . forEach ( result => {
73
- const { url, title, tags, text} = search_data . find ( doc => doc . url === result . ref )
74
- const result_div = document . createElement ( 'a' )
75
- result_div . classList . add ( 'search-result' )
61
+
62
+ const search_results = document . getElementById ( "search-results" )
63
+
64
+ if ( results . length !== 0 ) {
65
+ search_results . innerHTML = ""
66
+ results . forEach ( ( result ) => {
67
+ const { url, title, tags, text } = search_data . find ( ( doc ) => doc . url === result . ref )
68
+ const result_div = document . createElement ( "a" )
69
+ result_div . classList . add ( "search-result" )
76
70
result_div . innerHTML = `
77
71
<h3 class="title"></h3>
78
72
<p class="snippet"></p>
@@ -81,85 +75,74 @@ const init_search = async () => {
81
75
console . log ( root_href )
82
76
result_div . querySelector ( ".title" ) . innerText = title
83
77
result_div . href = new URL ( url , new URL ( root_href , window . location . href ) ) . href
84
- result_div . querySelector ( ".tags" ) . innerText = tags . join ( ', ' )
78
+ result_div . querySelector ( ".tags" ) . innerText = tags . join ( ", " )
85
79
result_div . querySelector ( ".snippet" ) . innerText = text . substring ( 0 , excerpt_length ) + "..."
86
-
87
-
88
- const text_match_positions = Object . values ( result ?. matchData ?. metadata ?? { } ) . flatMap ( z => z ?. text ?. position ?? [ ] ) . sort ( ( [ a , _a ] , [ b , _b ] ) => a - b )
89
- const title_match_positions = Object . values ( result ?. matchData ?. metadata ?? { } ) . flatMap ( z => z ?. title ?. position ?? [ ] ) . sort ( ( [ a , _a ] , [ b , _b ] ) => a - b )
90
-
80
+
81
+ const text_match_positions = Object . values ( result ?. matchData ?. metadata ?? { } )
82
+ . flatMap ( ( z ) => z ?. text ?. position ?? [ ] )
83
+ . sort ( ( [ a , _a ] , [ b , _b ] ) => a - b )
84
+ const title_match_positions = Object . values ( result ?. matchData ?. metadata ?? { } )
85
+ . flatMap ( ( z ) => z ?. title ?. position ?? [ ] )
86
+ . sort ( ( [ a , _a ] , [ b , _b ] ) => a - b )
87
+
91
88
console . error ( title_match_positions )
92
- if ( title_match_positions . length > 0 ) {
89
+ if ( title_match_positions . length > 0 ) {
93
90
const strong_el = document . createElement ( "strong" )
94
91
strong_el . innerText = title
95
92
result_div . querySelector ( ".title" ) . innerHTML = ``
96
93
result_div . querySelector ( ".title" ) . appendChild ( strong_el )
97
94
}
98
-
99
- if ( text_match_positions . length > 0 ) {
100
-
101
-
102
-
103
-
95
+
96
+ if ( text_match_positions . length > 0 ) {
104
97
// console.log(text_match_positions)
105
98
// console.log(find_longest_run(text_match_positions, 50))
106
99
// console.log(find_longest_run(text_match_positions, 100))
107
100
// console.log(find_longest_run(text_match_positions, 200))
108
101
// console.log(find_longest_run(text_match_positions, 300))
109
102
// console.log(find_longest_run(text_match_positions, 400))
110
-
103
+
111
104
const [ start_index , num_matches ] = find_longest_run ( text_match_positions , excerpt_length )
112
-
105
+
113
106
const excerpt_start = text_match_positions [ start_index ] [ 0 ]
114
107
const excerpt_end = excerpt_start + excerpt_length
115
-
116
-
108
+
117
109
const highlighted_ranges = text_match_positions . slice ( start_index , start_index + num_matches )
118
-
110
+
119
111
const elements = highlighted_ranges . flatMap ( ( [ h_start , h_length ] , i ) => {
120
112
const h_end = h_start + h_length
121
113
const word = text . slice ( h_start , h_end )
122
114
const filler = text . slice ( h_end , highlighted_ranges [ i + 1 ] ?. [ 0 ] ?? excerpt_end )
123
- const word_el = document . createElement ( ' strong' )
115
+ const word_el = document . createElement ( " strong" )
124
116
word_el . innerText = word
125
117
return [ word_el , filler ]
126
118
} )
127
-
128
- const snippet_p = result_div . querySelector ( ".snippet" ) ;
129
- snippet_p . innerHTML = `` ;
130
- [ "..." , text . slice ( excerpt_start - excerpt_padding , excerpt_start ) . trimStart ( ) , ...elements , "..." ] . forEach ( el => snippet_p . append ( el ) )
119
+
120
+ const snippet_p = result_div . querySelector ( ".snippet" )
121
+ snippet_p . innerHTML = ``
122
+ ; [ "..." , text . slice ( excerpt_start - excerpt_padding , excerpt_start ) . trimStart ( ) , ...elements , "..." ] . forEach ( ( el ) => snippet_p . append ( el ) )
131
123
}
132
-
133
-
134
-
124
+
135
125
// text_match_positions.slice(start_index, start_index + num_matches).forEach(([start, length]) => {
136
-
137
-
138
-
126
+
139
127
search_results . appendChild ( result_div )
140
-
141
128
} )
142
129
} else {
143
130
search_results . innerText = `No results found for "${ query } "`
144
- }
131
+ }
145
132
}
146
133
}
147
134
148
-
149
135
const count = ( arr , fn ) => arr . reduce ( ( a , b ) => fn ( a ) + fn ( b ) , 0 )
150
136
151
-
152
-
153
137
const find_longest_run = ( /** @type {Array<[number, number]> } */ positions , max_dist ) => {
154
- const legal_run_size = start_index => positions . slice ( start_index ) . filter ( ( [ start , length ] ) => ( start + length ) < positions [ start_index ] [ 0 ] + max_dist ) . length
155
-
138
+ const legal_run_size = ( start_index ) =>
139
+ positions . slice ( start_index ) . filter ( ( [ start , length ] ) => start + length < positions [ start_index ] [ 0 ] + max_dist ) . length
140
+
156
141
console . warn ( range ( positions . length ) . map ( legal_run_size ) )
157
-
142
+
158
143
const best_start = maxby ( range ( positions . length ) , legal_run_size )
159
144
const best_length = legal_run_size ( best_start )
160
145
return [ best_start , best_length ]
161
146
}
162
-
163
147
164
148
window . init_search = init_search
165
-
0 commit comments