Эх сурвалжийг харах

feat: improve search for diacritics + Chinese/Korean/Japanese languages

Close #1361
George Cushen 5 жил өмнө
parent
commit
6d0c021011

+ 3 - 3
assets/js/academic-search.js

@@ -14,11 +14,11 @@ let fuseOptions = {
   shouldSort: true,
   includeMatches: true,
   tokenize: true,
-  threshold: 0.0,
+  threshold: search_config.threshold,  // Set to ~0.3 for parsing diacritics and CJK languages.
   location: 0,
   distance: 100,
   maxPatternLength: 32,
-  minMatchCharLength: 2,
+  minMatchCharLength: search_config.minLength,  // Set to 1 for parsing CJK languages.
   keys: [
     {name:'title', weight:0.99}, /* 1.0 doesn't work o_O */
     {name:'summary', weight:0.6},
@@ -158,7 +158,7 @@ function render(template, data) {
 // If Academic's in-built search is enabled and Fuse loaded, then initialize it.
 if (typeof Fuse === 'function') {
 // Wait for Fuse to initialize.
-  $.getJSON(search_index_filename, function (search_index) {
+  $.getJSON(search_config.indexURI, function (search_index) {
     let fuse = new Fuse(search_index, fuseOptions);
 
     // On page load, check for search query in URL.

+ 6 - 6
layouts/partials/site_js.html

@@ -51,13 +51,13 @@
 
     {{ if ne site.Params.search.engine 0 }}
     {{/* Configure search engine. */}}
+    {{ $min_length := site.Params.search.academic.min_length | default 1 }}
+    {{ $threshold := site.Params.search.academic.threshold | default 0.3 }}
+    {{ $search_i18n := dict "placeholder" (i18n "search_placeholder") "results" (i18n "search_results") "no_results" (i18n "search_no_results") }}
+    {{ $search_config := dict "indexURI" ("/index.json" | relLangURL) "threshold" $threshold "minLength" $min_length }}
     <script>
-      const search_index_filename = {{ "/index.json" | relLangURL }};
-      const i18n = {
-        'placeholder': {{ i18n "search_placeholder" }},
-        'results': {{ i18n "search_results" }},
-        'no_results': {{ i18n "search_no_results" }}
-      };
+      const search_config = {{ $search_config | jsonify | safeJS }};
+      const i18n = {{ $search_i18n | jsonify | safeJS }};
       const content_type = {
         'post': {{ i18n "posts" }},
         'project': {{ i18n "projects" }},