@tinacms/search 0.0.0-ef282d9-20241024212433 → 0.0.0-f1cec43-20251216232909

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.
@@ -1,215 +1,199 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("stopword")) : typeof define === "function" && define.amd ? define(["exports", "stopword"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@tinacms/search"] = {}, global.NOOP));
3
- })(this, function(exports2, sw) {
4
- "use strict";
5
- function _interopNamespaceDefault(e) {
6
- const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
7
- if (e) {
8
- for (const k in e) {
9
- if (k !== "default") {
10
- const d = Object.getOwnPropertyDescriptor(e, k);
11
- Object.defineProperty(n, k, d.get ? d : {
12
- enumerable: true,
13
- get: () => e[k]
14
- });
15
- }
16
- }
17
- }
18
- n.default = e;
19
- return Object.freeze(n);
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ import * as sw from "stopword";
8
+ const INDEXABLE_NODE_TYPES = ["text", "code_block", "html"];
9
+ class StringBuilder {
10
+ constructor(limit) {
11
+ __publicField(this, "buffer", []);
12
+ __publicField(this, "limit");
13
+ __publicField(this, "length", 0);
14
+ this.limit = limit;
20
15
  }
21
- const sw__namespace = /* @__PURE__ */ _interopNamespaceDefault(sw);
22
- class StringBuilder {
23
- constructor(limit) {
24
- this.length = 0;
25
- this.buffer = [];
26
- this.limit = limit;
27
- }
28
- append(str) {
29
- if (this.length + str.length > this.limit) {
30
- return true;
31
- } else {
32
- this.buffer.push(str);
33
- this.length += str.length;
34
- if (this.length > this.limit) {
35
- return true;
36
- }
37
- return false;
38
- }
39
- }
40
- toString() {
41
- return this.buffer.join(" ");
16
+ append(str) {
17
+ if (this.length + str.length > this.limit)
18
+ return true;
19
+ this.buffer.push(str);
20
+ this.length += str.length;
21
+ return this.length > this.limit;
22
+ }
23
+ toString() {
24
+ return this.buffer.join(" ");
25
+ }
26
+ }
27
+ const tokenizeString = (str) => {
28
+ return str.split(/[\s\.,]+/).map((s) => s.toLowerCase()).filter((s) => s);
29
+ };
30
+ const extractText = (data, builder, nodeTypes) => {
31
+ var _a;
32
+ if (!data)
33
+ return;
34
+ if (nodeTypes.includes(data.type ?? "") && (data.text || data.value)) {
35
+ const tokens = tokenizeString(data.text || data.value || "");
36
+ for (const token of tokens) {
37
+ if (builder.append(token))
38
+ return;
42
39
  }
43
40
  }
44
- const extractText = (data, acc, indexableNodeTypes) => {
45
- var _a, _b;
46
- if (data) {
47
- if (indexableNodeTypes.indexOf(data.type) !== -1 && (data.text || data.value)) {
48
- const tokens = tokenizeString(data.text || data.value);
49
- for (const token of tokens) {
50
- if (acc.append(token)) {
51
- return;
52
- }
53
- }
54
- }
55
- (_b = (_a = data.children) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(
56
- _a,
57
- (child) => extractText(child, acc, indexableNodeTypes)
58
- );
41
+ (_a = data.children) == null ? void 0 : _a.forEach((child) => extractText(child, builder, nodeTypes));
42
+ };
43
+ const getRelativePath = (path, collection) => {
44
+ return path.replace(/\\/g, "/").replace(collection.path, "").replace(/^\/|\/$/g, "");
45
+ };
46
+ const processTextField = (value, maxLength) => {
47
+ const tokens = tokenizeString(value);
48
+ const builder = new StringBuilder(maxLength);
49
+ for (const part of tokens) {
50
+ if (builder.append(part))
51
+ break;
52
+ }
53
+ return builder.toString();
54
+ };
55
+ const processRichTextField = (value, maxLength) => {
56
+ const builder = new StringBuilder(maxLength);
57
+ extractText(value, builder, INDEXABLE_NODE_TYPES);
58
+ return builder.toString();
59
+ };
60
+ const processObjectField = (data, path, collection, textIndexLength, field) => {
61
+ if (field.list) {
62
+ return data.map(
63
+ (obj) => processDocumentForIndexing(obj, path, collection, textIndexLength, field)
64
+ );
65
+ }
66
+ return processDocumentForIndexing(
67
+ data,
68
+ path,
69
+ collection,
70
+ textIndexLength,
71
+ field
72
+ );
73
+ };
74
+ const processStringField = (data, maxLength, isList) => {
75
+ if (isList) {
76
+ return data.map(
77
+ (value) => processTextField(value, maxLength)
78
+ );
79
+ }
80
+ return processTextField(data, maxLength);
81
+ };
82
+ const processRichTextFieldData = (data, maxLength, isList) => {
83
+ if (isList) {
84
+ return data.map(
85
+ (value) => processRichTextField(value, maxLength)
86
+ );
87
+ }
88
+ return processRichTextField(data, maxLength);
89
+ };
90
+ const processDocumentForIndexing = (data, path, collection, textIndexLength, field) => {
91
+ if (!field) {
92
+ const relativePath = getRelativePath(path, collection);
93
+ data["_id"] = `${collection.name}:${relativePath}`;
94
+ data["_relativePath"] = relativePath;
95
+ }
96
+ const fields = (field == null ? void 0 : field.fields) || collection.fields || [];
97
+ for (const f of fields) {
98
+ if (!f.searchable) {
99
+ delete data[f.name];
100
+ continue;
59
101
  }
60
- };
61
- const relativePath = (path, collection) => {
62
- return path.replace(/\\/g, "/").replace(collection.path, "").replace(/^\/|\/$/g, "");
63
- };
64
- const tokenizeString = (str) => {
65
- return str.split(/[\s\.,]+/).map((s) => s.toLowerCase()).filter((s) => s);
66
- };
67
- const processTextFieldValue = (value, maxLen) => {
68
- const tokens = tokenizeString(value);
69
- const builder = new StringBuilder(maxLen);
70
- for (const part of tokens) {
71
- if (builder.append(part)) {
102
+ if (!data[f.name])
103
+ continue;
104
+ const fieldMaxLength = f.maxSearchIndexFieldLength || textIndexLength;
105
+ const isList = Boolean(f.list);
106
+ switch (f.type) {
107
+ case "object":
108
+ data[f.name] = processObjectField(
109
+ data[f.name],
110
+ path,
111
+ collection,
112
+ textIndexLength,
113
+ f
114
+ );
115
+ break;
116
+ case "string":
117
+ data[f.name] = processStringField(
118
+ data[f.name],
119
+ fieldMaxLength,
120
+ isList
121
+ );
122
+ break;
123
+ case "rich-text":
124
+ data[f.name] = processRichTextFieldData(
125
+ data[f.name],
126
+ fieldMaxLength,
127
+ isList
128
+ );
72
129
  break;
73
- }
74
- }
75
- return builder.toString();
76
- };
77
- const processDocumentForIndexing = (data, path, collection, textIndexLength, field) => {
78
- if (!field) {
79
- const relPath = relativePath(path, collection);
80
- data["_id"] = `${collection.name}:${relPath}`;
81
- data["_relativePath"] = relPath;
82
- }
83
- for (const f of (field == null ? void 0 : field.fields) || collection.fields || []) {
84
- if (!f.searchable) {
85
- delete data[f.name];
86
- continue;
87
- }
88
- const isList = f.list;
89
- if (data[f.name]) {
90
- if (f.type === "object") {
91
- if (isList) {
92
- data[f.name] = data[f.name].map(
93
- (obj) => processDocumentForIndexing(
94
- obj,
95
- path,
96
- collection,
97
- textIndexLength,
98
- f
99
- )
100
- );
101
- } else {
102
- data[f.name] = processDocumentForIndexing(
103
- data[f.name],
104
- path,
105
- collection,
106
- textIndexLength,
107
- f
108
- );
109
- }
110
- } else if (f.type === "string") {
111
- const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
112
- if (isList) {
113
- data[f.name] = data[f.name].map(
114
- (value) => processTextFieldValue(value, fieldTextIndexLength)
115
- );
116
- } else {
117
- data[f.name] = processTextFieldValue(
118
- data[f.name],
119
- fieldTextIndexLength
120
- );
121
- }
122
- } else if (f.type === "rich-text") {
123
- const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
124
- if (isList) {
125
- data[f.name] = data[f.name].map((value) => {
126
- const acc = new StringBuilder(fieldTextIndexLength);
127
- extractText(value, acc, ["text", "code_block", "html"]);
128
- return acc.toString();
129
- });
130
- } else {
131
- const acc = new StringBuilder(fieldTextIndexLength);
132
- extractText(data[f.name], acc, ["text", "code_block", "html"]);
133
- data[f.name] = acc.toString();
134
- }
135
- }
136
- }
137
- }
138
- return data;
139
- };
140
- const memo = {};
141
- const lookupStopwords = (keys, defaultStopWords = sw__namespace.eng) => {
142
- let stopwords = defaultStopWords;
143
- if (keys) {
144
- if (memo[keys.join(",")]) {
145
- return memo[keys.join(",")];
146
- }
147
- stopwords = [];
148
- for (const key of keys) {
149
- stopwords.push(...sw__namespace[key]);
150
- }
151
- memo[keys.join(",")] = stopwords;
152
130
  }
153
- return stopwords;
154
- };
155
- const queryToSearchIndexQuery = (query, stopwordLanguages) => {
156
- let q;
157
- const parts = query.split(" ");
158
- const stopwords = lookupStopwords(stopwordLanguages);
159
- if (parts.length === 1) {
160
- q = { AND: [parts[0]] };
161
- } else {
162
- q = {
163
- AND: parts.filter(
164
- (part) => part.toLowerCase() !== "and" && stopwords.indexOf(part.toLowerCase()) === -1
165
- )
166
- };
131
+ }
132
+ return data;
133
+ };
134
+ const stopwordCache = {};
135
+ const PRESERVED_WORDS = ["about"];
136
+ const lookupStopwords = (keys, defaultStopWords = sw.eng) => {
137
+ let stopwords = defaultStopWords;
138
+ if (keys) {
139
+ const cacheKey = keys.join(",");
140
+ if (stopwordCache[cacheKey]) {
141
+ return stopwordCache[cacheKey];
167
142
  }
168
- return q;
169
- };
170
- const optionsToSearchIndexOptions = (options) => {
171
- const opt = {};
172
- if (options == null ? void 0 : options.limit) {
173
- opt["PAGE"] = {
174
- SIZE: options.limit,
175
- NUMBER: (options == null ? void 0 : options.cursor) ? parseInt(options.cursor) : 0
176
- };
143
+ stopwords = keys.flatMap((key) => sw[key] || []);
144
+ stopwordCache[cacheKey] = stopwords;
145
+ }
146
+ return stopwords.filter((word) => !PRESERVED_WORDS.includes(word));
147
+ };
148
+ const queryToSearchIndexQuery = (query, stopwordLanguages) => {
149
+ const parts = query.split(" ");
150
+ const stopwords = lookupStopwords(stopwordLanguages);
151
+ if (parts.length === 1) {
152
+ return { AND: [parts[0]] };
153
+ }
154
+ const filteredParts = parts.filter(
155
+ (part) => part.toLowerCase() !== "and" && !stopwords.includes(part.toLowerCase())
156
+ );
157
+ return { AND: filteredParts };
158
+ };
159
+ const optionsToSearchIndexOptions = (options) => {
160
+ if (!(options == null ? void 0 : options.limit))
161
+ return {};
162
+ return {
163
+ PAGE: {
164
+ SIZE: options.limit,
165
+ NUMBER: options.cursor ? parseInt(options.cursor) : 0
177
166
  }
178
- return opt;
179
167
  };
180
- const parseSearchIndexResponse = (data, options) => {
181
- const results = data["RESULT"];
182
- const total = data["RESULT_LENGTH"];
183
- if ((options == null ? void 0 : options.cursor) && (options == null ? void 0 : options.limit)) {
184
- const prevCursor = options.cursor === "0" ? null : (parseInt(options.cursor) - 1).toString();
185
- const nextCursor = total <= (parseInt(options.cursor) + 1) * options.limit ? null : (parseInt(options.cursor) + 1).toString();
186
- return {
187
- results,
188
- total,
189
- prevCursor,
190
- nextCursor
191
- };
192
- } else if (!(options == null ? void 0 : options.cursor) && (options == null ? void 0 : options.limit)) {
193
- const prevCursor = null;
194
- const nextCursor = total <= options.limit ? null : "1";
195
- return {
196
- results,
197
- total,
198
- prevCursor,
199
- nextCursor
200
- };
201
- } else {
202
- return {
203
- results,
204
- total,
205
- prevCursor: null,
206
- nextCursor: null
207
- };
208
- }
168
+ };
169
+ const parseSearchIndexResponse = (data, options) => {
170
+ const results = data.RESULT;
171
+ const total = data.RESULT_LENGTH;
172
+ const fuzzyMatches = data.FUZZY_MATCHES;
173
+ if (data.NEXT_CURSOR !== void 0 || data.PREV_CURSOR !== void 0) {
174
+ return {
175
+ results,
176
+ total,
177
+ prevCursor: data.PREV_CURSOR ?? null,
178
+ nextCursor: data.NEXT_CURSOR ?? null,
179
+ fuzzyMatches
180
+ };
181
+ }
182
+ const currentPage = (options == null ? void 0 : options.cursor) ? parseInt(options.cursor) : 0;
183
+ const pageSize = options == null ? void 0 : options.limit;
184
+ const hasPreviousPage = currentPage > 0;
185
+ const hasNextPage = pageSize ? total > (currentPage + 1) * pageSize : false;
186
+ return {
187
+ results,
188
+ total,
189
+ prevCursor: hasPreviousPage ? (currentPage - 1).toString() : null,
190
+ nextCursor: hasNextPage ? (currentPage + 1).toString() : null,
191
+ fuzzyMatches
209
192
  };
210
- exports2.optionsToSearchIndexOptions = optionsToSearchIndexOptions;
211
- exports2.parseSearchIndexResponse = parseSearchIndexResponse;
212
- exports2.processDocumentForIndexing = processDocumentForIndexing;
213
- exports2.queryToSearchIndexQuery = queryToSearchIndexQuery;
214
- Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
215
- });
193
+ };
194
+ export {
195
+ optionsToSearchIndexOptions,
196
+ parseSearchIndexResponse,
197
+ processDocumentForIndexing,
198
+ queryToSearchIndexQuery
199
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import si from 'search-index';
2
2
  export { SearchIndexer } from './indexer';
3
3
  export { LocalSearchIndexClient, TinaCMSSearchIndexClient } from './client';
4
- export type { SearchClient } from './types';
4
+ export type { SearchClient, SearchOptions, SearchResult, SearchQueryResponse, IndexableDocument, SearchIndexResult, } from './types';
5
+ export type { FuzzySearchOptions, FuzzyMatch } from './fuzzy';
6
+ export { levenshteinDistance, similarityScore, damerauLevenshteinDistance, findSimilarTerms, FuzzyCache, DEFAULT_FUZZY_OPTIONS, } from './fuzzy';
7
+ export { FuzzySearchWrapper } from './fuzzy-search-wrapper';
8
+ export { buildPageOptions, buildPaginationCursors } from './pagination';
9
+ export type { PaginationOptions, PageOptions, PaginationCursors, } from './pagination';
5
10
  export { si };