@@ -88,7 +88,7 @@ export default {
88
88
const result = searchResult . map ( page => ( {
89
89
...page ,
90
90
parentPageTitle : getParentPageTitle ( page ) ,
91
- ...getAdditionalInfo ( page , queryString , queryTerms ) ,
91
+ ...getAdditionalInfo ( page , normalizeString ( queryString ) , queryTerms ) ,
92
92
} ) )
93
93
94
94
const resultByParent = _ . groupBy ( result , 'parentPageTitle' )
@@ -101,6 +101,7 @@ export default {
101
101
)
102
102
. flat ( )
103
103
} ,
104
+ normalizeString,
104
105
}
105
106
106
107
function getParentPageTitle ( page ) {
@@ -117,15 +118,15 @@ function getAdditionalInfo(page, queryString, queryTerms) {
117
118
const match = getMatch ( page , query , queryTerms )
118
119
if ( ! match )
119
120
return {
120
- headingStr : getFullHeading ( page ) ,
121
+ ... getFullHeading ( page ) ,
121
122
slug : '' ,
122
123
contentStr : null ,
123
124
}
124
125
125
126
if ( match . headerIndex != null ) {
126
127
// header match
127
128
return {
128
- headingStr : getFullHeading ( page , match . headerIndex ) ,
129
+ ... getFullHeading ( page , match . headerIndex , match ) ,
129
130
slug : '#' + page . headers [ match . headerIndex ] . slug ,
130
131
contentStr : null ,
131
132
}
@@ -136,22 +137,27 @@ function getAdditionalInfo(page, queryString, queryTerms) {
136
137
if ( headerIndex === - 1 ) headerIndex = null
137
138
138
139
return {
139
- headingStr : getFullHeading ( page , headerIndex ) ,
140
+ ... getFullHeading ( page , headerIndex ) ,
140
141
slug : headerIndex == null ? '' : '#' + page . headers [ headerIndex ] . slug ,
141
- contentStr : getContentStr ( page , match ) ,
142
+ ... getContentStr ( page , match ) ,
142
143
}
143
144
}
144
145
145
- function getFullHeading ( page , headerIndex ) {
146
- if ( headerIndex == null ) return page . title
146
+ function getFullHeading ( page , headerIndex , match ) {
147
+ if ( headerIndex == null ) return { headingStr : page . title }
147
148
const headersPath = [ ]
148
149
while ( headerIndex != null ) {
149
150
const header = page . headers [ headerIndex ]
150
151
headersPath . unshift ( header )
151
152
headerIndex = _ . findLastIndex ( page . headers , h => h . level === header . level - 1 , headerIndex - 1 )
152
153
if ( headerIndex === - 1 ) headerIndex = null
153
154
}
154
- return headersPath . map ( h => h . title ) . join ( ' > ' )
155
+
156
+ const headingStr = headersPath . map ( h => h . title ) . join ( ' > ' )
157
+ const prefixPath = headersPath . slice ( 0 , - 1 )
158
+ const prefixLength = _ . sum ( prefixPath . map ( p => ( p . title || '' ) . length ) ) + prefixPath . length * 3
159
+ const headingHighlight = match && match . headerIndex != null && [ match . charIndex + prefixLength , match . termLength ]
160
+ return { headingStr, headingHighlight }
155
161
}
156
162
157
163
function getMatch ( page , query , terms ) {
@@ -170,12 +176,10 @@ function getMatch(page, query, terms) {
170
176
}
171
177
172
178
function getHeaderMatch ( page , term ) {
173
- // if (page.slug && page.slug.includes('versioning'))
174
- console . log ( page )
175
179
if ( ! page . headers ) return null
176
180
for ( let i = 0 ; i < page . headers . length ; i ++ ) {
177
181
const h = page . headers [ i ]
178
- const charIndex = h . title . toLowerCase ( ) . indexOf ( term )
182
+ const charIndex = h . normalizedTitle . indexOf ( term )
179
183
if ( charIndex === - 1 ) continue
180
184
return {
181
185
headerIndex : i ,
@@ -187,8 +191,8 @@ function getHeaderMatch(page, term) {
187
191
}
188
192
189
193
function getContentMatch ( page , term ) {
190
- if ( ! page . contentLowercase ) return null
191
- const charIndex = page . contentLowercase . indexOf ( term )
194
+ if ( ! page . normalizedContent ) return null
195
+ const charIndex = page . normalizedContent . indexOf ( term )
192
196
if ( charIndex === - 1 ) return null
193
197
194
198
return { headerIndex : null , charIndex, termLength : term . length }
@@ -205,16 +209,33 @@ function getContentStr(page, match) {
205
209
if ( lineEndIndex === - 1 ) lineEndIndex = page . content . length
206
210
207
211
const line = page . content . slice ( lineStartIndex , lineEndIndex )
208
-
209
- if ( snippetLength >= line . length ) return line
210
-
211
212
const lineCharIndex = charIndex - lineStartIndex
213
+ const contentHighlight = [ lineCharIndex , termLength ]
212
214
213
- const additionalCharactersFromStart = ( snippetLength - termLength ) / 2
215
+ if ( snippetLength >= line . length ) return { contentStr : line , contentHighlight }
216
+
217
+ const additionalCharactersFromStart = _ . round ( ( snippetLength - termLength ) / 2 )
214
218
const snippetStart = Math . max ( lineCharIndex - additionalCharactersFromStart , 0 )
215
219
const snippetEnd = Math . min ( snippetStart + snippetLength , line . length )
216
- let result = line . slice ( snippetStart , snippetEnd )
217
- if ( snippetStart > 0 ) result = '...' + result
218
- if ( snippetEnd < line . length ) result = result + '...'
219
- return result
220
+ let contentStr = line . slice ( snippetStart , snippetEnd )
221
+ contentHighlight [ 0 ] = contentHighlight [ 0 ] - snippetStart
222
+
223
+ if ( snippetStart > 0 ) {
224
+ contentStr = '...' + contentStr
225
+ contentHighlight [ 0 ] = contentHighlight [ 0 ] + 3
226
+ }
227
+ if ( snippetEnd < line . length ) contentStr = contentStr + '...'
228
+ return {
229
+ contentStr,
230
+ contentHighlight,
231
+ }
232
+ }
233
+
234
+ function normalizeString ( str ) {
235
+ if ( ! str ) return str
236
+ return str
237
+ . trim ( )
238
+ . toLowerCase ( )
239
+ . normalize ( 'NFD' )
240
+ . replace ( / [ \u0300 - \u036f ] / g, '' )
220
241
}
0 commit comments