mirror of
https://gitee.com/docsifyjs/docsify.git
synced 2024-12-01 11:39:41 +08:00
2 lines
4.5 KiB
JavaScript
2 lines
4.5 KiB
JavaScript
this.D=this.D||{},function(){"use strict";function n(n){var e={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"};return String(n).replace(/[&<>"'\/]/g,function(n){return e[n]})}function e(){var n=[];return d.dom.findAll("a:not([data-nosearch])").map(function(e){var t=e.href,a=e.getAttribute("href"),o=d.route.parse(t).path;o&&n.indexOf(o)===-1&&!d.route.isAbsolutePath(a)&&n.push(o)}),n}function t(n){localStorage.setItem("docsify.search.expires",Date.now()+n),localStorage.setItem("docsify.search.index",JSON.stringify(g))}function a(n,e){void 0===e&&(e="");var t,a=window.marked.lexer(e),o=window.Docsify.slugify,r=Docsify.route.toURL,i={};return a.forEach(function(e){if("heading"===e.type&&e.depth<=2)t=r(n,{id:o(e.text)}),i[t]={slug:t,title:e.text,body:""};else{if(!t)return;i[t]?i[t].body?i[t].body+="\n"+(e.text||""):i[t].body=e.text:i[t]={slug:t,title:"",body:""}}}),o.clear(),i}function o(e){var t=[],a=[];Object.keys(g).forEach(function(n){a=a.concat(Object.keys(g[n]).map(function(e){return g[n][e]}))}),e=[].concat(e,e.trim().split(/[\s\-\,\\\/]+/));for(var o=function(o){var r=a[o],i=!1,s="",c=r.title&&r.title.trim(),l=r.body&&r.body.trim(),p=r.slug||"";if(c&&l&&(e.forEach(function(e,t){var a=new RegExp(e,"gi"),o=-1,r=-1;if(o=c&&c.search(a),r=l&&l.search(a),o<0&&r<0)i=!1;else{i=!0,r<0&&(r=0);var p=0,u=0;p=r<11?0:r-10,u=0===p?70:r+e.length+60,u>l.length&&(u=l.length);var f="..."+n(l).substring(p,u).replace(a,'<em class="search-keyword">'+e+"</em>")+"...";s+=f}}),i)){var u={title:n(c),content:s,url:p};t.push(u)}},r=0;r<a.length;r++)o(r);return t}function r(n,o){d=Docsify;var r="auto"===n.paths,i=localStorage.getItem("docsify.search.expires")<Date.now();if(g=JSON.parse(localStorage.getItem("docsify.search.index")),i)g={};else if(!r)return;var s=r?e():n.paths,c=s.length,l=0;s.forEach(function(e){return g[e]?l++:void d.get(o.$getFile(e)).then(function(o){g[e]=a(e,o),c===++l&&t(n.maxAge)})})}function i(){var n="\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 -webkit-appearance: none;\n -moz-appearance: none;\n appearance: none;\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}",e=h.create("style",n);h.appendTo(h.head,e)}function s(n){var e='<input type="search" /><div class="results-panel"></div></div>',t=h.create("div",e),a=h.find("aside");h.toggleClass(t,"search"),h.before(a,t)}function c(){var n,e=h.find("div.search"),t=h.find(e,"input"),a=h.find(e,".results-panel"),r=function(n){if(!n)return a.classList.remove("show"),void(a.innerHTML="");var e=o(n),t="";e.forEach(function(n){t+='<div class="matching-post">\n <h2><a href="'+n.url+'">'+n.title+"</a></h2>\n <p>"+n.content+"</p>\n</div>"}),a.classList.add("show"),a.innerHTML=t||'<p class="empty">'+y+"</p>"};h.on(e,"click",function(n){return"A"!==n.target.tagName&&n.stopPropagation()}),h.on(t,"input",function(e){clearTimeout(n),n=setTimeout(function(n){return r(e.target.value.trim())},100)})}function l(n,e){var t=h.getNode('.search input[type="search"]');if("string"==typeof n)t.placeholder=n;else{var a=Object.keys(n).find(function(n){return e.indexOf(n)>-1});t.placeholder=n[a]}}function p(n,e){if("string"==typeof n)y=n;else{var t=Object.keys(n).find(function(n){return e.indexOf(n)>-1});y=n[t]}}function u(n){h=Docsify.dom,i(),s(n),c()}function f(n,e){l(n.placeholder,e.route.path),p(n.noData,e.route.path)}var d,h,g={},y="",m={placeholder:"Type to search",noData:"No Results!",paths:"auto",maxAge:864e5},v=function(n,e){var t=Docsify.util,a=e.config.search||m;Array.isArray(a)?m.paths=a:"object"==typeof a&&(m.paths=Array.isArray(a.paths)?a.paths:"auto",m.maxAge=t.isPrimitive(a.maxAge)?a.maxAge:m.maxAge,m.placeholder=a.placeholder||m.placeholder,m.noData=a.noData||m.noData);var o="auto"===m.paths;n.mounted(function(n){u(m),!o&&r(m,e)}),n.doneEach(function(n){f(m,e),o&&r(m,e)})};window.$docsify.plugins=[].concat(v,window.$docsify.plugins)}();
|