@@ -69,9 +69,9 @@ function style () {
69
69
dom . appendTo ( dom . head , style )
70
70
}
71
71
72
- function tpl ( opts ) {
72
+ function tpl ( opts , defaultValue = '' ) {
73
73
const html =
74
- `<input type="search" />` +
74
+ `<input type="search" value=" ${ defaultValue } " />` +
75
75
'<div class="results-panel"></div>' +
76
76
'</div>'
77
77
const el = dom . create ( 'div' , html )
@@ -81,29 +81,32 @@ function tpl (opts) {
81
81
dom . before ( aside , el )
82
82
}
83
83
84
- function bindEvents ( ) {
84
+ function doSearch ( value ) {
85
85
const $search = dom . find ( 'div.search' )
86
- const $input = dom . find ( $search , 'input' )
87
86
const $panel = dom . find ( $search , '.results-panel' )
88
- const doSearch = function ( value ) {
89
- if ( ! value ) {
90
- $panel . classList . remove ( 'show' )
91
- $panel . innerHTML = ''
92
- return
93
- }
94
- const matchs = search ( value )
95
-
96
- let html = ''
97
- matchs . forEach ( post => {
98
- html += `<div class="matching-post">
99
- <h2><a href="${ post . url } ">${ post . title } </a></h2>
100
- <p>${ post . content } </p>
101
- </div>`
102
- } )
103
87
104
- $panel . classList . add ( 'show' )
105
- $panel . innerHTML = html || `<p class="empty">${ NO_DATA_TEXT } </p>`
88
+ if ( ! value ) {
89
+ $panel . classList . remove ( 'show' )
90
+ $panel . innerHTML = ''
91
+ return
106
92
}
93
+ const matchs = search ( value )
94
+
95
+ let html = ''
96
+ matchs . forEach ( post => {
97
+ html += `<div class="matching-post">
98
+ <h2><a href="${ post . url } ">${ post . title } </a></h2>
99
+ <p>${ post . content } </p>
100
+ </div>`
101
+ } )
102
+
103
+ $panel . classList . add ( 'show' )
104
+ $panel . innerHTML = html || `<p class="empty">${ NO_DATA_TEXT } </p>`
105
+ }
106
+
107
+ function bindEvents ( ) {
108
+ const $search = dom . find ( 'div.search' )
109
+ const $input = dom . find ( $search , 'input' )
107
110
108
111
let timeId
109
112
// Prevent to Fold sidebar
@@ -137,9 +140,12 @@ function updateNoData (text, path) {
137
140
138
141
export function init ( opts ) {
139
142
dom = Docsify . dom
143
+ const keywords = Docsify . route . parse ( ) . query . s
144
+
140
145
style ( )
141
- tpl ( opts )
146
+ tpl ( opts , keywords )
142
147
bindEvents ( )
148
+ keywords && setTimeout ( _ => doSearch ( keywords ) , 500 )
143
149
}
144
150
145
151
export function update ( opts , vm ) {
0 commit comments