mirror of
https://gitee.com/docsifyjs/docsify.git
synced 2024-12-05 05:29:12 +08:00
2 lines
4.2 KiB
JavaScript
2 lines
4.2 KiB
JavaScript
this.D=this.D||{},function(){"use strict";function e(e){var n={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"};return String(e).replace(/[&<>"'\/]/g,function(e){return n[e]})}function n(){var e=[];return d.dom.findAll("a").map(function(n){var t=n.href,r=n.getAttribute("href"),i=d.route.parse(t).path;i&&e.indexOf(i)===-1&&!d.route.isAbsolutePath(r)&&e.push(i)}),e}function t(e){localStorage.setItem("docsify.search.expires",Date.now()+e),localStorage.setItem("docsify.search.index",JSON.stringify(f))}function r(e,n){void 0===n&&(n="");var t,r=window.marked.lexer(n),i=Docsify.route.toURL,o={};return r.forEach(function(n){if("heading"===n.type&&n.depth<=2)t=i(e,{id:n.text}),o[t]={slug:t,title:n.text,body:""};else{if(!t)return;o[t]?o[t].body?o[t].body+="\n"+(n.text||""):o[t].body=n.text:o[t]={slug:t,title:"",body:""}}}),o}function i(n){var t=[],r=[];Object.keys(f).forEach(function(e){r=r.concat(Object.keys(f[e]).map(function(n){return f[e][n]}))}),n=n.trim().split(/[\s\-\,\\\/]+/);for(var i=function(i){var o=r[i],a=!1,s="",c=o.title&&o.title.trim(),l=o.body&&o.body.trim(),p=o.slug||"";if(c&&l&&(n.forEach(function(n,t){var r=new RegExp(n,"gi"),i=-1,o=-1;if(i=c&&c.search(r),o=l&&l.search(r),i<0&&o<0)a=!1;else{a=!0,o<0&&(o=0);var p=0,u=0;p=o<11?0:o-10,u=0===p?70:o+n.length+60,u>l.length&&(u=l.length);var d="..."+e(l).substring(p,u).replace(r,'<em class="search-keyword">'+n+"</em>")+"...";s+=d}}),a)){var u={title:e(c),content:s,url:p};t.push(u)}},o=0;o<r.length;o++)i(o);return t}function o(e,i){d=Docsify;var o="auto"===e.paths,a=localStorage.getItem("docsify.search.expires")<Date.now();if(f=JSON.parse(localStorage.getItem("docsify.search.index")),a)f={};else if(!o)return;var s=o?n():e.paths,c=s.length,l=0;s.forEach(function(n){return f[n]?l++:void d.get(i.$getFile(n)).then(function(i){f[n]=r(n,i),c===++l&&t(e.maxAge)})})}function a(){var e="\n.sidebar {\n padding-top: 0;\n}\n\n.search {\n margin-bottom: 20px;\n padding: 6px;\n border-bottom: 1px solid #eee;\n}\n\n.search .results-panel {\n display: none;\n}\n\n.search .results-panel.show {\n display: block;\n}\n\n.search input {\n outline: none;\n border: none;\n width: 100%;\n padding: 7px;\n line-height: 22px;\n font-size: 14px;\n}\n\n.search h2 {\n font-size: 17px;\n margin: 10px 0;\n}\n\n.search a {\n text-decoration: none;\n color: inherit;\n}\n\n.search .matching-post {\n border-bottom: 1px solid #eee;\n}\n\n.search .matching-post:last-child {\n border-bottom: 0;\n}\n\n.search p {\n font-size: 14px;\n overflow: hidden;\n text-overflow: ellipsis;\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n}\n\n.search p.empty {\n text-align: center;\n}",n=h.create("style",e);h.appendTo(h.head,n)}function s(e){var n='<input type="search" /><div class="results-panel"></div></div>',t=h.create("div",n),r=h.find("aside");h.toggleClass(t,"search"),h.before(r,t)}function c(){var e,n=h.find("div.search"),t=h.find(n,"input"),r=h.find(n,".results-panel"),o=function(e){if(!e)return r.classList.remove("show"),void(r.innerHTML="");var n=i(e),t="";n.forEach(function(e){t+='<div class="matching-post">\n <h2><a href="'+e.url+'">'+e.title+"</a></h2>\n <p>"+e.content+"</p>\n</div>"}),r.classList.add("show"),r.innerHTML=t||'<p class="empty">No Results!</p>'};h.on(n,"click",function(e){return"A"!==e.target.tagName&&e.stopPropagation()}),h.on(t,"input",function(n){clearTimeout(e),e=setTimeout(function(e){return o(n.target.value.trim())},200)})}function l(e,n){var t=h.getNode('.search input[type="search"]');if("string"==typeof e)t.placeholder=e;else{var r=Object.keys(e).find(function(e){return n.indexOf(e)>-1});t.placeholder=e[r]}}function p(e){h=Docsify.dom,a(),s(e),c()}function u(e,n){l(e.placeholder,n.route.path)}var d,h,f={},g={placeholder:"Type to search",paths:"auto",maxAge:864e5},m=function(e,n){var t=Docsify.util,r=n.config.search||g;Array.isArray(r)?g.paths=r:"object"==typeof r&&(g.paths=Array.isArray(r.paths)?r.paths:"auto",g.maxAge=t.isPrimitive(r.maxAge)?r.maxAge:g.maxAge,g.placeholder=r.placeholder||g.placeholder);var i="auto"===g.paths;e.mounted(function(e){p(g),!i&&o(g,n)}),e.doneEach(function(e){u(g,n),i&&o(g,n)})};window.$docsify.plugins=[].concat(m,window.$docsify.plugins)}();
|