@extravirgin/payload-plugin-meilisearch 0.0.2 → 0.0.4

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,395 +1,197 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1
+ import { MeiliSearch } from 'meilisearch';
2
+ import { compile } from 'html-to-text';
3
+ import { convertLexicalToHTML, consolidateHTMLConverters } from '@payloadcms/richtext-lexical';
4
+ const getIndexName = (config, collectionSlug)=>`${config?.indexPrefix ?? ''}${collectionSlug}`;
5
+ export const indexCollection = (collection, config)=>{
6
+ if (!collection.hooks) {
7
+ collection.hooks = {};
47
8
  }
48
- };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
- if (ar || !(i in from)) {
52
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
- ar[i] = from[i];
54
- }
9
+ if (!collection.hooks.afterChange) {
10
+ collection.hooks.afterChange = [];
11
+ }
12
+ collection.hooks.afterChange.push(handleAfterChange(config));
13
+ if (!collection.hooks.afterDelete) {
14
+ collection.hooks.afterDelete = [];
55
15
  }
56
- return to.concat(ar || Array.prototype.slice.call(from));
16
+ collection.hooks.afterDelete.push(afterDeleteHook(config));
57
17
  };
58
- Object.defineProperty(exports, "__esModule", { value: true });
59
- exports.indexCollections = void 0;
60
- var meilisearch_1 = require("meilisearch");
61
- var html_to_text_1 = require("html-to-text");
62
- var richtext_lexical_1 = require("@payloadcms/richtext-lexical");
63
- var getIndexName = function (config, collectionSlug) { var _a; return "".concat((_a = config === null || config === void 0 ? void 0 : config.indexPrefix) !== null && _a !== void 0 ? _a : '').concat(collectionSlug); };
64
- function handleAfterChange(_a) {
65
- var _b, _c, _d, _e, _f, _g;
66
- var doc = _a.doc, previousDoc = _a.previousDoc, req = _a.req, collection = _a.collection, config = _a.config;
67
- return __awaiter(this, void 0, void 0, function () {
68
- var baseDoc, collectionConfig, payload, client, index, documents, _h, locales, defaultLocale, fallback, _i, locales_1, locale, localizedDoc, _j, documentId, shouldIndex, transformedDoc, _k, documentId, shouldIndex, transformedDoc, _l, error_1;
69
- return __generator(this, function (_m) {
70
- switch (_m.label) {
71
- case 0:
72
- _m.trys.push([0, 19, , 20]);
73
- baseDoc = doc;
74
- collectionConfig = config.collections && (config === null || config === void 0 ? void 0 : config.collections.find(function (c) { return c.slug === collection.slug; }));
75
- payload = req.payload;
76
- client = getMeilisearchClient(config);
77
- return [4 /*yield*/, upsertIndex(client, getIndexName(config, collection.slug))];
78
- case 1:
79
- _m.sent();
80
- index = client.index(getIndexName(config, collection.slug));
81
- documents = [];
82
- if (!(((_b = payload.config) === null || _b === void 0 ? void 0 : _b.localization) && ((_c = payload.config.localization.locales) === null || _c === void 0 ? void 0 : _c.length) > 0)) return [3 /*break*/, 13];
83
- _h = payload.config.localization, locales = _h.locales, defaultLocale = _h.defaultLocale, fallback = _h.fallback;
84
- _i = 0, locales_1 = locales;
85
- _m.label = 2;
86
- case 2:
87
- if (!(_i < locales_1.length)) return [3 /*break*/, 10];
88
- locale = locales_1[_i];
89
- if (!(req.locale === locale.code)) return [3 /*break*/, 3];
90
- _j = doc;
91
- return [3 /*break*/, 5];
92
- case 3: return [4 /*yield*/, req.payload.findByID({
18
+ const handleAfterChange = (config)=>{
19
+ return async function({ doc, previousDoc, req, collection }) {
20
+ try {
21
+ let baseDoc = doc;
22
+ const collectionConfig = config.collections && config?.collections.find((c)=>c.slug === collection.slug);
23
+ const payload = req.payload;
24
+ const client = getMeilisearchClient(config);
25
+ await upsertIndex(client, getIndexName(config, collection.slug));
26
+ const index = client.index(getIndexName(config, collection.slug));
27
+ const documents = [];
28
+ if (payload.config?.localization && payload.config.localization.locales?.length > 0) {
29
+ const { locales, defaultLocale, fallback } = payload.config.localization;
30
+ for (const locale of locales){
31
+ const localizedDoc = await req.payload.findByID({
93
32
  collection: collection.slug,
94
33
  id: doc.id,
95
- locale: locale,
96
- fallbackLocale: locale.fallbackLocale || defaultLocale,
97
- })];
98
- case 4:
99
- _j = _m.sent();
100
- _m.label = 5;
101
- case 5:
102
- localizedDoc = _j;
103
- documentId = doc.id + "_".concat(locale.code);
104
- shouldIndex = (_e = (_d = collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.shouldIndex) === null || _d === void 0 ? void 0 : _d.call(collectionConfig, localizedDoc, previousDoc, locale.code)) !== null && _e !== void 0 ? _e : 'index';
105
- if (!(shouldIndex === 'index')) return [3 /*break*/, 7];
106
- _k = [{}];
107
- return [4 /*yield*/, transformDocumentForMeilisearch(localizedDoc, collection.fields, req)];
108
- case 6:
109
- transformedDoc = __assign.apply(void 0, [__assign.apply(void 0, _k.concat([_m.sent()])), { id: documentId, documentId: doc.id, locale: locale.code }]);
110
- documents.push(transformedDoc);
111
- return [3 /*break*/, 9];
112
- case 7:
113
- if (!(shouldIndex === 'delete')) return [3 /*break*/, 9];
114
- return [4 /*yield*/, index.deleteDocument(documentId)];
115
- case 8:
116
- _m.sent();
117
- _m.label = 9;
118
- case 9:
119
- _i++;
120
- return [3 /*break*/, 2];
121
- case 10:
122
- if (!(documents.length > 0)) return [3 /*break*/, 12];
123
- return [4 /*yield*/, index.addDocuments(documents)];
124
- case 11:
125
- _m.sent();
126
- _m.label = 12;
127
- case 12: return [3 /*break*/, 18];
128
- case 13:
129
- documentId = doc.id;
130
- shouldIndex = (_g = (_f = collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.shouldIndex) === null || _f === void 0 ? void 0 : _f.call(collectionConfig, doc, previousDoc)) !== null && _g !== void 0 ? _g : 'index';
131
- if (!(shouldIndex === 'index')) return [3 /*break*/, 16];
132
- _l = [{}];
133
- return [4 /*yield*/, transformDocumentForMeilisearch(doc, collection.fields, req)];
134
- case 14:
135
- transformedDoc = __assign.apply(void 0, [__assign.apply(void 0, _l.concat([_m.sent()])), { id: documentId, documentId: doc.id }]);
136
- return [4 /*yield*/, index.addDocuments([transformedDoc])];
137
- case 15:
138
- _m.sent();
139
- return [3 /*break*/, 18];
140
- case 16:
141
- if (!(shouldIndex === 'delete')) return [3 /*break*/, 18];
142
- return [4 /*yield*/, index.deleteDocument(documentId)];
143
- case 17:
144
- _m.sent();
145
- _m.label = 18;
146
- case 18: return [3 /*break*/, 20];
147
- case 19:
148
- error_1 = _m.sent();
149
- console.error('Meilisearch indexing error:', error_1);
150
- return [3 /*break*/, 20];
151
- case 20: return [2 /*return*/];
34
+ locale: locale.code,
35
+ draft: false,
36
+ overrideAccess: true,
37
+ fallbackLocale: locale.fallbackLocale || defaultLocale
38
+ });
39
+ const documentId = doc.id + `_${locale.code}`;
40
+ if (!localizedDoc) {
41
+ await index.deleteDocument(documentId);
42
+ continue;
43
+ }
44
+ const shouldIndex = collectionConfig?.shouldIndex?.(localizedDoc, previousDoc, locale.code) ?? 'index';
45
+ if (shouldIndex === 'index') {
46
+ const transformedDoc = {
47
+ ...await transformDocumentForMeilisearch(localizedDoc, collection.fields, req),
48
+ id: documentId,
49
+ documentId: doc.id,
50
+ locale: locale.code
51
+ };
52
+ documents.push(transformedDoc);
53
+ } else if (shouldIndex === 'delete') {
54
+ await index.deleteDocument(documentId);
55
+ }
56
+ }
57
+ if (documents.length > 0) {
58
+ await index.addDocuments(documents);
59
+ }
60
+ } else {
61
+ const documentId = doc.id;
62
+ const shouldIndex = collectionConfig?.shouldIndex?.(doc, previousDoc) ?? 'index';
63
+ if (shouldIndex === 'index') {
64
+ const transformedDoc = {
65
+ ...await transformDocumentForMeilisearch(doc, collection.fields, req),
66
+ id: documentId,
67
+ documentId: doc.id
68
+ };
69
+ await index.addDocuments([
70
+ transformedDoc
71
+ ]);
72
+ } else if (shouldIndex === 'delete') {
73
+ await index.deleteDocument(documentId);
74
+ }
152
75
  }
153
- });
154
- });
155
- }
156
- var indexCollections = function (collections, config) {
157
- return collections.map(function (collection) {
158
- var _a, _b;
159
- var collectionConfig = config.collections && (config === null || config === void 0 ? void 0 : config.collections.find(function (c) { return c.slug === collection.slug; }));
160
- if (!collectionConfig) {
161
- return collection;
76
+ } catch (error) {
77
+ console.error('Meilisearch indexing error:', error);
162
78
  }
163
- return __assign(__assign({}, collection), { hooks: __assign(__assign({}, (collection.hooks || {})), { afterChange: __spreadArray(__spreadArray([], (((_a = collection.hooks) === null || _a === void 0 ? void 0 : _a.afterChange) || []), true), [
164
- function (_a) {
165
- var doc = _a.doc, previousDoc = _a.previousDoc, req = _a.req;
166
- handleAfterChange({ doc: doc, previousDoc: previousDoc, req: req, collection: collection, config: config });
167
- },
168
- ], false), afterDelete: __spreadArray(__spreadArray([], (((_b = collection.hooks) === null || _b === void 0 ? void 0 : _b.afterDelete) || []), true), [
169
- function (_a) {
170
- var doc = _a.doc, req = _a.req;
171
- return __awaiter(void 0, void 0, void 0, function () {
172
- var payload, localization, client, index, locales, _i, locales_2, locale, documentId, error_2;
173
- var _b, _c;
174
- return __generator(this, function (_d) {
175
- switch (_d.label) {
176
- case 0:
177
- _d.trys.push([0, 9, , 10]);
178
- payload = req.payload;
179
- localization = (_b = payload.config) === null || _b === void 0 ? void 0 : _b.localization;
180
- client = getMeilisearchClient(config);
181
- return [4 /*yield*/, upsertIndex(client, getIndexName(config, collection.slug))];
182
- case 1:
183
- _d.sent();
184
- index = client.index(getIndexName(config, collection.slug));
185
- if (!(localization && ((_c = localization.locales) === null || _c === void 0 ? void 0 : _c.length) > 0)) return [3 /*break*/, 6];
186
- locales = localization.locales;
187
- _i = 0, locales_2 = locales;
188
- _d.label = 2;
189
- case 2:
190
- if (!(_i < locales_2.length)) return [3 /*break*/, 5];
191
- locale = locales_2[_i];
192
- documentId = doc.id + "_".concat(locale.code);
193
- return [4 /*yield*/, index.deleteDocument(documentId)];
194
- case 3:
195
- _d.sent();
196
- _d.label = 4;
197
- case 4:
198
- _i++;
199
- return [3 /*break*/, 2];
200
- case 5: return [3 /*break*/, 8];
201
- case 6: return [4 /*yield*/, index.deleteDocument(doc.id)];
202
- case 7:
203
- _d.sent();
204
- _d.label = 8;
205
- case 8: return [3 /*break*/, 10];
206
- case 9:
207
- error_2 = _d.sent();
208
- console.error('Meilisearch indexing error:', error_2);
209
- return [3 /*break*/, 10];
210
- case 10: return [2 /*return*/];
211
- }
212
- });
213
- });
214
- },
215
- ], false) }) });
216
- });
79
+ };
217
80
  };
218
- exports.indexCollections = indexCollections;
219
- var meilisearch;
81
+ const afterDeleteHook = (config)=>{
82
+ return async ({ doc, req, collection })=>{
83
+ try {
84
+ const payload = req.payload;
85
+ const localization = payload.config?.localization;
86
+ const client = getMeilisearchClient(config);
87
+ await upsertIndex(client, getIndexName(config, collection.slug));
88
+ const index = client.index(getIndexName(config, collection.slug));
89
+ if (localization && localization.locales?.length > 0) {
90
+ const { locales } = localization;
91
+ for (const locale of locales){
92
+ const documentId = doc.id + `_${locale.code}`;
93
+ await index.deleteDocument(documentId);
94
+ }
95
+ } else {
96
+ await index.deleteDocument(doc.id);
97
+ }
98
+ } catch (error) {
99
+ console.error('Meilisearch indexing error:', error);
100
+ }
101
+ };
102
+ };
103
+ let meilisearch;
220
104
  function getMeilisearchClient(config) {
221
105
  if (!meilisearch) {
222
- meilisearch = new meilisearch_1.MeiliSearch({
106
+ meilisearch = new MeiliSearch({
223
107
  host: config.host,
224
- apiKey: config.apiKey,
108
+ apiKey: config.apiKey
225
109
  });
226
110
  }
227
111
  return meilisearch;
228
112
  }
229
- function upsertIndex(client, indexUID) {
230
- var _a;
231
- return __awaiter(this, void 0, void 0, function () {
232
- var error_3;
233
- return __generator(this, function (_b) {
234
- switch (_b.label) {
235
- case 0:
236
- _b.trys.push([0, 2, , 6]);
237
- // Attempt to retrieve the index
238
- return [4 /*yield*/, client.getIndex(indexUID)];
239
- case 1:
240
- // Attempt to retrieve the index
241
- _b.sent();
242
- return [3 /*break*/, 6];
243
- case 2:
244
- error_3 = _b.sent();
245
- if (!(((_a = error_3 === null || error_3 === void 0 ? void 0 : error_3.cause) === null || _a === void 0 ? void 0 : _a.code) === 'index_not_found')) return [3 /*break*/, 4];
246
- // Index doesn't exist; create it
247
- return [4 /*yield*/, client.createIndex(indexUID, {
248
- primaryKey: 'id',
249
- })];
250
- case 3:
251
- // Index doesn't exist; create it
252
- _b.sent();
253
- console.log("Index '".concat(indexUID, "' has been created."));
254
- return [3 /*break*/, 5];
255
- case 4:
256
- // Rethrow any other errors
257
- throw error_3;
258
- case 5: return [3 /*break*/, 6];
259
- case 6: return [2 /*return*/];
260
- }
261
- });
262
- });
113
+ async function upsertIndex(client, indexUID) {
114
+ try {
115
+ // Attempt to retrieve the index
116
+ await client.getIndex(indexUID);
117
+ } catch (error) {
118
+ // Check if the error is because the index was not found
119
+ // @ts-ignore
120
+ if (error?.cause?.code === 'index_not_found') {
121
+ // Index doesn't exist; create it
122
+ await client.createIndex(indexUID, {
123
+ primaryKey: 'id'
124
+ });
125
+ console.log(`Index '${indexUID}' has been created.`);
126
+ } else {
127
+ // Rethrow any other errors
128
+ throw error;
129
+ }
130
+ }
263
131
  }
264
- var options = {
265
- wordwrap: 130,
266
- // ...
132
+ const options = {
133
+ wordwrap: 130
267
134
  };
268
- var compiledConvert = (0, html_to_text_1.compile)(options);
135
+ const compiledConvert = compile(options);
269
136
  // Helper function to extract text from HTML in nodejs
270
137
  function extractTextFromHTML(html) {
271
138
  return compiledConvert(html);
272
139
  }
273
140
  // Helper function to transform a document for Meilisearch indexing
274
- function transformDocumentForMeilisearch(doc, fields, req) {
275
- return __awaiter(this, void 0, void 0, function () {
276
- var transformedDoc;
277
- var _this = this;
278
- return __generator(this, function (_a) {
279
- switch (_a.label) {
280
- case 0:
281
- transformedDoc = {};
282
- return [4 /*yield*/, Promise.all(fields.map(function (field) { return __awaiter(_this, void 0, void 0, function () {
283
- var lexicalAdapter, sanitizedServerEditorConfig, html, _a, _b, _c, _d, _e, _f;
284
- var _this = this;
285
- var _g;
286
- return __generator(this, function (_h) {
287
- switch (_h.label) {
288
- case 0:
289
- if (!(field.type === 'richText')) return [3 /*break*/, 2];
290
- lexicalAdapter = field.editor;
291
- sanitizedServerEditorConfig = lexicalAdapter.editorConfig;
292
- return [4 /*yield*/, (0, richtext_lexical_1.convertLexicalToHTML)({
293
- converters: (0, richtext_lexical_1.consolidateHTMLConverters)({ editorConfig: sanitizedServerEditorConfig }),
294
- data: doc[field.name],
295
- })];
296
- case 1:
297
- html = _h.sent();
298
- transformedDoc[field.name] = extractTextFromHTML(html);
299
- return [3 /*break*/, 11];
300
- case 2:
301
- if (!(field.type === 'array' && field.fields)) return [3 /*break*/, 4];
302
- // For array fields, transform each item
303
- _a = transformedDoc;
304
- _b = field.name;
305
- return [4 /*yield*/, Promise.all((doc[field.name] || []).map(function (item) {
306
- return transformDocumentForMeilisearch(item, field.fields, req);
307
- }))];
308
- case 3:
309
- // For array fields, transform each item
310
- _a[_b] = _h.sent();
311
- return [3 /*break*/, 11];
312
- case 4:
313
- if (!(field.type === 'tabs')) return [3 /*break*/, 6];
314
- return [4 /*yield*/, Promise.all(field.tabs.map(function (tab, index) { return __awaiter(_this, void 0, void 0, function () {
315
- return __generator(this, function (_a) {
316
- switch (_a.label) {
317
- case 0: return [4 /*yield*/, transformDocumentForMeilisearch(doc, tab.fields, req)];
318
- case 1: return [2 /*return*/, _a.sent()];
319
- }
320
- });
321
- }); }))];
322
- case 5:
323
- // For tab fields, transform each tab
324
- (_h.sent()).forEach(function (t) { return Object.assign(transformedDoc, t); });
325
- return [3 /*break*/, 11];
326
- case 6:
327
- if (!(field.type === 'blocks')) return [3 /*break*/, 8];
328
- // For block fields, transform each block
329
- _c = transformedDoc;
330
- _d = field.name;
331
- return [4 /*yield*/, Promise.all((doc[field.name] || []).map(function (block) { return __awaiter(_this, void 0, void 0, function () {
332
- var fields;
333
- var _a;
334
- var _b;
335
- return __generator(this, function (_c) {
336
- switch (_c.label) {
337
- case 0:
338
- fields = (_b = field.blocks.find(function (b) { return b.slug === block.blockType; })) === null || _b === void 0 ? void 0 : _b.fields;
339
- if (!fields) {
340
- return [2 /*return*/, {
341
- type: block.blockType,
342
- data: "",
343
- }];
344
- }
345
- _a = {
346
- type: block.blockType
347
- };
348
- return [4 /*yield*/, transformDocumentForMeilisearch(block, fields, req)];
349
- case 1: return [2 /*return*/, (_a.data = _c.sent(),
350
- _a)];
351
- }
352
- });
353
- }); }))];
354
- case 7:
355
- // For block fields, transform each block
356
- _c[_d] = _h.sent();
357
- return [3 /*break*/, 11];
358
- case 8:
359
- if (!(field.type === 'group' && field.fields)) return [3 /*break*/, 10];
360
- // For group fields, transform the nested fields
361
- _e = transformedDoc;
362
- _f = field.name;
363
- return [4 /*yield*/, transformDocumentForMeilisearch(doc[field.name], field.fields, req)];
364
- case 9:
365
- // For group fields, transform the nested fields
366
- _e[_f] = _h.sent();
367
- return [3 /*break*/, 11];
368
- case 10:
369
- if (field.type === 'relationship' || field.type === 'upload') {
370
- // For relationship and upload fields, just include the ID
371
- transformedDoc[field.name] = ((_g = doc[field.name]) === null || _g === void 0 ? void 0 : _g.id) || doc[field.name];
372
- }
373
- else {
374
- // For other field types, include as-is
375
- try {
376
- // @ts-ignore
377
- transformedDoc[field.name] = doc[field.name];
378
- }
379
- catch (err) {
380
- console.log(err, field, doc);
381
- }
382
- }
383
- _h.label = 11;
384
- case 11: return [2 /*return*/];
385
- }
386
- });
387
- }); }))];
388
- case 1:
389
- _a.sent();
390
- return [2 /*return*/, transformedDoc];
141
+ async function transformDocumentForMeilisearch(doc, fields, req) {
142
+ const transformedDoc = {};
143
+ await Promise.all(fields.map(async (field)=>{
144
+ if (field.type === 'richText') {
145
+ // For rich text fields, convert Lexical to HTML, then extract text
146
+ const lexicalAdapter = field.editor;
147
+ const sanitizedServerEditorConfig = lexicalAdapter.editorConfig;
148
+ const html = await convertLexicalToHTML({
149
+ converters: consolidateHTMLConverters({
150
+ editorConfig: sanitizedServerEditorConfig
151
+ }),
152
+ data: doc[field.name]
153
+ });
154
+ transformedDoc[field.name] = extractTextFromHTML(html);
155
+ } else if (field.type === 'array' && field.fields) {
156
+ // For array fields, transform each item
157
+ transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map((item)=>transformDocumentForMeilisearch(item, field.fields, req)));
158
+ } else if (field.type === 'tabs') {
159
+ // For tab fields, transform each tab
160
+ (await Promise.all(field.tabs.map(async (tab)=>{
161
+ return await transformDocumentForMeilisearch(doc, tab.fields, req);
162
+ }))).forEach((t)=>Object.assign(transformedDoc, t));
163
+ } else if (field.type === 'blocks') {
164
+ // For block fields, transform each block
165
+ transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map(async (block)=>{
166
+ const fields = field.blocks.find((b)=>b.slug === block.blockType)?.fields;
167
+ if (!fields) {
168
+ return {
169
+ type: block.blockType,
170
+ data: ""
171
+ };
172
+ }
173
+ return {
174
+ type: block.blockType,
175
+ data: await transformDocumentForMeilisearch(block, fields, req)
176
+ };
177
+ }));
178
+ } else if (field.type === 'group' && field.fields) {
179
+ // For group fields, transform the nested fields
180
+ transformedDoc[field.name] = await transformDocumentForMeilisearch(doc[field.name], field.fields, req);
181
+ } else if (field.type === 'relationship' || field.type === 'upload') {
182
+ // For relationship and upload fields, just include the ID
183
+ transformedDoc[field.name] = doc[field.name]?.id || doc[field.name];
184
+ } else {
185
+ // For other field types, include as-is
186
+ try {
187
+ // @ts-ignore
188
+ transformedDoc[field.name] = doc[field.name];
189
+ } catch (err) {
190
+ console.log(err, field, doc);
391
191
  }
392
- });
393
- });
192
+ }
193
+ }));
194
+ return transformedDoc;
394
195
  }
196
+
395
197
  //# sourceMappingURL=indexCollections.js.map