Better searching
All checks were successful
Publish Library / Build NPM Project (push) Successful in 10s
Publish Library / Tag Version (push) Successful in 8s

This commit is contained in:
2026-08-22 12:27:54 -04:00
parent c5207261fa
commit 86730f1de8
4 changed files with 46 additions and 9 deletions

View File

@@ -3,7 +3,7 @@
import fs from 'node:fs';
import {decompress as lzmaDecompress} from 'lzma1';
import {ZstdCodec} from 'zstd-codec';
import {fuzzyMatch} from './utils.js';
import {fuzzyMatch, titleFromUrl} from './utils.js';
const HEADER_SIZE = 80;
const NS_CONTENT = 'C';
@@ -219,7 +219,7 @@ export class ZimReader {
const termList = String(terms).split(',').map(t => t.trim()).filter(Boolean);
if (!termList.length) return [];
const candidates = this.#hasTitleListing
const candidates = (this.#hasTitleListing && this.#header.articleCount > 5000)
? await this.#titleIndexCandidates(termList[0])
: await this.#allDirents();
@@ -227,9 +227,16 @@ export class ZimReader {
for (const dirent of candidates) {
if (dirent.namespace !== NS_CONTENT) continue;
if (htmlOnly && !(this.#mimeTypes[dirent.mimetype] || '').startsWith('text/html')) continue;
const {max} = fuzzyMatch(dirent.title, ...termList);
scored.push({url: dirent.url, title: dirent.title, namespace: dirent.namespace, score: max});
const urlTitle = titleFromUrl(dirent.url);
const titleScore = fuzzyMatch(dirent.title, ...termList).max;
const urlScore = fuzzyMatch(urlTitle, ...termList).max;
scored.push({
url: dirent.url,
title: dirent.title.length > urlTitle.length ? dirent.title : urlTitle,
namespace: dirent.namespace,
score: Math.max(titleScore, urlScore)
});
}
return scored.toSorted((a, b) => b.score - a.score).slice(0, limit);
return scored.filter(a => a.score > 0).toSorted((a, b) => b.score - a.score).slice(0, limit);
}
}