@@ -193,13 +193,14 @@ window.addEventListener('hexo-blog-decrypt', () => {
193193
194194// 搜索弹窗交互与搜索逻辑
195195( function ( ) {
196+ const theme_config = window . theme_config || { } ;
196197 if ( ! theme_config || ! theme_config . search || ! theme_config . search . enable ) {
197- return
198+ return ;
198199 }
199- let language = theme_config . language || 'zh-CN' ;
200+ const language = theme_config . language || 'zh-CN' ;
200201
201202 // 动态插入搜索弹窗结构到 body
202- var modalHtml = `
203+ const modalHtml = `
203204 <div id="search-modal" class="search-modal" style="display:none;">
204205 <div class="search-modal-mask"></div>
205206 <div class="search-modal-content">
@@ -211,57 +212,58 @@ window.addEventListener('hexo-blog-decrypt', () => {
211212 </div>
212213 </div>
213214 ` ;
214- var temp = document . createElement ( 'div' ) ;
215+ const temp = document . createElement ( 'div' ) ;
215216 temp . innerHTML = modalHtml ;
216217 document . body . appendChild ( temp . firstElementChild ) ;
217218
218219 // 创建右下角悬浮按钮
219- var floatBtn = document . createElement ( 'button' ) ;
220+ const floatBtn = document . createElement ( 'button' ) ;
220221 floatBtn . id = 'search-float-btn' ;
221222 floatBtn . title = language === 'en' ? 'Search' : '搜索' ;
222- var searchIcon = '<svg width="22" height="22" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8" stroke="#409eff" stroke-width="2" fill="none"/><line x1="17" y1="17" x2="21" y2="21" stroke="#409eff" stroke-width="2" stroke-linecap="round"/></svg>' ;
223- var closeIcon = '<svg width="22" height="22" viewBox="0 0 24 24"><line x1="6" y1="6" x2="18" y2="18" stroke="#f56c6c" stroke-width="2" stroke-linecap="round"/><line x1="18" y1="6" x2="6" y2="18" stroke="#f56c6c" stroke-width="2" stroke-linecap="round"/></svg>' ;
223+ const searchIcon = '<svg width="22" height="22" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8" stroke="#409eff" stroke-width="2" fill="none"/><line x1="17" y1="17" x2="21" y2="21" stroke="#409eff" stroke-width="2" stroke-linecap="round"/></svg>' ;
224+ const closeIcon = '<svg width="22" height="22" viewBox="0 0 24 24"><line x1="6" y1="6" x2="18" y2="18" stroke="#f56c6c" stroke-width="2" stroke-linecap="round"/><line x1="18" y1="6" x2="6" y2="18" stroke="#f56c6c" stroke-width="2" stroke-linecap="round"/></svg>' ;
224225 floatBtn . innerHTML = searchIcon ;
225226 document . body . appendChild ( floatBtn ) ;
226227
227- var modal = document . getElementById ( 'search-modal' ) ;
228- var closeBtn = document . getElementById ( 'search-modal-close' ) ;
229- var mask = document . querySelector ( '.search-modal-mask' ) ;
228+ const modal = document . getElementById ( 'search-modal' ) ;
229+ const closeBtn = document . getElementById ( 'search-modal-close' ) ;
230+ const mask = document . querySelector ( '.search-modal-mask' ) ;
230231
231232 // 搜索数据缓存
232- var posts = [ ] ;
233- var algoliaLoaded = false ;
234- var algoliaIndex = null ;
233+ let posts = [ ] ;
234+ let algoliaLoaded = false ;
235+ let algoliaIndex = null ;
235236
236237 // 搜索类型
237- var searchType = ` ${ theme_config . search . type || " json" } ` ;
238+ const searchType = theme_config . search . type || ' json' ;
238239
239240 // 打开弹窗时绑定 input 事件
240241 function openModal ( ) {
241242 modal . style . display = 'flex' ;
242243 floatBtn . innerHTML = closeIcon ;
243- var input = document . getElementById ( 'search-input' ) ;
244- var results = document . getElementById ( 'search-results' ) ;
245- setTimeout ( function ( ) { input && input . focus ( ) ; } , 100 ) ;
244+ const input = document . getElementById ( 'search-input' ) ;
245+ const results = document . getElementById ( 'search-results' ) ;
246+ setTimeout ( ( ) => { input && input . focus ( ) ; } , 100 ) ;
246247 // 解绑旧事件,防止多次绑定
247248 input . oninput = null ;
248249 if ( searchType === 'algolia' ) {
249250 if ( ! algoliaLoaded ) {
250- var appId = theme_config . search . algolia . appID ;
251- var apiKey = theme_config . search . algolia . apiKey ;
252- var indexName = theme_config . search . algolia . indexName ;
253- var script = document . createElement ( 'script' ) ;
251+ const appId = theme_config . search . algolia . appID ;
252+ const apiKey = theme_config . search . algolia . apiKey ;
253+ const indexName = theme_config . search . algolia . indexName ;
254+ const script = document . createElement ( 'script' ) ;
254255 script . src = 'https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js' ;
255256 script . onload = function ( ) {
256- var client = algoliasearch ( appId , apiKey ) ;
257+ // eslint-disable-next-line no-undef
258+ const client = algoliasearch ( appId , apiKey ) ;
257259 algoliaIndex = client . initIndex ( indexName ) ;
258260 algoliaLoaded = true ;
259261 } ;
260262 document . body . appendChild ( script ) ;
261263 }
262264 input . oninput = function ( ) {
263265 if ( ! algoliaIndex ) return ;
264- var keyword = this . value . trim ( ) ;
266+ const keyword = this . value . trim ( ) ;
265267 renderResults ( results , [ ] ) ;
266268 if ( ! keyword ) return ;
267269 algoliaIndex . search ( keyword ) . then ( ( { hits } ) => {
@@ -276,11 +278,11 @@ window.addEventListener('hexo-blog-decrypt', () => {
276278 . then ( data => { posts = data ; } ) ;
277279 }
278280 input . oninput = function ( ) {
279- var keyword = this . value . trim ( ) . toLowerCase ( ) ;
281+ const keyword = this . value . trim ( ) . toLowerCase ( ) ;
280282 if ( ! keyword ) return renderResults ( results , [ ] ) ;
281- var filtered = posts . filter ( post =>
282- ( post . title && post . title . toLowerCase ( ) . includes ( keyword ) ) ||
283- ( post . content && post . content . toLowerCase ( ) . includes ( keyword ) )
283+ const filtered = posts . filter ( post =>
284+ ( post . title && post . title . toLowerCase ( ) . includes ( keyword ) )
285+ || ( post . content && post . content . toLowerCase ( ) . includes ( keyword ) )
284286 ) ;
285287 renderResults ( results , filtered ) ;
286288 } ;
@@ -290,26 +292,26 @@ window.addEventListener('hexo-blog-decrypt', () => {
290292 function closeModal ( ) {
291293 modal . style . display = 'none' ;
292294 floatBtn . innerHTML = searchIcon ;
293- var input = document . getElementById ( 'search-input' ) ;
294- var results = document . getElementById ( 'search-results' ) ;
295+ const input = document . getElementById ( 'search-input' ) ;
296+ const results = document . getElementById ( 'search-results' ) ;
295297 if ( input ) input . value = '' ;
296298 if ( results ) results . innerHTML = '' ;
297299 }
298300
299301 function renderResults ( results , list ) {
300302 results . innerHTML = '' ;
301303 if ( ! list . length ) {
302- let text = theme_config . language === 'en' ? 'No results found' : '未找到结果' ;
303- results . innerHTML = '<li style="color:#888;padding:1em;">' + text + '</li>' ;
304+ const text = theme_config . language === 'en' ? 'No results found' : '未找到结果' ;
305+ results . innerHTML = '<li style="color:#888;padding:1em;">' + text + '</li>' ;
304306 return ;
305307 }
306- list . forEach ( function ( item ) {
307- var summary = '' ;
308+ list . forEach ( item => {
309+ let summary = '' ;
308310 if ( item . content ) {
309- var clean = item . content . replace ( / < [ ^ > ] + > / g, '' ) . replace ( / \n / g, '' ) ;
311+ const clean = item . content . replace ( / < [ ^ > ] + > / g, '' ) . replace ( / \n / g, '' ) ;
310312 summary = clean . length > 80 ? clean . slice ( 0 , 80 ) + '...' : clean ;
311313 }
312- var li = document . createElement ( 'li' ) ;
314+ const li = document . createElement ( 'li' ) ;
313315 li . innerHTML = `<a href="${ item . url || item . permalink } " target="_blank"><div class="search-title">${ item . title } </div><div class="search-summary">${ summary } </div></a>` ;
314316 results . appendChild ( li ) ;
315317 } ) ;
@@ -325,7 +327,7 @@ window.addEventListener('hexo-blog-decrypt', () => {
325327 } ;
326328 if ( closeBtn ) closeBtn . onclick = closeModal ;
327329 if ( mask ) mask . onclick = closeModal ;
328- window . addEventListener ( 'keydown' , function ( e ) {
330+ window . addEventListener ( 'keydown' , e => {
329331 if ( e . key === 'Escape' ) closeModal ( ) ;
330332 } ) ;
331- } ) ( ) ;
333+ } ( ) ) ;
0 commit comments