generated from silverbulletmd/silverbullet-plug-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.plug.js
More file actions
1 lines (1 loc) · 6.94 KB
/
search.plug.js
File metadata and controls
1 lines (1 loc) · 6.94 KB
1
var N=Object.create;var A=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var Y=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),J=(e,t)=>{for(var i in t)A(e,i,{get:t[i],enumerable:!0})},X=(e,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of _(t))!G.call(e,n)&&n!==i&&A(e,n,{get:()=>t[n],enumerable:!(s=V(t,n))||s.enumerable});return e};var Z=(e,t,i)=>(i=e!=null?N(H(e)):{},X(t||!e||!e.__esModule?A(i,"default",{value:e,enumerable:!0}):i,e));var O=Y(v=>{(function(){var e={ational:"ate",tional:"tion",enci:"ence",anci:"ance",izer:"ize",bli:"ble",alli:"al",entli:"ent",eli:"e",ousli:"ous",ization:"ize",ation:"ate",ator:"ate",alism:"al",iveness:"ive",fulness:"ful",ousness:"ous",aliti:"al",iviti:"ive",biliti:"ble",logi:"log"},t={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},i="[^aeiou]",s="[aeiouy]",n=i+"[^aeiouy]*",p=s+"[aeiou]*",u="^("+n+")?"+p+n,m="^("+n+")?"+p+n+"("+p+")?$",f="^("+n+")?"+p+n+p+n,g="^("+n+")?"+s;function y(r){var c,b,P,o,l,T,k,Ue=r;if(r.length<3)return r;if(P=r.substr(0,1),P=="y"&&(r=P.toUpperCase()+r.substr(1)),o=/^(.+?)(ss|i)es$/,l=/^(.+?)([^s])s$/,o.test(r)?r=r.replace(o,"$1$2"):l.test(r)&&(r=r.replace(l,"$1$2")),o=/^(.+?)eed$/,l=/^(.+?)(ed|ing)$/,o.test(r)){var d=o.exec(r);o=new RegExp(u),o.test(d[1])&&(o=/.$/,r=r.replace(o,""))}else if(l.test(r)){var d=l.exec(r);c=d[1],l=new RegExp(g),l.test(c)&&(r=c,l=/(at|bl|iz)$/,T=new RegExp("([^aeiouylsz])\\1$"),k=new RegExp("^"+n+s+"[^aeiouwxy]$"),l.test(r)?r=r+"e":T.test(r)?(o=/.$/,r=r.replace(o,"")):k.test(r)&&(r=r+"e"))}if(o=/^(.+?)y$/,o.test(r)){var d=o.exec(r);c=d[1],o=new RegExp(g),o.test(c)&&(r=c+"i")}if(o=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,o.test(r)){var d=o.exec(r);c=d[1],b=d[2],o=new RegExp(u),o.test(c)&&(r=c+e[b])}if(o=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,o.test(r)){var d=o.exec(r);c=d[1],b=d[2],o=new RegExp(u),o.test(c)&&(r=c+t[b])}if(o=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/,l=/^(.+?)(s|t)(ion)$/,o.test(r)){var d=o.exec(r);c=d[1],o=new RegExp(f),o.test(c)&&(r=c)}else if(l.test(r)){var d=l.exec(r);c=d[1]+d[2],l=new RegExp(f),l.test(c)&&(r=c)}if(o=/^(.+?)e$/,o.test(r)){var d=o.exec(r);c=d[1],o=new RegExp(f),l=new RegExp(m),T=new RegExp("^"+n+s+"[^aeiouwxy]$"),(o.test(c)||l.test(c)&&!T.test(c))&&(r=c)}return o=/ll$/,l=new RegExp(f),o.test(r)&&l.test(r)&&(o=/.$/,r=r.replace(o,"")),P=="y"&&(r=P.toLowerCase()+r.substr(1)),r}var C={},z=function(r){return C[r]||(C[r]=y(r)),C[r]};typeof v<"u"&&v!=null&&(v.stemmer=y,v.memoizingStemmer=z)})()});function D(e){let t=atob(e),i=t.length,s=new Uint8Array(i);for(let n=0;n<i;n++)s[n]=t.charCodeAt(n);return s}function M(e){typeof e=="string"&&(e=new TextEncoder().encode(e));let t="",i=e.byteLength;for(let s=0;s<i;s++)t+=String.fromCharCode(e[s]);return btoa(t)}var h=e=>{throw new Error("Not initialized yet")},F=typeof window>"u"&&typeof globalThis.WebSocketPair>"u";typeof Deno>"u"&&(self.Deno={args:[],build:{arch:"x86_64"},env:{get(){}}});var E=new Map,S=0;F&&(globalThis.syscall=async(e,...t)=>await new Promise((i,s)=>{S++,E.set(S,{resolve:i,reject:s}),h({type:"sys",id:S,name:e,args:t})}));function K(e,t,i){F&&(h=i,self.addEventListener("message",s=>{(async()=>{let n=s.data;switch(n.type){case"inv":{let p=e[n.name];if(!p)throw new Error(`Function not loaded: ${n.name}`);try{let u=await Promise.resolve(p(...n.args||[]));h({type:"invr",id:n.id,result:u})}catch(u){console.error("An exception was thrown as a result of invoking function",n.name,"error:",u.message),h({type:"invr",id:n.id,error:u.message})}}break;case"sysr":{let p=n.id,u=E.get(p);if(!u)throw Error("Invalid request id");E.delete(p),n.error?u.reject(new Error(n.error)):u.resolve(n.result)}break}})().catch(console.error)}),h({type:"manifest",manifest:t}))}async function ee(e,t){if(typeof e!="string"){let i=new Uint8Array(await e.arrayBuffer()),s=i.length>0?M(i):void 0;t={method:e.method,headers:Object.fromEntries(e.headers.entries()),base64Body:s},e=e.url}return syscall("sandboxFetch.fetch",e,t)}globalThis.nativeFetch=globalThis.fetch;function te(){globalThis.fetch=async function(e,t){let i=t&&t.body?M(new Uint8Array(await new Response(t.body).arrayBuffer())):void 0,s=await ee(e,t&&{method:t.method,headers:t.headers,base64Body:i});return new Response(s.base64Body?D(s.base64Body):null,{status:s.status,headers:s.headers})}}F&&te();function R(e){if(!e)return"";let t=[];if(e.text!==void 0)return e.text;for(let i of e.children)t.push(R(i));return t.join("")}typeof self>"u"&&(self={syscall:()=>{throw new Error("Not implemented here")}});function a(e,...t){return globalThis.syscall(e,...t)}var x={};J(x,{cleanDatabases:()=>me,getConfig:()=>pe,getMode:()=>le,getVersion:()=>de,invokeCommand:()=>se,invokeFunction:()=>ie,listCommands:()=>ae,listSyscalls:()=>ce,reloadPlugs:()=>ue,wipeClient:()=>fe});function ie(e,...t){return a("system.invokeFunction",e,...t)}function se(e,t){return a("system.invokeCommand",e,t)}function ae(){return a("system.listCommands")}function ce(){return a("system.listSyscalls")}function ue(){return a("system.reloadPlugs")}function le(){return a("system.getMode")}function de(){return a("system.getVersion")}function pe(e,t=void 0){return a("system.getConfig",e,t)}function fe(e=!1){return a("system.wipeClient",e)}function me(){return a("system.cleanDatabases")}var Ve=new Uint8Array(16);var $=Z(O()),Fe=["and","or","the","a","an"];function U(e){return e.toLowerCase().split(/[^\p{L}]+/u)}function L(e){return e.filter(t=>t.length>2&&!Fe.includes(t)&&/^\p{L}+$/u.test(t))}function q(e){return(0,$.stemmer)(e)}async function B(e,t){let i=new Map,s=U(e),n=U(t),p=[...s,...n],m=L(p).map(q);for(let f of m){let g=i.get(f)||0;i.set(f,g+1)}await x.invokeFunction("index.batchSet",e,[...i.entries()].map(([f,g])=>({key:["fts",f],value:g})))}async function j(e){let t=U(e),s=L(t).map(u=>q(u)),n=new Map;for(let u of s){let m=await x.invokeFunction("index.query",{prefix:["fts",u]});for(let{key:f,value:g}of m){let y=f[2];n.has(y)?n.set(y,n.get(y)+g):n.set(y,g)}}return Array.from(n.entries()).map(([u,m])=>({id:u,score:m})).sort((u,m)=>m.score-u.score)}var w=class{queue=[];processing=!1;runInQueue(t){return new Promise((i,s)=>{this.queue.push({fn:t,resolve:i,reject:s}),this.processing||this.process()})}async process(){if(this.queue.length===0){this.processing=!1;return}this.processing=!0;let{fn:t,resolve:i,reject:s}=this.queue.shift();try{let n=await t();i(n)}catch(n){s(n)}this.process()}};var Re=new w;async function I({name:e,tree:t}){if(!await x.getConfig("index.search.enabled",!0))return;let i=R(t);return Re.runInQueue(async()=>{await B(e,i)})}var W={indexPage:I,ftsSearch:j},Q={name:"search",functions:{indexPage:{path:"search.ts:indexPage",events:["page:index"]},ftsSearch:{path:"./engine.ts:ftsSearch",syscall:"search.ftsSearch"}},assets:{}},vt={manifest:Q,functionMapping:W};K(W,Q,self.postMessage);export{vt as plug};