« get me outta code hell

upd8, search: pass urls & thumbs utils into search spec process - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-13 09:02:11 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:52 -0300
commit5038549631b0413552fe6589e7c77f66e53e7bcd (patch)
treec0a5d8cece358918e12be5c0a504f409ccf0b45f /src
parentb6d09fa09a731f21cb35861715cf076f49460e28 (diff)
upd8, search: pass urls & thumbs utils into search spec process
Diffstat (limited to 'src')
-rw-r--r--src/search.js12
-rwxr-xr-xsrc/upd8.js2
-rw-r--r--src/util/search-spec.js31
3 files changed, 42 insertions, 3 deletions
diff --git a/src/search.js b/src/search.js
index 5524344f..33d5d838 100644
--- a/src/search.js
+++ b/src/search.js
@@ -8,6 +8,8 @@ import FlexSearch from 'flexsearch';
 import {logError, logInfo, logWarn} from '#cli';
 import {makeSearchIndex, populateSearchIndex, searchSpec} from '#search-spec';
 import {stitchArrays} from '#sugar';
+import {checkIfImagePathHasCachedThumbnails, getThumbnailEqualOrSmaller}
+  from '#thumbs';
 
 async function exportIndexToJSON(index) {
   const results = {};
@@ -20,6 +22,8 @@ async function exportIndexToJSON(index) {
 }
 
 export async function writeSearchData({
+  thumbsCache,
+  urls,
   wikiCachePath,
   wikiData,
 }) {
@@ -47,7 +51,13 @@ export async function writeSearchData({
     index: indexes,
     descriptor: descriptors,
   }).forEach(({index, descriptor}) =>
-      populateSearchIndex(index, descriptor, {wikiData}));
+      populateSearchIndex(index, descriptor, {
+        checkIfImagePathHasCachedThumbnails,
+        getThumbnailEqualOrSmaller,
+        thumbsCache,
+        urls,
+        wikiData,
+      }));
 
   const jsonIndexes =
     await Promise.all(indexes.map(exportIndexToJSON));
diff --git a/src/upd8.js b/src/upd8.js
index 9ea78020..de4b51e8 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -1492,6 +1492,8 @@ async function main() {
     });
 
     await writeSearchData({
+      thumbsCache,
+      urls,
       wikiCachePath,
       wikiData,
     });
diff --git a/src/util/search-spec.js b/src/util/search-spec.js
index 08717d92..92ed4dec 100644
--- a/src/util/search-spec.js
+++ b/src/util/search-spec.js
@@ -222,7 +222,34 @@ export function makeSearchIndex(descriptor, {FlexSearch}) {
   });
 }
 
-export function populateSearchIndex(index, descriptor, {wikiData}) {
+// 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,
+}) {
+  const bound = {
+    urls,
+  };
+
+  bound.checkIfImagePathHasCachedThumbnails =
+    (imagePath) =>
+      checkIfImagePathHasCachedThumbnails(imagePath, thumbsCache);
+
+  bound.getThumbnailEqualOrSmaller =
+    (preferred, imagePath) =>
+      getThumbnailEqualOrSmaller(preferred, imagePath, thumbsCache);
+
+  return bound;
+}
+
+export function populateSearchIndex(index, descriptor, opts) {
+  const {wikiData} = opts;
+  const bound = bindSearchUtilities(opts);
+
   const collection = descriptor.query(wikiData);
 
   for (const thing of collection) {
@@ -230,7 +257,7 @@ export function populateSearchIndex(index, descriptor, {wikiData}) {
 
     let processed;
     try {
-      processed = descriptor.process(thing);
+      processed = descriptor.process(thing, bound);
     } catch (caughtError) {
       throw new Error(
         `Failed to process searchable thing ${reference}`,