sagedesk 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -126,17 +126,33 @@ function dotProduct(a, b) {
126
126
  for (let i = 0; i < a.length; i++) dot += a[i] * b[i];
127
127
  return dot;
128
128
  }
129
+ function insertSorted(arr, item, maxLen) {
130
+ arr.push(item);
131
+ let i = arr.length - 1;
132
+ while (i > 0 && arr[i - 1].score < arr[i].score) {
133
+ const tmp = arr[i - 1];
134
+ arr[i - 1] = arr[i];
135
+ arr[i] = tmp;
136
+ i--;
137
+ }
138
+ if (arr.length > maxLen) arr.pop();
139
+ }
129
140
  function search(queryVector, index, topK = 3, minScore = 0.42) {
130
141
  const results = [];
131
142
  for (const chunk of index) {
132
143
  const score = dotProduct(queryVector, chunk.vector384);
133
144
  if (score < minScore) continue;
134
145
  if (results.length < topK) {
135
- results.push({ chunk, score });
136
- results.sort((a, b) => b.score - a.score);
146
+ insertSorted(results, { chunk, score }, topK);
137
147
  } else if (score > results[topK - 1].score) {
138
148
  results[topK - 1] = { chunk, score };
139
- results.sort((a, b) => b.score - a.score);
149
+ let i = topK - 1;
150
+ while (i > 0 && results[i - 1].score < results[i].score) {
151
+ const tmp = results[i - 1];
152
+ results[i - 1] = results[i];
153
+ results[i] = tmp;
154
+ i--;
155
+ }
140
156
  }
141
157
  }
142
158
  return results;
@@ -147,15 +163,23 @@ function keywordSearch(query, index, topK = 3) {
147
163
  const results = [];
148
164
  for (const chunk of index) {
149
165
  const chunkLower = chunk.textLower || chunk.text.toLowerCase();
150
- const matchCount = terms.filter((t) => chunkLower.includes(t)).length;
166
+ let matchCount = 0;
167
+ for (const t of terms) {
168
+ if (chunkLower.includes(t)) matchCount++;
169
+ }
151
170
  const score = matchCount / terms.length;
152
171
  if (score <= 0) continue;
153
172
  if (results.length < topK) {
154
- results.push({ chunk, score });
155
- results.sort((a, b) => b.score - a.score);
173
+ insertSorted(results, { chunk, score }, topK);
156
174
  } else if (score > results[topK - 1].score) {
157
175
  results[topK - 1] = { chunk, score };
158
- results.sort((a, b) => b.score - a.score);
176
+ let i = topK - 1;
177
+ while (i > 0 && results[i - 1].score < results[i].score) {
178
+ const tmp = results[i - 1];
179
+ results[i - 1] = results[i];
180
+ results[i] = tmp;
181
+ i--;
182
+ }
159
183
  }
160
184
  }
161
185
  return results;
@@ -511,13 +535,10 @@ var PURIFY_CONFIG = {
511
535
  ALLOWED_ATTR: ["href", "title", "target", "rel"],
512
536
  ALLOW_DATA_ATTR: false
513
537
  };
514
- var HOOK_ALLOWLIST = ["a"];
515
538
  import_dompurify.default.addHook("afterSanitizeAttributes", (node) => {
516
- if (HOOK_ALLOWLIST.includes(node.tagName.toLowerCase())) {
517
- if (node.tagName.toLowerCase() === "a") {
518
- node.setAttribute("target", "_blank");
519
- node.setAttribute("rel", "noopener noreferrer");
520
- }
539
+ if (node.tagName.toLowerCase() === "a") {
540
+ node.setAttribute("target", "_blank");
541
+ node.setAttribute("rel", "noopener noreferrer");
521
542
  }
522
543
  });
523
544
  function parseMarkdown(markdown) {