@myst-theme/search-minisearch 0.0.0

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.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @myst-theme/search-minisearch
2
+
3
+ A minisearch implementation for client-side searching in MyST sites.
@@ -0,0 +1,8 @@
1
+ import { type Options, type SearchResult as MiniSearchResult } from 'minisearch';
2
+ import type { SearchRecord, SearchResult, ISearch } from '@myst-theme/search';
3
+ export type ExtendedOptions = Options & Required<Pick<Options, 'tokenize' | 'processTerm'>>;
4
+ export declare function prepareOptions(options: Options): ExtendedOptions;
5
+ export type RawSearchResult = SearchRecord & MiniSearchResult;
6
+ export declare function combineResults(results: Map<string, Map<string, RawSearchResult>>): SearchResult[];
7
+ export declare function createSearch(documents: SearchRecord[], options: Options): ISearch;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAmB,EAAE,KAAK,OAAO,EAAE,KAAK,YAAY,IAAI,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC7F,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAG9E,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,aAAa,CAAC,CAAC,CAAC;AAE5F,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,eAAe,CAOhE;AAED,MAAM,MAAM,eAAe,GAAG,YAAY,GAAG,gBAAgB,CAAC;AAE9D,wBAAgB,cAAc,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,GAAG,YAAY,EAAE,CAiDjG;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAqBjF"}
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ import MiniSearch from 'minisearch';
2
+ import { extractField } from '@myst-theme/search';
3
+ export function prepareOptions(options) {
4
+ return {
5
+ ...options,
6
+ tokenize: MiniSearch.getDefault('tokenize'),
7
+ processTerm: MiniSearch.getDefault('processTerm'),
8
+ extractField,
9
+ };
10
+ }
11
+ export function combineResults(results) {
12
+ const [firstEntry, ...restEntries] = results.entries();
13
+ if (firstEntry === undefined) {
14
+ return [];
15
+ }
16
+ // Transform from { terms, queryTerms, match} to [ { term, matches} ]
17
+ const firstRawResults = firstEntry[1];
18
+ const initialValue = new Map(Array.from(firstRawResults.entries(), ([id, rawResult]) => {
19
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
20
+ const { id: _, score, terms, queryTerms, match, ...rest } = rawResult;
21
+ return [
22
+ id,
23
+ {
24
+ id,
25
+ queries: [
26
+ {
27
+ term: queryTerms[0],
28
+ matches: match,
29
+ },
30
+ ],
31
+ ...rest,
32
+ },
33
+ ];
34
+ }));
35
+ // Reduce all entries with this transform
36
+ const mergedResults = restEntries.reduce((accumulator, value) => {
37
+ const nextAccumulator = new Map();
38
+ const rawResults = value[1];
39
+ rawResults.forEach((rawResult, docID) => {
40
+ const existing = accumulator.get(docID);
41
+ if (existing == null) {
42
+ return;
43
+ }
44
+ const { queryTerms, match } = rawResult;
45
+ existing.queries.push({
46
+ term: queryTerms[0],
47
+ matches: match,
48
+ });
49
+ nextAccumulator.set(docID, existing);
50
+ });
51
+ return nextAccumulator;
52
+ }, initialValue);
53
+ return Array.from(mergedResults.values());
54
+ }
55
+ export function createSearch(documents, options) {
56
+ const extendedOptions = prepareOptions(options);
57
+ const search = new MiniSearch(extendedOptions);
58
+ search.addAll(documents.map((doc, index) => ({ ...doc, id: index })));
59
+ return async (query) => {
60
+ // Implement executeQuery whilst retaining distinction between terms
61
+ // TODO: should we check for unique terms?
62
+ const terms = extendedOptions.tokenize(query).filter((token) => !!token);
63
+ if (!terms.length) {
64
+ return undefined;
65
+ }
66
+ else {
67
+ const termResults = new Map(terms.map((term) => [
68
+ term,
69
+ new Map(search.search(term).map((doc) => [doc.id, doc])),
70
+ ]));
71
+ return combineResults(termResults);
72
+ }
73
+ };
74
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@myst-theme/search-minisearch",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "exports": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "license": "MIT",
11
+ "sideEffects": false,
12
+ "scripts": {
13
+ "clean": "rimraf dist",
14
+ "lint": "eslint \"src/**/*.ts*\" -c ./.eslintrc.cjs",
15
+ "lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\"",
16
+ "build:esm": "tsc --project ./tsconfig.json --module Node16 --outDir dist --declaration",
17
+ "build": "npm-run-all -l clean -p build:esm"
18
+ },
19
+ "dependencies": {
20
+ "@myst-theme/search": "^0.0.0",
21
+ "minisearch": "^7.1.0"
22
+ }
23
+ }