-
Notifications
You must be signed in to change notification settings - Fork 0
/
zh-cn.search.min.93b3e40a778092685b7ab437ade443a84d44572aa24c83aea013b58514429928.js
8 lines (8 loc) · 5.01 KB
/
zh-cn.search.min.93b3e40a778092685b7ab437ade443a84d44572aa24c83aea013b58514429928.js
1
2
3
4
5
6
7
8
document.addEventListener("DOMContentLoaded",function(){if(/iPad|iPhone|Macintosh/.test(navigator.userAgent)){const e=document.querySelectorAll(".search-wrapper kbd");e.forEach(e=>{e.innerHTML='<span class="text-xs">⌘</span>K'})}}),function(){const l="/zh-cn.search-data.json",d=document.querySelectorAll(".search-input");for(const e of d)e.addEventListener("focus",s),e.addEventListener("keyup",h),e.addEventListener("keydown",o);function e(){const e=Array.from(document.querySelectorAll(".search-wrapper")).filter(e=>e.clientHeight>0);return e.length===1?{wrapper:e[0],inputElement:e[0].querySelector(".search-input"),resultsElement:e[0].querySelector(".search-results")}:void 0}const r=["input","select","button","textarea"];document.addEventListener("keydown",function(t){const{inputElement:n}=e();if(!n)return;const s=document.activeElement,o=s&&s.tagName;if(n===s||!o||r.includes(o)||s&&s.isContentEditable)return;t.key==="/"||t.key==="k"&&(t.metaKey||t.ctrlKey)?(t.preventDefault(),n.focus()):t.key==="Escape"&&n.value&&n.blur()}),document.addEventListener("mousedown",function(n){const{inputElement:o,resultsElement:s}=e();if(!o||!s)return;n.target!==o&&n.target!==s&&!s.contains(n.target)&&t()});function i(){const{resultsElement:n}=e();if(!n)return{result:void 0,index:-1};const t=n.querySelector(".active");if(!t)return{result:void 0,index:-1};const s=parseInt(t.dataset.index,10);return{result:t,index:s}}function a(t){const{resultsElement:s}=e();if(!s)return;const{result:o}=i();o&&o.classList.remove("active");const n=s.querySelector(`[data-index="${t}"]`);n&&(n.classList.add("active"),n.focus())}function c(){const{resultsElement:t}=e();return t?t.dataset.count:0}function n(){const{inputElement:n}=e();if(!n)return;t(),n.value="",n.blur()}function t(){const{resultsElement:t}=e();if(!t)return;t.classList.add("hidden")}function o(s){const{inputElement:r}=e();if(!r)return;const d=c(),{result:l,index:o}=i();switch(s.key){case"ArrowUp":s.preventDefault(),o>0&&a(o-1);break;case"ArrowDown":s.preventDefault(),o+1<d&&a(o+1);break;case"Enter":s.preventDefault(),l&&l.click(),n();case"Escape":s.preventDefault(),t(),r.blur();break}}function s(e){e.target.removeEventListener("focus",s),window.pageIndex&&window.sectionIndex||u()}async function u(){window.pageIndex=new FlexSearch.Document({tokenize:"forward",cache:100,document:{id:"id",store:["title"],index:"content"}}),window.sectionIndex=new FlexSearch.Document({tokenize:"forward",cache:100,document:{id:"id",store:["title","content","url","display"],index:"content",tag:"pageId"}});const n=await fetch(l),e=await n.json();let t=0;for(const n in e){let s="";++t;for(const r in e[n].data){const[c,d]=r.split("#"),o=n.trimEnd("/")+(c?"#"+c:""),i=d||e[n].title,l=e[n].data[r]||"",a=l.split(`
`).filter(Boolean);sectionIndex.add({id:o,url:o,title:i,pageId:`page_${t}`,content:i,...a[0]&&{display:a[0]}});for(let e=0;e<a.length;e++)sectionIndex.add({id:`${o}_${e}`,url:o,title:i,pageId:`page_${t}`,content:a[e]});s+=` ${i} ${l}`}window.pageIndex.add({id:t,title:e[n].title,content:s})}}function h(n){const i=n.target.value;if(!n.target.value){t();return}const{resultsElement:o}=e();for(;o.firstChild;)o.removeChild(o.firstChild);o.classList.remove("hidden");const a=window.pageIndex.search(i,5,{enrich:!0,suggest:!0})[0]?.result||[],r=[],s={};for(let e=0;e<a.length;e++){const t=a[e];s[e]=0;const n=window.sectionIndex.search(i,5,{enrich:!0,suggest:!0,tag:`page_${t.id}`})[0]?.result||[];let o=!0;const c={};for(let i=0;i<n.length;i++){const{doc:a}=n[i],u=a.display!==void 0;u&&s[e]++;const{url:l,title:h}=a,d=a.display||a.content;if(c[l+"@"+d])continue;c[l+"@"+d]=!0,r.push({_page_rk:e,_section_rk:i,route:l,prefix:o?t.doc.title:void 0,children:{title:h,content:d}}),o=!1}}const c=r.sort((e,t)=>e._page_rk===t._page_rk?e._section_rk-t._section_rk:s[e._page_rk]!==s[t._page_rk]?s[t._page_rk]-s[e._page_rk]:e._page_rk-t._page_rk).map(e=>({id:`${e._page_rk}_${e._section_rk}`,route:e.route,prefix:e.prefix,children:e.children}));m(c,i)}function m(t,s){const{resultsElement:i}=e();if(!i)return;if(!t.length){i.innerHTML=`<span class="no-result">无结果</span>`;return}function r(e,t){const n=t.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&"),s=new RegExp(n,"gi");return e.replace(s,e=>`<span class="match">${e}</span>`)}function c(e){const t=document.createElement("div");return t.innerHTML=e.trim(),t.firstChild}function l(e){const t=e.target.closest("a");if(t){const e=i.querySelector("a.active");e&&e.classList.remove("active"),t.classList.add("active")}}const a=document.createDocumentFragment();for(let i=0;i<t.length;i++){const e=t[i];e.prefix&&a.appendChild(c(`
<div class="prefix">${e.prefix}</div>`));let d=c(`
<li>
<a data-index="${i}" href="${e.route}" class=${i===0?"active":""}>
<div class="title">`+r(e.children.title,s)+`</div>`+(e.children.content?`<div class="excerpt">`+r(e.children.content,s)+`</div>`:"")+`
</a>
</li>`);d.addEventListener("mousemove",l),d.addEventListener("keydown",o),d.querySelector("a").addEventListener("click",n),a.appendChild(d)}i.appendChild(a),i.dataset.count=t.length}}()