From 7fdb6a3a076bd3ffc0b8a89905363ef76341f581 Mon Sep 17 00:00:00 2001 From: Xin Date: Sun, 30 Mar 2025 21:52:41 +0100 Subject: [PATCH] chore: upgrade flexsearch to 0.8 (#652) * chore: upgrade flexsearch to 0.8.x - use cdn to download flexsearch js bundle * chore: remove flexsearch.bundle.min.js --- assets/js/flexsearch.js | 17 ++------ .../lib/flexsearch/flexsearch.bundle.min.js | 39 ------------------- layouts/partials/scripts/search.html | 14 ++++++- 3 files changed, 16 insertions(+), 54 deletions(-) delete mode 100644 assets/lib/flexsearch/flexsearch.bundle.min.js diff --git a/assets/js/flexsearch.js b/assets/js/flexsearch.js index 0cd84e2..a6ba7bf 100644 --- a/assets/js/flexsearch.js +++ b/assets/js/flexsearch.js @@ -196,18 +196,8 @@ document.addEventListener("DOMContentLoaded", function () { async function preloadIndex() { const tokenize = '{{- site.Params.search.flexsearch.tokenize | default "forward" -}}'; - const isCJK = () => { - const lang = document.documentElement.lang || "en"; - return lang.startsWith("zh") || lang.startsWith("ja") || lang.startsWith("ko"); - } - - const encodeCJK = (str) => str.replace(/[\x00-\x7F]/g, "").split(""); - const encodeDefault = (str) => (""+str).toLocaleLowerCase().split(/[\p{Z}\p{S}\p{P}\p{C}]+/u); - const encodeFunction = isCJK() ? encodeCJK : encodeDefault; - window.pageIndex = new FlexSearch.Document({ tokenize, - encode: encodeFunction, cache: 100, document: { id: 'id', @@ -218,13 +208,14 @@ document.addEventListener("DOMContentLoaded", function () { window.sectionIndex = new FlexSearch.Document({ tokenize, - encode: encodeFunction, cache: 100, document: { id: 'id', store: ['title', 'content', 'url', 'display', 'crumb'], index: "content", - tag: 'pageId' + tag: [{ + field: "pageId" + }] } }); @@ -328,7 +319,7 @@ document.addEventListener("DOMContentLoaded", function () { pageTitleMatches[i] = 0; // Show the top 5 results for each page - const sectionResults = window.sectionIndex.search(query, 5, { enrich: true, suggest: true, tag: `page_${result.id}` })[0]?.result || []; + const sectionResults = window.sectionIndex.search(query, 5, { enrich: true, suggest: true, tag: { 'pageId': `page_${result.id}` } })[0]?.result || []; let isFirstItemOfPage = true const occurred = {} diff --git a/assets/lib/flexsearch/flexsearch.bundle.min.js b/assets/lib/flexsearch/flexsearch.bundle.min.js deleted file mode 100644 index 4ebf76e..0000000 --- a/assets/lib/flexsearch/flexsearch.bundle.min.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Skipped minification because the original files appears to be already minified. - * Original file: /npm/flexsearch@0.7.31/dist/flexsearch.bundle.js - * - * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files - */ -/**! - * FlexSearch.js v0.7.31 (Bundle) - * Copyright 2018-2022 Nextapps GmbH - * Author: Thomas Wilkerling - * Licence: Apache-2.0 - * https://github.com/nextapps-de/flexsearch - */ -(function _f(self){'use strict';try{if(module)self=module}catch(e){}self._factory=_f;var t;function u(a){return"undefined"!==typeof a?a:!0}function aa(a){const b=Array(a);for(let c=0;c=this.B&&(w||!n[l])){var f=L(q,d,r),g="";switch(this.G){case "full":if(2f;h--)if(h-f>=this.B){var k=L(q,d,r,e,f);g=l.substring(f,h);M(this,n,g,k,a,c)}break}case "reverse":if(1=this.B&&M(this,n, -g,L(q,d,r,e,h),a,c);g=""}case "forward":if(1=this.B&&M(this,n,g,f,a,c);break}default:if(this.C&&(f=Math.min(f/this.C(b,l,r)|0,q-1)),M(this,n,l,f,a,c),w&&1=this.B&&!e[l]){e[l]=1;const p=this.l&&l>f;M(this,m,p?f:l,L(g+(d/2>g?0:1),d,r,h-1,k-1),a,c,p?l:f)}}}}this.m||(this.register[a]=1)}}return this}; -function L(a,b,c,d,e){return c&&1=this.B&&!c[q])if(this.s||f||this.map[q])k[w++]=q,c[q]=1;else return d;a=k;e=a.length}if(!e)return d;b||(b=100);h=this.depth&&1=d)))break;if(n){if(f)return ta(k,d,0);b[b.length]=k;return}}return!c&&k}function ta(a,b,c){a=1===a.length?a[0]:[].concat.apply([],a);return c||a.length>b?a.slice(c,c+b):a} -function ua(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}t.contain=function(a){return!!this.register[a]};t.update=function(a,b){return this.remove(a).add(a,b)}; -t.remove=function(a,b){const c=this.register[a];if(c){if(this.m)for(let d=0,e;db||c)e=e.slice(c,c+b);d&&(e=za.call(this,e));return{tag:a,result:e}}}function za(a){const b=Array(a.length);for(let c=0,d;c + {{- $flexSearchVersion := site.Params.search.flexsearch.version | default "0.8.143" -}} + {{- $flexSearchJsUrl := printf "https://cdn.jsdelivr.net/npm/flexsearch@%s/dist/flexsearch.bundle%s.js" $flexSearchVersion (cond hugo.IsProduction ".min" ".debug") -}} + {{ with try (resources.GetRemote $flexSearchJsUrl) -}} + {{ with .Err -}} + {{ errorf "Could not retrieve FlexSearch js file from %s. Reason: %s." $flexSearchJsUrl . -}} + {{ else with.Value -}} + {{ with resources.Copy (printf "js/flexsearch.js") . -}} + {{ $flexSearchJs := . | fingerprint -}} + + {{ end -}} + {{ end -}} + {{ end -}} {{- else -}} {{- warnf `search type "%s" is not supported` $searchType -}}