@@ -50,14 +50,14 @@ const genFilePath = function (path) {
50
50
51
51
filePath = basePath + filePath
52
52
53
- return filePath . replace ( / \/ \/ / g, '/' )
53
+ return filePath . replace ( / \/ + / g, '/' )
54
54
}
55
55
56
56
/**
57
57
* generate index
58
58
*/
59
59
const genIndex = function ( path , content = '' ) {
60
- // INDEXS[path] = {}
60
+ INDEXS [ path ] = { slug : '' , title : '' , body : '' }
61
61
let slug
62
62
63
63
content
@@ -73,7 +73,7 @@ const genIndex = function (path, content = '') {
73
73
// <h1 id="xxx"></h1>
74
74
const id = attr . match ( / i d = " ( \S + ) " / ) [ 1 ]
75
75
76
- slug = `#/${ path } #${ id } ` . replace ( / \/ \/ / , '/' )
76
+ slug = `#/${ path } #${ id } ` . replace ( / \/ + / , '/' )
77
77
INDEXS [ slug ] = { slug, title : text , body : '' }
78
78
} else {
79
79
// other html tag
@@ -221,12 +221,10 @@ class SearchComponent {
221
221
for ( let i = 0 ; i < data . length ; i ++ ) {
222
222
const post = data [ i ]
223
223
let isMatch = false
224
- let matchingNum = 0
225
224
let resultStr = ''
226
225
const postTitle = post . title && post . title . trim ( )
227
226
const postContent = post . body && post . body . trim ( )
228
227
const postUrl = post . slug || ''
229
- const postType = post . pagetitle
230
228
231
229
if ( postTitle !== '' && postContent !== '' ) {
232
230
keywords . forEach ( ( keyword , i ) => {
@@ -241,7 +239,6 @@ class SearchComponent {
241
239
isMatch = false
242
240
} else {
243
241
isMatch = true
244
- matchingNum ++
245
242
if ( indexContent < 0 ) indexContent = 0
246
243
247
244
let start = 0
@@ -266,9 +263,7 @@ class SearchComponent {
266
263
const matchingPost = {
267
264
title : escapeHtml ( postTitle ) ,
268
265
content : resultStr ,
269
- url : postUrl ,
270
- type : postType ,
271
- matchingNum : matchingNum
266
+ url : postUrl
272
267
}
273
268
274
269
matchingResults . push ( matchingPost )
@@ -280,23 +275,30 @@ class SearchComponent {
280
275
}
281
276
}
282
277
283
- // TODO 如果不存在就重新加载
284
278
const searchPlugin = function ( ) {
285
- if ( localStorage . getItem ( 'docsify.search.expires' ) > Date . now ( ) ) {
286
- INDEXS = JSON . parse ( localStorage . getItem ( 'docsify.search.index' ) )
279
+ const isAuto = CONFIG . paths === 'auto'
280
+ const isExpired = localStorage . getItem ( 'docsify.search.expires' ) < Date . now ( )
281
+
282
+ INDEXS = JSON . parse ( localStorage . getItem ( 'docsify.search.index' ) )
283
+
284
+ if ( isExpired ) {
285
+ INDEXS = { }
286
+ } else if ( ! isAuto ) {
287
287
return
288
288
}
289
289
290
- const paths = CONFIG . paths === 'auto' ? getAllPaths ( ) : CONFIG . paths
290
+ let count = 0
291
+ const paths = isAuto ? getAllPaths ( ) : CONFIG . paths
291
292
const len = paths . length
292
293
const { load, marked, slugify } = window . Docsify . utils
293
- let count = 0
294
294
const done = ( ) => {
295
295
localStorage . setItem ( 'docsify.search.expires' , Date . now ( ) + CONFIG . maxAge )
296
296
localStorage . setItem ( 'docsify.search.index' , JSON . stringify ( INDEXS ) )
297
297
}
298
298
299
299
paths . forEach ( path => {
300
+ if ( INDEXS [ path ] ) return count ++
301
+
300
302
load ( genFilePath ( path ) ) . then ( content => {
301
303
genIndex ( path , marked ( content ) )
302
304
slugify . clear ( )
0 commit comments