@@ -40,30 +40,36 @@ function saveData (maxAge) {
40
40
export function genIndex ( path , content = '' ) {
41
41
const tokens = window . marked . lexer ( content )
42
42
const toURL = Docsify . route . toURL
43
+ const index = { }
43
44
let slug
44
45
45
46
tokens . forEach ( token => {
46
- if ( token . type === 'heading' && token . depth === 1 ) {
47
+ if ( token . type === 'heading' && token . depth <= 2 ) {
47
48
slug = toURL ( path , { id : token . text } )
48
- INDEXS [ slug ] = { slug, title : token . text , body : '' }
49
+ index [ slug ] = { slug, title : token . text , body : '' }
49
50
} else {
50
51
if ( ! slug ) return
51
- if ( ! INDEXS [ slug ] ) {
52
- INDEXS [ slug ] = { slug, title : '' , body : '' }
52
+ if ( ! index [ slug ] ) {
53
+ index [ slug ] = { slug, title : '' , body : '' }
53
54
} else {
54
- if ( INDEXS [ slug ] . body ) {
55
- INDEXS [ slug ] . body += '\n' + ( token . text || '' )
55
+ if ( index [ slug ] . body ) {
56
+ index [ slug ] . body += '\n' + ( token . text || '' )
56
57
} else {
57
- INDEXS [ slug ] . body = token . text
58
+ index [ slug ] . body = token . text
58
59
}
59
60
}
60
61
}
61
62
} )
63
+
64
+ return index
62
65
}
63
66
64
67
export function search ( keywords ) {
65
68
const matchingResults = [ ]
66
- const data = Object . keys ( INDEXS ) . map ( key => INDEXS [ key ] )
69
+ let data = [ ]
70
+ Object . keys ( INDEXS ) . forEach ( key => {
71
+ data = data . concat ( Object . keys ( INDEXS [ key ] ) . map ( page => INDEXS [ key ] [ page ] ) )
72
+ } )
67
73
68
74
keywords = keywords . trim ( ) . split ( / [ \s \- \, \\ / ] + / )
69
75
@@ -99,7 +105,7 @@ export function search (keywords) {
99
105
if ( end > postContent . length ) end = postContent . length
100
106
101
107
const matchContent = '...' +
102
- postContent
108
+ escapeHtml ( postContent )
103
109
. substring ( start , end )
104
110
. replace ( regEx , `<em class="search-keyword">${ keyword } </em>` ) +
105
111
'...'
@@ -144,11 +150,10 @@ export function init (config, vm) {
144
150
paths . forEach ( path => {
145
151
if ( INDEXS [ path ] ) return count ++
146
152
147
- path = vm . $getFile ( path )
148
153
helper
149
- . get ( path )
154
+ . get ( vm . $getFile ( path ) )
150
155
. then ( result => {
151
- genIndex ( path , result )
156
+ INDEXS [ path ] = genIndex ( path , result )
152
157
len === ++ count && saveData ( config . maxAge )
153
158
} )
154
159
} )
0 commit comments