@@ -7,7 +7,7 @@ function escapeHtml (string) {
7
7
'<' : '<' ,
8
8
'>' : '>' ,
9
9
'"' : '"' ,
10
- '\'' : ''' ,
10
+ "'" : ''' ,
11
11
'/' : '/'
12
12
}
13
13
@@ -17,18 +17,19 @@ function escapeHtml (string) {
17
17
function getAllPaths ( router ) {
18
18
const paths = [ ]
19
19
20
- helper . dom . findAll ( 'a:not([data-nosearch])' )
21
- . map ( node => {
22
- const href = node . href
23
- const originHref = node . getAttribute ( 'href' )
24
- const path = router . parse ( href ) . path
25
-
26
- if ( path &&
27
- paths . indexOf ( path ) === - 1 &&
28
- ! Docsify . util . isAbsolutePath ( originHref ) ) {
29
- paths . push ( path )
30
- }
31
- } )
20
+ helper . dom . findAll ( 'a:not([data-nosearch])' ) . map ( node => {
21
+ const href = node . href
22
+ const originHref = node . getAttribute ( 'href' )
23
+ const path = router . parse ( href ) . path
24
+
25
+ if (
26
+ path &&
27
+ paths . indexOf ( path ) === - 1 &&
28
+ ! Docsify . util . isAbsolutePath ( originHref )
29
+ ) {
30
+ paths . push ( path )
31
+ }
32
+ } )
32
33
33
34
return paths
34
35
}
@@ -92,7 +93,11 @@ export function search (query) {
92
93
93
94
if ( postTitle && postContent ) {
94
95
keywords . forEach ( ( keyword , i ) => {
95
- const regEx = new RegExp ( keyword , 'gi' )
96
+ // From https://github.com/sindresorhus/escape-string-regexp
97
+ const regEx = new RegExp (
98
+ keyword . replace ( / [ | \\ { } ( ) [ \] ^ $ + * ? . ] / g, '\\$&' ) ,
99
+ 'gi'
100
+ )
96
101
let indexTitle = - 1
97
102
let indexContent = - 1
98
103
@@ -113,11 +118,12 @@ export function search (query) {
113
118
114
119
if ( end > postContent . length ) end = postContent . length
115
120
116
- const matchContent = '...' +
121
+ const matchContent =
122
+ '...' +
117
123
escapeHtml ( postContent )
118
124
. substring ( start , end )
119
125
. replace ( regEx , `<em class="search-keyword">${ keyword } </em>` ) +
120
- '...'
126
+ '...'
121
127
122
128
resultStr += matchContent
123
129
}
@@ -159,12 +165,9 @@ export function init (config, vm) {
159
165
paths . forEach ( path => {
160
166
if ( INDEXS [ path ] ) return count ++
161
167
162
- helper
163
- . get ( vm . router . getFile ( path ) )
164
- . then ( result => {
165
- INDEXS [ path ] = genIndex ( path , result , vm . router , config . depth )
166
- len === ++ count && saveData ( config . maxAge )
167
- } )
168
+ helper . get ( vm . router . getFile ( path ) ) . then ( result => {
169
+ INDEXS [ path ] = genIndex ( path , result , vm . router , config . depth )
170
+ len === ++ count && saveData ( config . maxAge )
171
+ } )
168
172
} )
169
173
}
170
-
0 commit comments