-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.min.js
6 lines (6 loc) · 4.57 KB
/
search.min.js
1
2
3
4
5
6
/*!
* Simple-Jekyll-Search (modified to decode base64-encoded HTML)
* Copyright 2015-2020, Christian Fei
* Licensed under the MIT License.
*/
!function(){"use strict";var t={compile:function(t){return e.template.replace(e.pattern,function(n,r){const o=e.middleware(r,t[r],e.template);return void 0!==o?o:t[r]||n})},setOptions:function(t){e.pattern=t.pattern||e.pattern,e.template=t.template||e.template,"function"==typeof t.middleware&&(e.middleware=t.middleware)}};const e={};e.pattern=/\{(.*?)\}/g,e.template="",e.middleware=function(){};var n=function(t,e){var n=e.length,r=t.length;if(r>n)return!1;if(r===n)return t===e;t:for(var o=0,i=0;o<r;o++){for(var u=t.charCodeAt(o);i<n;)if(e.charCodeAt(i++)===u)continue t;return!1}return!0},r=new function(){this.matches=function(t,e){return n(e.toLowerCase(),t.toLowerCase())}};var o=new function(){this.matches=function(t,e){return!!t&&(t=t.trim().toLowerCase(),(e=e.trim().toLowerCase()).split(" ").filter(function(e){return t.indexOf(e)>=0}).length===e.split(" ").length)}};var i={put:function(t){if(a(t))return f(t);if(e=t,Boolean(e)&&"[object Array]"===Object.prototype.toString.call(e))return function(t){const e=[];l();for(let n=0,r=t.length;n<r;n++)a(t[n])&&e.push(f(t[n]));return e}(t);var e;return},clear:l,search:function(t){if(!t)return[];return function(t,e,n,r){const o=[];for(let i=0;i<t.length&&o.length<r.limit;i++){const u=d(t[i],e,n,r);u&&o.push(u)}return o}(c,t,s.searchStrategy,s).sort(s.sort)},setOptions:function(t){(s=t||{}).fuzzy=t.fuzzy||!1,s.limit=t.limit||10,s.searchStrategy=t.fuzzy?r:o,s.sort=t.sort||u,s.exclude=t.exclude||[]}};function u(){return 0}const c=[];let s={};function l(){return c.length=0,c}function a(t){return Boolean(t)&&"[object Object]"===Object.prototype.toString.call(t)}function f(t){return c.push(t),c}function d(t,e,n,r){for(const o in t)if(!p(t[o],r.exclude)&&n.matches(t[o],e))return"y"===t.isencoded&&(t.img=w(t.img),t.isencoded="n"),t}function p(t,e){for(let n=0,r=e.length;n<r;n++){const r=e[n];if(new RegExp(r).test(t))return!0}return!1}s.fuzzy=!1,s.limit=10,s.searchStrategy=s.fuzzy?r:o,s.sort=u,s.exclude=[];var h={load:function(t,e){const n=window.XMLHttpRequest?new window.XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");n.open("GET",t,!0),n.onreadystatechange=function(t,e){return function(){if(4===t.readyState&&200===t.status)try{e(null,JSON.parse(t.responseText))}catch(t){e(t,null)}}}(n,e),n.send()}};var m={merge:function(t,e){const n={};for(const r in t)n[r]=t[r],void 0!==e[r]&&(n[r]=e[r]);return n},isJSON:function(t){try{return!!(t instanceof Object&&JSON.parse(JSON.stringify(t)))}catch(t){return!1}}};function w(t){if("function"==typeof atob)return atob(t);if("function"==typeof Buffer)return Buffer.from(t,"base64").toString("utf-8");throw new Error("Failed to determine the platform specific decoder")}!function(e){let n,r={searchInput:null,resultsContainer:null,json:[],success:Function.prototype,searchResultTemplate:'<li><a href="{url}" title="{desc}">{title}</a></li>',templateMiddleware:Function.prototype,sortMiddleware:function(){return 0},noResultsText:"No results found",limit:10,fuzzy:!1,debounceTime:null,exclude:[]};const o=function(t,e){e?(clearTimeout(n),n=setTimeout(t,e)):t.call()},u=["searchInput","resultsContainer","json"],c=function t(e){if(!function(t){return!!t&&(void 0!==t.required&&t.required instanceof Array)}(e))throw new Error("-- OptionsValidator: required options missing");if(!(this instanceof t))return new t(e);const n=e.required;this.getRequiredOptions=function(){return n},this.validate=function(t){const e=[];return n.forEach(function(n){void 0===t[n]&&e.push(n)}),e}}({required:u});function s(t){i.put(t),r.searchInput.addEventListener("input",function(t){var e;e=t.which,-1===[13,16,20,37,38,39,40,91].indexOf(e)&&(l(),o(function(){f(t.target.value)},r.debounceTime))})}function l(){r.resultsContainer.innerHTML=""}function a(t){r.resultsContainer.innerHTML+=t}function f(e){(function(t){return t&&t.length>0})(e)&&(l(),function(e,n){const o=e.length;if(0===o)return a(r.noResultsText);for(let r=0;r<o;r++)e[r].query=n,a(t.compile(e[r]))}(i.search(e),e))}function d(t){throw new Error("SimpleJekyllSearch --- "+t)}e.SimpleJekyllSearch=function(e){var n;c.validate(e).length>0&&d("You must specify the following required options: "+u),r=m.merge(r,e),t.setOptions({template:r.searchResultTemplate,middleware:r.templateMiddleware}),i.setOptions({fuzzy:r.fuzzy,limit:r.limit,sort:r.sortMiddleware,exclude:r.exclude}),m.isJSON(r.json)?s(r.json):(n=r.json,h.load(n,function(t,e){t&&d("failed to get JSON ("+n+")"),s(e)}));const o={search:f};return"function"==typeof r.success&&r.success.call(o),o}}(window)}();