blog/js/local-search.min.js

3 lines
4.3 KiB
JavaScript

"use strict";function searchEscape(e){var t={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;"};return e.replace(/[&<>"'/]/g,(function(e){return t[e]}))}function regEscape(e){var t={"{":"\\{","}":"\\}","[":"\\[","]":"\\]","(":"\\(",")":"\\)","?":"\\?","*":"\\*",".":"\\.","+":"\\+","^":"\\^",$:"\\$"};return e.replace(/[\{\}\[\]\(\)\?\*\.\+\^\$]/g,(function(e){return t[e]}))}function getParam(e){e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var t=new RegExp("[\\?&]"+e+"=([^&#]*)").exec(window.location);return null===t?"":decodeURIComponent(t[1].replace(/\+/g," "))}function setNotice(e,t){var n=document.getElementById("kr-search-notice");n.className="alert alert-"+e,n.innerText=t}function clearPosts(){document.getElementById("result-posts").innerHTML=""}function createPosts(e){var t=document.getElementById("result-posts"),n="";e.forEach((function(e){var t=e[0],c="";t.tags.forEach((function(e,t){c+=t?", ":"";var n='<a class="tag-link" href="'.concat(e[1],'" rel="tag">').concat(e[0],"</a>");c+=n}));var a='\n <article class="kratos-hentry clearfix">\n <div class="kratos-entry-border-new clearfix">\n <div class="kratos-post-inner-new kr-search-result">\n <header class="kratos-entry-header-new">\n '.concat("undefined"!==t.category[0]?'<a class="label-link" href="'.concat(t.category[1],'">').concat(t.category[0],"</a>"):"",'\n <h2 class="kratos-entry-title-new"><a href="').concat(t.link,'">').concat(t.title,'</a></h2>\n </header>\n <div class="kratos-entry-content-new">\n <p>...').concat(t.content,'...</p>\n </div>\n </div>\n <div class="kratos-post-meta-new">\n <span class="pull-left">\n <a><i class="fa fa-calendar"></i></a><a>').concat(t.date,'</a>\n <a><i class="fa fa-tags"></i></a>\n ').concat(c,"\n </span>\n </div>\n </div>\n </article>\n ");n+=a})),t.innerHTML=n}function loadDataSearch(e,t){fetch(e).then((function(e){return setNotice("success","文件加载完成,开始搜索啦~"),"undefined"!=typeof NProgress&&NProgress.inc(),e.json()})).then((function(e){var n=performance.now(),c=[],a=0,r=t.trim().toLowerCase().split(/\s/);if(e.forEach((function(e){if("undefined"!=typeof e.title&&"undefined"!=typeof e.content){var t=!1,n=e.title.trim().toLowerCase(),o=e.content?e.content.trim().replace(/<[^>]+>/g,"").toLowerCase():"",s=0,i={title:-1,content:-1,firstOccur:-1,lastOccur:-1},l=100;if(n&&r.forEach((function(e){i.title=n.indexOf(e),i.content=o.indexOf(e),-1===i.title&&-1===i.content||(t=!0,-1!==i.content?((i.firstOccur>i.content||-1===i.firstOccur)&&(i.firstOccur=i.content),i.lastOccur<i.content&&(i.lastOccur=i.content)):(i.firstOccur=l,i.lastOccur=0),s+=-1!==i.title?2:0,s+=-1!==i.content?1:0,a++)})),t){var u={};if(u.title=e.title,u.date=new Date(e.date).toLocaleDateString(),u.tags=e.tags||[],u.category=e.categories[0]||[],u.link=e.url,r.forEach((function(e){var t=new RegExp(regEscape(e)+"(?!>)","gi");u.title=u.title.replace(t,"<m>$&</m>")})),i.firstOccur>=0){var f=i.firstOccur-l,d=i.lastOccur+l;f<0&&(f=0),0===f&&(d=200),d>o.length&&(d=o.length),u.content=o.substr(f,d-f),r.forEach((function(e){var t=new RegExp(regEscape(e)+"(?!>)","gi");u.content=u.content.replace(t,"<m>$&</m>")}))}c.push([u,s])}}})),0!==a){var o=performance.now();setNotice("success","找到 "+a+" 条搜索结果,用时 "+Math.round(100*(o-n))/100+" 毫秒~"),c.sort((function(e,t){return t[1]-e[1]})),createPosts(c)}else setNotice("danger","什么都没有找到欸..."),clearPosts();"undefined"!=typeof NProgress&&NProgress.done()}))["catch"]((function(e){setNotice("danger","错误 : "+e)}))}function keySearch(e){setNotice("info","正在加载搜索文件..."),"undefined"!=typeof NProgress&&NProgress.start(),loadDataSearch(searchDataFile,searchEscape(e))}function inpSearch(){var e=document.getElementById("search-input").value;return window.history.pushState({},0,window.location.href.split("?")[0]+"?s="+e.replace(/\s/g,"+")),keySearch(e),!1}!function(){var e=getParam("s");""!==e&&(document.getElementById("search-input").value=e,keySearch(e))}();
//# sourceMappingURL=../maps/local-search.min.js.map