6 lines
40 KiB
JavaScript
6 lines
40 KiB
JavaScript
const pagefind_version="1.5.2";let wasm_bindgen=(function(exports){let script_src;if(typeof document!=='undefined'&&document.currentScript!==null){script_src=new URL("UNHANDLED",location.href).toString();}function add_synthetic_filter(ptr,filter){const ptr0=passStringToWasm0(filter,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len0=WASM_VECTOR_LEN;const ret=wasm.add_synthetic_filter(ptr,ptr0,len0);return ret>>>0;}exports.add_synthetic_filter=add_synthetic_filter;function enter_playground_mode(ptr){const ret=wasm.enter_playground_mode(ptr);return ret>>>0;}exports.enter_playground_mode=enter_playground_mode;function filters(ptr){let deferred1_0;let deferred1_1;try{const ret=wasm.filters(ptr);deferred1_0=ret[0];deferred1_1=ret[1];return getStringFromWasm0(ret[0],ret[1]);}finally{wasm.__wbindgen_free(deferred1_0,deferred1_1,1);}}exports.filters=filters;function init_pagefind(metadata_bytes){const ptr0=passArray8ToWasm0(metadata_bytes,wasm.__wbindgen_malloc);const len0=WASM_VECTOR_LEN;const ret=wasm.init_pagefind(ptr0,len0);return ret>>>0;}exports.init_pagefind=init_pagefind;function load_filter_chunk(ptr,chunk_bytes){const ptr0=passArray8ToWasm0(chunk_bytes,wasm.__wbindgen_malloc);const len0=WASM_VECTOR_LEN;const ret=wasm.load_filter_chunk(ptr,ptr0,len0);return ret>>>0;}exports.load_filter_chunk=load_filter_chunk;function load_index_chunk(ptr,chunk_bytes){const ptr0=passArray8ToWasm0(chunk_bytes,wasm.__wbindgen_malloc);const len0=WASM_VECTOR_LEN;const ret=wasm.load_index_chunk(ptr,ptr0,len0);return ret>>>0;}exports.load_index_chunk=load_index_chunk;function request_all_filter_indexes(ptr){let deferred1_0;let deferred1_1;try{const ret=wasm.request_all_filter_indexes(ptr);deferred1_0=ret[0];deferred1_1=ret[1];return getStringFromWasm0(ret[0],ret[1]);}finally{wasm.__wbindgen_free(deferred1_0,deferred1_1,1);}}exports.request_all_filter_indexes=request_all_filter_indexes;function request_filter_indexes(ptr,filters){let deferred2_0;let deferred2_1;try{const ptr0=passStringToWasm0(filters,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len0=WASM_VECTOR_LEN;const ret=wasm.request_filter_indexes(ptr,ptr0,len0);deferred2_0=ret[0];deferred2_1=ret[1];return getStringFromWasm0(ret[0],ret[1]);}finally{wasm.__wbindgen_free(deferred2_0,deferred2_1,1);}}exports.request_filter_indexes=request_filter_indexes;function request_indexes(ptr,query){let deferred2_0;let deferred2_1;try{const ptr0=passStringToWasm0(query,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len0=WASM_VECTOR_LEN;const ret=wasm.request_indexes(ptr,ptr0,len0);deferred2_0=ret[0];deferred2_1=ret[1];return getStringFromWasm0(ret[0],ret[1]);}finally{wasm.__wbindgen_free(deferred2_0,deferred2_1,1);}}exports.request_indexes=request_indexes;function search(ptr,query,original_query,filter,sort,exact,exact_diacritics){let deferred5_0;let deferred5_1;try{const ptr0=passStringToWasm0(query,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len0=WASM_VECTOR_LEN;const ptr1=passStringToWasm0(original_query,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len1=WASM_VECTOR_LEN;const ptr2=passStringToWasm0(filter,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len2=WASM_VECTOR_LEN;const ptr3=passStringToWasm0(sort,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len3=WASM_VECTOR_LEN;const ret=wasm.search(ptr,ptr0,len0,ptr1,len1,ptr2,len2,ptr3,len3,exact,exact_diacritics);deferred5_0=ret[0];deferred5_1=ret[1];return getStringFromWasm0(ret[0],ret[1]);}finally{wasm.__wbindgen_free(deferred5_0,deferred5_1,1);}}exports.search=search;function set_ranking_weights(ptr,weights){const ptr0=passStringToWasm0(weights,wasm.__wbindgen_malloc,wasm.__wbindgen_realloc);const len0=WASM_VECTOR_LEN;const ret=wasm.set_ranking_weights(ptr,ptr0,len0);return ret>>>0;}exports.set_ranking_weights=set_ranking_weights;function __wbg_get_imports(){const import0={__proto__:null,__wbindgen_init_externref_table:function(){const table=wasm.__wbindgen_externrefs;const offset=table.grow(4);table.set(0,undefined);table.set(offset+0,undefined);table.set(offset+1,null);table.set(offset+2,tru
|
|||
|
|
${e?.toString()}`);throw new Error("Failed to load Pagefind metadata");}}findIndex(language){if(this.languages){let index=this.languages[language];if(index)return index;index=this.languages[language.split("-")[0]];if(index)return index;let topLang=Object.values(this.languages).sort((a,b)=>b.page_count-a.page_count);if(topLang[0])return topLang[0];}throw new Error("Pagefind Error: No language indexes found.");}async loadMeta(index){try{let compressed_resp=await this.throttledFetch(`${this.basePath}pagefind.${index}.pf_meta`);let compressed_meta=await compressed_resp.arrayBuffer();this.searchMeta=this.decompress(new Uint8Array(compressed_meta),"Pagefind metadata");}catch(e){console.error(`Failed to load the meta index:
|
||
|
|
${e?.toString()}`);}}async loadWasm(language){try{const wasm_url=`${this.basePath}wasm.${language}.pagefind`;let compressed_resp=await this.throttledFetch(wasm_url);let compressed_wasm=await compressed_resp.arrayBuffer();const final_wasm=this.decompress(new Uint8Array(compressed_wasm),"Pagefind WebAssembly");if(!final_wasm){throw new Error("No WASM after decompression");}this.wasm=await this.backend({module_or_path:final_wasm});}catch(e){console.error(`Failed to load the Pagefind WASM:
|
||
|
|
${e?.toString()}`);throw new Error(`Failed to load the Pagefind WASM:
|
||
|
|
${e?.toString()}`);}}async _loadGenericChunk(url,method){try{let compressed_resp=await this.throttledFetch(url);let compressed_chunk=await compressed_resp.arrayBuffer();let chunk=this.decompress(new Uint8Array(compressed_chunk),url);let ptr=await this.getPtr();this.raw_ptr=this.backend[method](ptr,chunk);}catch(e){console.error(`Failed to load the index chunk ${url}:
|
||
|
|
${e?.toString()}`);}}async loadChunk(hash){if(!this.loaded_chunks[hash]){const url=`${this.basePath}index/${hash}.pf_index`;this.loaded_chunks[hash]=this._loadGenericChunk(url,"load_index_chunk");}return await this.loaded_chunks[hash];}async loadFilterChunk(hash){if(!this.loaded_filters[hash]){const url=`${this.basePath}filter/${hash}.pf_filter`;this.loaded_filters[hash]=this._loadGenericChunk(url,"load_filter_chunk");}return await this.loaded_filters[hash];}async _loadFragment(hash){let compressed_resp=await this.throttledFetch(`${this.basePath}fragment/${hash}.pf_fragment`);let compressed_fragment=await compressed_resp.arrayBuffer();let fragment=this.decompress(new Uint8Array(compressed_fragment),`Fragment ${hash}`);return JSON.parse(new TextDecoder().decode(fragment));}async loadFragment(hash,weighted_locations=[],search_term){if(!this.loaded_fragments[hash]){this.loaded_fragments[hash]=this._loadFragment(hash);}let fragment=await this.loaded_fragments[hash];fragment.weighted_locations=weighted_locations;fragment.locations=weighted_locations.map((l)=>l.location);if(!fragment.raw_content){fragment.raw_content=fragment.content.replace(/</g,"<").replace(/>/g,">");fragment.content=fragment.content.replace(/\u200B/g,"");}if(!fragment.raw_url){fragment.raw_url=fragment.url;}fragment.url=this.processedUrl(fragment.raw_url,search_term);const excerpt_start=calculate_excerpt_region(weighted_locations,this.excerptLength);const excerpts=build_excerpt(fragment.raw_content,excerpt_start,this.excerptLength,fragment.locations);fragment.excerpt=excerpts.excerpt;fragment.plain_excerpt=excerpts.plain_excerpt;fragment.sub_results=calculate_sub_results(fragment,this.excerptLength);return fragment;}fullUrl(raw){if(/^(https?:)?\/\//.test(raw)){return raw;}return`${this.baseUrl}/${raw}`.replace(/\/+/g,"/").replace(/^(https?:\/)/,"$1/");}processedUrl(url,search_term){const normalized=this.fullUrl(url);if(this.highlightParam===null){return normalized;}let individual_terms=search_term.split(/\s+/);try{let processed=new URL(normalized);for(const term of individual_terms){processed.searchParams.append(this.highlightParam,term);}return processed.toString();}catch(e){try{let processed=new URL(`https://example.com${normalized}`);for(const term of individual_terms){processed.searchParams.append(this.highlightParam,term);}return processed.toString().replace(/^https:\/\/example\.com/,"");}catch(e2){return normalized;}}}async getPtr(){while(this.raw_ptr===null){if(this.initError){throw this.initError;}await asyncSleep(50);}if(!this.raw_ptr){console.error("Pagefind: WASM Error (No pointer)");throw new Error("Pagefind: WASM Error (No pointer)");}return this.raw_ptr;}stringifyFilters(obj={}){return JSON.stringify(obj);}stringifySorts(obj={}){let sorts=Object.entries(obj);for(let[sort,direction]of sorts){if(sorts.length>1){console.warn(`Pagefind was provided multiple sort options in this search, but can only operate on one. Using the ${sort} sort.`);}if(direction!=="asc"&&direction!=="desc"){console.warn(`Pagefind was provided a sort with unknown direction ${direction}. Supported: [asc, desc]`);}return`${sort}:${direction}`;}return``;}async filters(){let ptr=await this.getPtr();let filters=this.backend.request_all_filter_indexes(ptr);let filter_array=JSON.parse(filters);if(Array.isArray(filter_array)){let filter_chunks=filter_array.filter((v)=>v).map((chunk)=>this.loadFilterChunk(chunk));await Promise.all([...filter_chunks]);}ptr=await this.getPtr();let results=this.backend.filters(ptr);return JSON.parse(results);}async preload(term,options={}){await this.search(term,{...options,preload:true});}async search(term,options={}){options={verbose:false,filters:{},sort:{},...options};const log=(str)=>{if(options.verbose)console.log(str);};log(`Starting search on ${this.basePath}`);let start=Date.now();let ptr=await this.getPtr();let filter_only=term===null;term=term??"";let exact_search=/^\s*".+"\s*$/.test(term);if(exact_search){log(`Running an exact search`);}let trueLanguage=null;try{trueLanguage=Intl.getCanonicalLocales(this.loadedLanguage)[0];}catch(err
|