« get me outta code hell

search: just move populateSearchIndex into backend code - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/search.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-10-08 20:54:04 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-10-08 20:54:04 -0300
commite2576723ec0ad59b6f8fe18cb4f1616293c213c9 (patch)
treed32705a29c35748cbf606353d04f99d8b5d0e5a5 /src/search.js
parent9be32e448e65efeef59fa1ed6c2f4190c86d83d4 (diff)
search: just move populateSearchIndex into backend code
Diffstat (limited to 'src/search.js')
-rw-r--r--src/search.js48
1 files changed, 47 insertions, 1 deletions
diff --git a/src/search.js b/src/search.js
index 5d4c7331..e190d63b 100644
--- a/src/search.js
+++ b/src/search.js
@@ -9,12 +9,58 @@ import FlexSearch from 'flexsearch';
 import {pack} from 'msgpackr';
 
 import {logWarn} from '#cli';
-import {makeSearchIndex, populateSearchIndex} from '#search-shape';
+import {makeSearchIndex} from '#search-shape';
 import searchSpec from '#search-select';
 import {stitchArrays} from '#sugar';
 import {checkIfImagePathHasCachedThumbnails, getThumbnailEqualOrSmaller}
   from '#thumbs';
 
+// TODO: This function basically mirrors bind-utilities.js, which isn't
+// exactly robust, but... binding might need some more thought across the
+// codebase in *general.*
+function bindSearchUtilities({
+  checkIfImagePathHasCachedThumbnails,
+  getThumbnailEqualOrSmaller,
+  thumbsCache,
+  urls,
+}) {
+  // TODO: :boom:
+
+  const bound = {
+    urls,
+  };
+
+  bound.checkIfImagePathHasCachedThumbnails =
+    (imagePath) =>
+      checkIfImagePathHasCachedThumbnails(imagePath, thumbsCache);
+
+  bound.getThumbnailEqualOrSmaller =
+    (preferred, imagePath) =>
+      getThumbnailEqualOrSmaller(preferred, imagePath, thumbsCache);
+
+  return bound;
+}
+
+function populateSearchIndex(index, descriptor, opts) {
+  const {wikiData} = opts;
+  const bound = bindSearchUtilities(opts);
+
+  for (const thing of descriptor.select(wikiData)) {
+    const reference = thing.constructor.getReference(thing);
+
+    let processed;
+    try {
+      processed = descriptor.process(thing, bound);
+    } catch (caughtError) {
+      throw new Error(
+        `Failed to process searchable thing ${reference}`,
+        {cause: caughtError});
+    }
+
+    index.add({reference, ...processed});
+  }
+}
+
 async function serializeIndex(index) {
   const results = {};