Packaged project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
4.1 KiB

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