@takeshape/util 9.80.3 → 9.81.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.
Files changed (53) hide show
  1. package/dist/arrays.js +0 -10
  2. package/dist/async-noop.js +0 -2
  3. package/dist/billing.js +8 -4
  4. package/dist/browser.js +0 -2
  5. package/dist/clone.js +0 -1
  6. package/dist/common.js +0 -50
  7. package/dist/delay.js +3 -6
  8. package/dist/draftjs-templates.js +1 -11
  9. package/dist/draftjs.js +37 -196
  10. package/dist/encryption.js +2 -11
  11. package/dist/get-image-url.js +2 -3
  12. package/dist/gzip.js +0 -4
  13. package/dist/highlight-code.js +1 -19
  14. package/dist/http.js +0 -6
  15. package/dist/index.js +0 -8
  16. package/dist/map.js +0 -1
  17. package/dist/merge.js +3 -20
  18. package/dist/mime.js +4 -12
  19. package/dist/predicate.js +0 -3
  20. package/dist/search-params.js +0 -6
  21. package/dist/set-in.js +0 -2
  22. package/dist/sets.js +0 -5
  23. package/dist/sleep.js +0 -1
  24. package/dist/sort-object.js +4 -4
  25. package/dist/strings.js +4 -21
  26. package/dist/templates.js +1 -25
  27. package/dist/timezone.js +2 -4
  28. package/dist/types.js +8 -10
  29. package/dist/unix-to-iso.js +0 -2
  30. package/dist/value.js +0 -2
  31. package/dist/visit.js +1 -15
  32. package/es/arrays.js +0 -4
  33. package/es/billing.js +10 -0
  34. package/es/delay.js +3 -4
  35. package/es/draftjs-templates.js +1 -1
  36. package/es/draftjs.js +39 -173
  37. package/es/encryption.js +2 -3
  38. package/es/get-image-url.js +2 -1
  39. package/es/highlight-code.js +0 -1
  40. package/es/http.js +0 -4
  41. package/es/index.js +2 -2
  42. package/es/merge.js +4 -8
  43. package/es/mime.js +4 -5
  44. package/es/search-params.js +0 -2
  45. package/es/set-in.js +1 -1
  46. package/es/sets.js +0 -3
  47. package/es/sort-object.js +4 -2
  48. package/es/strings.js +4 -8
  49. package/es/templates.js +0 -14
  50. package/es/timezone.js +2 -2
  51. package/es/types.js +8 -3
  52. package/es/visit.js +1 -12
  53. package/package.json +3 -3
package/dist/draftjs.js CHANGED
@@ -12,39 +12,25 @@ exports.getImagePathFromUrl = getImagePathFromUrl;
12
12
  exports.insertBreaksAroundBlocks = insertBreaksAroundBlocks;
13
13
  exports.mdToDraftjs = mdToDraftjs;
14
14
  exports.mdxToDraftjs = mdxToDraftjs;
15
-
16
15
  var _markdownDraftJs = require("markdown-draft-js");
17
-
18
16
  var _templates = require("./templates");
19
-
20
17
  var _htmlparser = require("htmlparser2");
21
-
22
18
  var _urlParse = _interopRequireDefault(require("url-parse"));
23
-
24
19
  var _domSerializer = _interopRequireDefault(require("dom-serializer"));
25
-
26
20
  var _shortid = _interopRequireDefault(require("shortid"));
27
-
28
21
  var _draftjsTemplates = require("./draftjs-templates");
29
-
30
22
  var _escape = _interopRequireDefault(require("lodash/escape"));
31
-
32
23
  var _unescape = _interopRequireDefault(require("lodash/unescape"));
33
-
34
24
  var _pickBy = _interopRequireDefault(require("lodash/pickBy"));
35
-
36
25
  var _routing = require("@takeshape/routing");
37
-
38
26
  var _forEach = _interopRequireDefault(require("lodash/forEach"));
39
-
40
27
  var _he = _interopRequireDefault(require("he"));
41
-
42
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
-
44
29
  /*
45
30
  * The intention is to move all this to the client package in the end, but right now it's here
46
31
  * to be shared between the draftjs / mdx implementations, which span the client / server differently.
47
32
  */
33
+
48
34
  // With @types/htmlparser2 it says parseDOM is not exported; excluding types for now
49
35
  // @ts-expect-error Untyped
50
36
  const SUPERSCRIPT_MARKER = 'TEMPORARY_SUPERSCRIPT_MARKER_TLfDNyf7VYKDduyL';
@@ -52,7 +38,6 @@ const SUBSCRIPT_MARKER = 'TEMPORARY_SUBSCRIPT_MARKER_K5VrdPEzyQyy2RcY';
52
38
  const INSERT_MARKER = 'TEMPORARY_INSERT_MARKER_FSYdr8m8CS7YLb8y';
53
39
  const EXTERNAL_LINK_MARKER = 'TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss';
54
40
  const pullquoteStyle = 'font-style: italic; margin: 2em 2.5em;';
55
-
56
41
  const getDraftjsEmpty = (depth = 0) => {
57
42
  return {
58
43
  key: _shortid.default.generate(),
@@ -63,9 +48,7 @@ const getDraftjsEmpty = (depth = 0) => {
63
48
  inlineStyleRanges: []
64
49
  };
65
50
  };
66
-
67
51
  const REMOVAL_MARKER_TYPE = 'removal-marker';
68
-
69
52
  const getDraftjsRemovalMarker = (depth = 0) => {
70
53
  return {
71
54
  key: _shortid.default.generate(),
@@ -76,35 +59,31 @@ const getDraftjsRemovalMarker = (depth = 0) => {
76
59
  inlineStyleRanges: []
77
60
  };
78
61
  };
79
-
80
62
  const mdxShortcodePrefix = tagName => `TS${tagName}`;
81
-
82
63
  const handleMultiword = (markdown, marker, markdownSyntax, markdownRegex) => {
83
64
  const parts = markdown.replace(markdownRegex, `\\${markdownSyntax}`).split(marker);
84
65
  let result = '';
85
66
  let open = false;
86
-
87
67
  for (const [i, part] of parts.entries()) {
88
68
  if (open) {
89
69
  result += part.replace(/\s+/g, match => `${markdownSyntax}${match}${markdownSyntax}`);
90
70
  } else {
91
71
  result += part;
92
72
  }
93
-
94
73
  if (i !== parts.length - 1) {
95
74
  result += markdownSyntax;
96
75
  open = !open;
97
76
  }
98
77
  }
99
-
100
78
  return result;
101
- }; // Workaround inability to reasonably process child content using draftToMarkdown
102
-
79
+ };
103
80
 
81
+ // Workaround inability to reasonably process child content using draftToMarkdown
104
82
  const handleExternalLinks = markdown => {
105
83
  const regex = /(<TSExternalLink text="TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss" href="[^"]+">)([^<]+)<\/TSExternalLink>/g;
106
84
  return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, content));
107
85
  };
86
+
108
87
  /**
109
88
  * Adjust entity ranges to insert `amount` space starting at `after`.
110
89
  * For example, take this visualization of a range with offset 5 and length 5, where
@@ -119,14 +98,14 @@ const handleExternalLinks = markdown => {
119
98
  * If you insert amount -2 after 7 you get this:
120
99
  * -----|-|-----
121
100
  */
122
-
123
-
124
101
  const adjustEntityRanges = (entityRanges, after, amount) => {
125
- return entityRanges.map(entityRange => ({ ...entityRange,
102
+ return entityRanges.map(entityRange => ({
103
+ ...entityRange,
126
104
  offset: entityRange.offset > after ? entityRange.offset + amount : entityRange.offset,
127
105
  length: entityRange.offset <= after && entityRange.offset + entityRange.length > after ? entityRange.length + amount : entityRange.length
128
106
  }));
129
107
  };
108
+
130
109
  /**
131
110
  * Encode HTML entities in this draftjs state.
132
111
  * We adjust entity ranges so that their offsets and lengths are still correct with the
@@ -135,12 +114,10 @@ const adjustEntityRanges = (entityRanges, after, amount) => {
135
114
  * the link entity's offset needs to be increased by 5 when the text is encoded.
136
115
  * This also applies to the entity's length if there are escaped characters inside the entity.
137
116
  */
138
-
139
-
140
117
  exports.adjustEntityRanges = adjustEntityRanges;
141
-
142
118
  const encodeHtmlEntities = draftjs => {
143
- const result = { ...draftjs,
119
+ const result = {
120
+ ...draftjs,
144
121
  blocks: draftjs.blocks.map(block => {
145
122
  let {
146
123
  text
@@ -148,12 +125,9 @@ const encodeHtmlEntities = draftjs => {
148
125
  let {
149
126
  entityRanges
150
127
  } = block;
151
-
152
128
  for (let i = 0; i < text.length; i++) {
153
129
  const char = text.charAt(i);
154
-
155
130
  const encodedChar = _he.default.encode(char);
156
-
157
131
  if (encodedChar !== char) {
158
132
  const offset = encodedChar.length - char.length;
159
133
  entityRanges = adjustEntityRanges(entityRanges, i, offset);
@@ -161,8 +135,8 @@ const encodeHtmlEntities = draftjs => {
161
135
  i += offset;
162
136
  }
163
137
  }
164
-
165
- return { ...block,
138
+ return {
139
+ ...block,
166
140
  entityRanges,
167
141
  text
168
142
  };
@@ -170,9 +144,7 @@ const encodeHtmlEntities = draftjs => {
170
144
  };
171
145
  return result;
172
146
  };
173
-
174
147
  exports.encodeHtmlEntities = encodeHtmlEntities;
175
-
176
148
  function fromDraftjs(draftjs, styleItems, entityItems) {
177
149
  let markdown = (0, _markdownDraftJs.draftToMarkdown)(encodeHtmlEntities(draftjs), {
178
150
  styleItems: {
@@ -180,54 +152,43 @@ function fromDraftjs(draftjs, styleItems, entityItems) {
180
152
  open() {
181
153
  return '***';
182
154
  },
183
-
184
155
  close() {
185
156
  return '';
186
157
  }
187
-
188
158
  },
189
159
  SUP: {
190
160
  open() {
191
161
  return SUPERSCRIPT_MARKER;
192
162
  },
193
-
194
163
  close() {
195
164
  return SUPERSCRIPT_MARKER;
196
165
  }
197
-
198
166
  },
199
167
  SUB: {
200
168
  open() {
201
169
  return SUBSCRIPT_MARKER;
202
170
  },
203
-
204
171
  close() {
205
172
  return SUBSCRIPT_MARKER;
206
173
  }
207
-
208
174
  },
209
175
  UNDERLINE: {
210
176
  open() {
211
177
  return INSERT_MARKER;
212
178
  },
213
-
214
179
  close() {
215
180
  return INSERT_MARKER;
216
181
  }
217
-
218
182
  },
219
183
  'code-block': {
220
184
  open(entity) {
221
185
  var _entity$data;
222
-
223
186
  const lang = (entity === null || entity === void 0 ? void 0 : (_entity$data = entity.data) === null || _entity$data === void 0 ? void 0 : _entity$data.lang) ?? '';
224
187
  return `\`\`\`${lang}\n`;
225
188
  },
226
-
227
189
  close() {
228
190
  return '\n```';
229
191
  }
230
-
231
192
  },
232
193
  ...styleItems
233
194
  },
@@ -235,89 +196,73 @@ function fromDraftjs(draftjs, styleItems, entityItems) {
235
196
  LINK: {
236
197
  open(entity) {
237
198
  var _entity$data2;
238
-
239
199
  if ((entity === null || entity === void 0 ? void 0 : (_entity$data2 = entity.data) === null || _entity$data2 === void 0 ? void 0 : _entity$data2.target) === '_blank') {
240
200
  var _entity$data3, _entity$data4;
241
-
242
201
  const url = (0, _escape.default)((entity === null || entity === void 0 ? void 0 : (_entity$data3 = entity.data) === null || _entity$data3 === void 0 ? void 0 : _entity$data3.url) || (entity === null || entity === void 0 ? void 0 : (_entity$data4 = entity.data) === null || _entity$data4 === void 0 ? void 0 : _entity$data4.href) || '');
243
202
  return `<TSExternalLink text="${EXTERNAL_LINK_MARKER}" href="${url}">`;
244
203
  }
245
-
246
204
  return '[';
247
205
  },
248
-
249
206
  close(entity) {
250
207
  var _entity$data5, _entity$data6, _entity$data7;
251
-
252
208
  if ((entity === null || entity === void 0 ? void 0 : (_entity$data5 = entity.data) === null || _entity$data5 === void 0 ? void 0 : _entity$data5.target) === '_blank') {
253
209
  return '</TSExternalLink>';
254
210
  }
255
-
256
211
  return ']('.concat((entity === null || entity === void 0 ? void 0 : (_entity$data6 = entity.data) === null || _entity$data6 === void 0 ? void 0 : _entity$data6.url) || (entity === null || entity === void 0 ? void 0 : (_entity$data7 = entity.data) === null || _entity$data7 === void 0 ? void 0 : _entity$data7.href) || '', ')');
257
212
  }
258
-
259
213
  },
260
214
  ...entityItems
261
215
  }
262
216
  });
263
217
  markdown = handleExternalLinks(markdown);
264
218
  markdown = handleMultiword(markdown, SUPERSCRIPT_MARKER, '^', /\^/g);
265
- markdown = handleMultiword(markdown, SUBSCRIPT_MARKER, '~', /~/g); // eslint-disable-next-line security-node/non-literal-reg-expr
266
-
219
+ markdown = handleMultiword(markdown, SUBSCRIPT_MARKER, '~', /~/g);
220
+ // eslint-disable-next-line security-node/non-literal-reg-expr
267
221
  markdown = markdown.replace(/\+/g, '\\+').replace(new RegExp(INSERT_MARKER, 'g'), '++');
268
222
  return markdown;
269
223
  }
270
-
271
224
  function draftjsToMd(draftjs, assets) {
272
225
  return fromDraftjs(draftjs, {
273
226
  pullquote: {
274
227
  open() {
275
228
  return `<aside style="${pullquoteStyle}">`;
276
229
  },
277
-
278
230
  close() {
279
231
  return '</aside>';
280
232
  }
281
-
282
233
  }
283
234
  }, {
284
235
  oembed: {
285
236
  open() {
286
237
  return '';
287
238
  },
288
-
289
239
  close(entity) {
290
240
  if (!(entity !== null && entity !== void 0 && entity.data)) {
291
241
  return '';
292
242
  }
293
-
294
243
  const {
295
244
  data
296
245
  } = entity;
297
246
  return (0, _templates.oembedTemplate)(str => str, data);
298
247
  }
299
-
300
248
  },
301
249
  image: {
302
250
  open() {
303
251
  return '';
304
252
  },
305
-
306
253
  close(entity) {
307
254
  if (!(entity !== null && entity !== void 0 && entity.data)) {
308
255
  return '';
309
256
  }
310
-
311
257
  const {
312
258
  data
313
259
  } = entity;
314
260
  const asset = assets[data.id];
315
-
316
261
  if (asset === undefined) {
317
262
  return '';
318
263
  }
319
-
320
- const imageTemplateData = { ...data,
264
+ const imageTemplateData = {
265
+ ...data,
321
266
  alignment: data.alignment,
322
267
  size: data.size,
323
268
  asset,
@@ -327,82 +272,66 @@ function draftjsToMd(draftjs, assets) {
327
272
  };
328
273
  return (0, _templates.imageTemplate)(str => str, imageTemplateData);
329
274
  }
330
-
331
275
  }
332
276
  });
333
277
  }
334
-
335
278
  function getTextFromDraftjs(draftjs) {
336
279
  var _draftjs$blocks, _draftjs$blocks$;
337
-
338
280
  return draftjs === null || draftjs === void 0 ? void 0 : (_draftjs$blocks = draftjs.blocks) === null || _draftjs$blocks === void 0 ? void 0 : (_draftjs$blocks$ = _draftjs$blocks[0]) === null || _draftjs$blocks$ === void 0 ? void 0 : _draftjs$blocks$.text;
339
281
  }
340
-
341
282
  function getAssetField(data, asset, field) {
342
283
  const dataText = getTextFromDraftjs(data[field]);
343
-
344
284
  if (dataText) {
345
285
  return dataText;
346
286
  }
347
-
348
287
  if (asset[field]) {
349
288
  return getTextFromDraftjs(asset[field]);
350
289
  }
351
-
352
290
  return '';
353
291
  }
354
-
355
292
  function draftjsToMdx(draftjs, assets, prefix = mdxShortcodePrefix) {
356
293
  return fromDraftjs(draftjs, {
357
294
  pullquote: {
358
295
  open() {
359
296
  return `<${prefix('Pullquote')}>`;
360
297
  },
361
-
362
298
  close() {
363
299
  return `</${prefix('Pullquote')}>`;
364
300
  }
365
-
366
301
  }
367
302
  }, {
368
303
  oembed: {
369
304
  open() {
370
305
  return '';
371
306
  },
372
-
373
307
  close(entity) {
374
308
  if (!(entity !== null && entity !== void 0 && entity.data)) {
375
309
  return '';
376
310
  }
377
-
378
311
  const {
379
312
  data
380
313
  } = entity;
381
314
  return (0, _templates.oembedTemplateMdx)(prefix, data);
382
315
  }
383
-
384
316
  },
385
317
  image: {
386
318
  open() {
387
319
  return '';
388
320
  },
389
-
390
321
  close(entity) {
391
322
  var _entity$data8, _data$link, _data$link2;
392
-
393
323
  const asset = assets[entity === null || entity === void 0 ? void 0 : (_entity$data8 = entity.data) === null || _entity$data8 === void 0 ? void 0 : _entity$data8.id];
394
-
395
324
  if (!(entity !== null && entity !== void 0 && entity.data) || asset === undefined) {
396
325
  return '';
397
326
  }
398
-
399
327
  const {
400
328
  data
401
329
  } = entity;
402
330
  const imageUrlOptions = process.env.NODE_ENV === 'production' ? {} : {
403
331
  baseUrl: 'https://images.dev.takeshape.io'
404
332
  };
405
- return (0, _templates.imageTemplateMdx)(prefix, { ...data,
333
+ return (0, _templates.imageTemplateMdx)(prefix, {
334
+ ...data,
406
335
  assetId: data.id,
407
336
  assetPath: asset.path,
408
337
  credit: getAssetField(data, asset, 'credit'),
@@ -412,15 +341,12 @@ function draftjsToMdx(draftjs, assets, prefix = mdxShortcodePrefix) {
412
341
  src: (0, _routing.getImageUrl)(asset.path, data.imageParams, imageUrlOptions)
413
342
  });
414
343
  }
415
-
416
344
  }
417
345
  });
418
346
  }
419
-
420
347
  function getAssetIdFromImageSrc(src) {
421
348
  return new _urlParse.default(src).pathname.split('/')[3];
422
349
  }
423
-
424
350
  function parseFigureClass(className) {
425
351
  if (className === undefined) {
426
352
  return {
@@ -428,56 +354,44 @@ function parseFigureClass(className) {
428
354
  size: undefined
429
355
  };
430
356
  }
431
-
432
357
  const parts = className.split(' ');
433
358
  return {
434
359
  alignment: parts[0],
435
360
  size: parts[1]
436
361
  };
437
- } // eslint-disable-next-line complexity
438
-
362
+ }
439
363
 
364
+ // eslint-disable-next-line complexity
440
365
  function htmlDomToDraftjsImage(dom, item, entityKeyGenerator) {
441
366
  for (const figure of dom) {
442
367
  if (figure.type === 'tag' && figure.name === 'figure') {
443
368
  var _figure$children, _figure$children2, _figcaption, _figcaption2, _caption, _credit, _link, _link2;
444
-
445
369
  let link;
446
370
  const figureChildOne = (_figure$children = figure.children) === null || _figure$children === void 0 ? void 0 : _figure$children[0];
447
371
  const figureChildTwo = (_figure$children2 = figure.children) === null || _figure$children2 === void 0 ? void 0 : _figure$children2[1];
448
-
449
372
  if ((figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.type) === 'tag' && (figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.name) === 'a') {
450
373
  link = figureChildOne;
451
374
  }
452
-
453
375
  let figcaption;
454
-
455
376
  if ((figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.type) === 'tag' && (figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.name) === 'figcaption') {
456
377
  figcaption = figureChildOne;
457
378
  } else if ((figureChildTwo === null || figureChildTwo === void 0 ? void 0 : figureChildTwo.type) === 'tag' && (figureChildTwo === null || figureChildTwo === void 0 ? void 0 : figureChildTwo.name) === 'figcaption') {
458
379
  figcaption = figure.children[1];
459
380
  }
460
-
461
381
  const img = link ? link.children[0] : figure.children[0];
462
-
463
382
  if ((img === null || img === void 0 ? void 0 : img.type) !== 'tag' || (img === null || img === void 0 ? void 0 : img.name) !== 'img') {
464
383
  return;
465
384
  }
466
-
467
385
  const figcaptionChild = (_figcaption = figcaption) === null || _figcaption === void 0 ? void 0 : _figcaption.children[0];
468
386
  let caption;
469
-
470
387
  if ((figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.type) === 'tag' && (figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.name) === 'span' && (figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.attribs.class) === 'caption') {
471
388
  caption = figcaptionChild;
472
389
  }
473
-
474
390
  const captionChildThree = (_figcaption2 = figcaption) === null || _figcaption2 === void 0 ? void 0 : _figcaption2.children[2];
475
391
  let credit;
476
-
477
392
  if ((captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.type) === 'tag' && (captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.name) === 'span' && (captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.attribs.class) === 'credit') {
478
393
  credit = captionChildThree;
479
394
  }
480
-
481
395
  const {
482
396
  alignment,
483
397
  size
@@ -497,22 +411,17 @@ function htmlDomToDraftjsImage(dom, item, entityKeyGenerator) {
497
411
  }
498
412
  }
499
413
  }
500
-
501
414
  function htmlDomToOembed(dom, item, entityKeyGenerator) {
502
415
  for (const oembedDiv of dom) {
503
416
  if (oembedDiv.type === 'tag' && oembedDiv.name === 'div' && oembedDiv.attribs.class === 'oembed') {
504
417
  const blockquote = oembedDiv.children[0];
505
-
506
418
  if ((blockquote === null || blockquote === void 0 ? void 0 : blockquote.type) !== 'tag' || (blockquote === null || blockquote === void 0 ? void 0 : blockquote.name) !== 'blockquote') {
507
419
  return;
508
420
  }
509
-
510
421
  const script = oembedDiv.children[2];
511
-
512
422
  if ((script === null || script === void 0 ? void 0 : script.type) !== 'script' || (script === null || script === void 0 ? void 0 : script.name) !== 'script') {
513
423
  return;
514
424
  }
515
-
516
425
  return (0, _draftjsTemplates.getDraftjsOembed)({
517
426
  html: `${(0, _domSerializer.default)(blockquote)}\n${(0, _domSerializer.default)(script)}\n`,
518
427
  key: entityKeyGenerator(),
@@ -521,7 +430,6 @@ function htmlDomToOembed(dom, item, entityKeyGenerator) {
521
430
  }
522
431
  }
523
432
  }
524
-
525
433
  function mdxToDraftjsOembed(item, entityKeyGenerator) {
526
434
  if (item.content.startsWith(`<${mdxShortcodePrefix('Oembed')}`)) {
527
435
  const dom = (0, _htmlparser.parseDOM)(item.content)[0];
@@ -542,16 +450,13 @@ function mdxToDraftjsOembed(item, entityKeyGenerator) {
542
450
  });
543
451
  }
544
452
  }
545
-
546
453
  function htmlDomToPullquote(dom, item) {
547
454
  for (const pullquote of dom) {
548
455
  if (pullquote.type === 'tag' && pullquote.name === 'aside' && pullquote.attribs.style === pullquoteStyle) {
549
456
  const text = pullquote.children[0];
550
-
551
457
  if ((text === null || text === void 0 ? void 0 : text.type) !== 'text') {
552
458
  return;
553
459
  }
554
-
555
460
  return (0, _draftjsTemplates.getDraftjsPullquote)({
556
461
  text: text.data,
557
462
  depth: item.level
@@ -559,7 +464,6 @@ function htmlDomToPullquote(dom, item) {
559
464
  }
560
465
  }
561
466
  }
562
-
563
467
  function fromMd(md, blockEntities, blockTypes) {
564
468
  md = md.replace(/\\\+/g, '+').replace(/\\~/g, '~').replace(/\\\^/g, '^');
565
469
  return (0, _markdownDraftJs.markdownToDraft)(md, {
@@ -578,7 +482,6 @@ function fromMd(md, blockEntities, blockTypes) {
578
482
  if (!item) {
579
483
  return getDraftjsEmpty();
580
484
  }
581
-
582
485
  return {
583
486
  key: _shortid.default.generate(),
584
487
  text: ' ',
@@ -589,12 +492,10 @@ function fromMd(md, blockEntities, blockTypes) {
589
492
  data: {}
590
493
  };
591
494
  },
592
-
593
495
  fence(item) {
594
496
  if (!item) {
595
497
  return getDraftjsEmpty();
596
498
  }
597
-
598
499
  return {
599
500
  type: 'code-block',
600
501
  data: {
@@ -607,63 +508,51 @@ function fromMd(md, blockEntities, blockTypes) {
607
508
  inlineStyleRanges: []
608
509
  };
609
510
  },
610
-
611
511
  ...blockTypes
612
512
  }
613
513
  });
614
514
  }
615
-
616
515
  function mdToDraftjs(mdx) {
617
- const entities = {}; // Start really high to avoid conflicts with keys created by markdown-draft-js
516
+ const entities = {};
618
517
 
518
+ // Start really high to avoid conflicts with keys created by markdown-draft-js
619
519
  let currentEntityKey = 1000000;
620
-
621
520
  function entityKeyGenerator() {
622
521
  return currentEntityKey++;
623
522
  }
624
-
625
523
  const result = fromMd(mdx, {}, {
626
524
  htmlblock(item) {
627
525
  if (item === undefined) {
628
526
  return getDraftjsEmpty();
629
527
  }
630
-
631
528
  const dom = (0, _htmlparser.parseDOM)(item.content);
632
529
  const image = htmlDomToDraftjsImage(dom, item, entityKeyGenerator);
633
-
634
530
  if (image) {
635
531
  Object.assign(entities, image.entities);
636
532
  return image.contentBlock;
637
533
  }
638
-
639
534
  const oembed = htmlDomToOembed(dom, item, entityKeyGenerator);
640
-
641
535
  if (oembed) {
642
536
  Object.assign(entities, oembed.entities);
643
537
  return oembed.contentBlock;
644
538
  }
645
-
646
539
  const pullquote = htmlDomToPullquote(dom, item);
647
-
648
540
  if (pullquote) {
649
541
  Object.assign(entities, pullquote.entities);
650
542
  return pullquote.contentBlock;
651
543
  }
652
-
653
544
  return getDraftjsEmpty(item.level);
654
545
  }
655
-
656
546
  });
657
- result.entityMap = { ...result.entityMap,
547
+ result.entityMap = {
548
+ ...result.entityMap,
658
549
  ...entities
659
550
  };
660
551
  return result;
661
552
  }
662
-
663
553
  function getImagePathFromUrl(url) {
664
554
  return new _urlParse.default(url).pathname.substr(1);
665
555
  }
666
-
667
556
  function mdxToBr(item) {
668
557
  if (item.content.startsWith('<br/>')) {
669
558
  return {
@@ -676,7 +565,6 @@ function mdxToBr(item) {
676
565
  };
677
566
  }
678
567
  }
679
-
680
568
  function mdxToLinkData(item) {
681
569
  if (item.content.startsWith('<TSExternalLink')) {
682
570
  const dom = (0, _htmlparser.parseDOM)(item.content)[0];
@@ -687,7 +575,6 @@ function mdxToLinkData(item) {
687
575
  };
688
576
  }
689
577
  }
690
-
691
578
  function mdxToDraftjsImage(item, entityKeyGenerator) {
692
579
  if (item.content.startsWith(`<${mdxShortcodePrefix('Image')}`)) {
693
580
  const dom = (0, _htmlparser.parseDOM)(item.content)[0];
@@ -705,7 +592,6 @@ function mdxToDraftjsImage(item, entityKeyGenerator) {
705
592
  });
706
593
  }
707
594
  }
708
-
709
595
  function mdxToDraftjsPullquote(item) {
710
596
  if (item.content.startsWith(`<${mdxShortcodePrefix('Pullquote')}`)) {
711
597
  const dom = (0, _htmlparser.parseDOM)(item.content)[0];
@@ -715,7 +601,6 @@ function mdxToDraftjsPullquote(item) {
715
601
  });
716
602
  }
717
603
  }
718
-
719
604
  /**
720
605
  * Mutates result to replace empty entities with links,
721
606
  * assuming there is nothing else they could be...
@@ -723,106 +608,84 @@ function mdxToDraftjsPullquote(item) {
723
608
  function addLinks(state, links) {
724
609
  let linkNumber = 0;
725
610
  let linkOpen = false;
726
-
727
611
  for (const block of state.blocks) {
728
612
  let blockIsCustom = false;
729
-
730
613
  for (const [i, entityRange] of block.entityRanges.entries()) {
731
614
  var _entity$data9;
732
-
733
615
  const entity = state.entityMap[entityRange.key];
734
-
735
616
  if (entity === undefined) {
736
617
  throw new Error('Missing entity');
737
618
  }
738
-
739
619
  if (i === 0 && (_entity$data9 = entity.data) !== null && _entity$data9 !== void 0 && _entity$data9.marker) {
740
620
  blockIsCustom = true;
741
621
  continue;
742
622
  }
743
-
744
623
  if (blockIsCustom && i === Object.keys(block.entityRanges).length - 1) {
745
624
  continue;
746
- } // Ignore line breaks that were inserted around blocks
747
-
625
+ }
748
626
 
627
+ // Ignore line breaks that were inserted around blocks
749
628
  if (entity.depth === undefined) {
750
629
  continue;
751
630
  }
752
-
753
631
  const link = links[linkNumber];
754
-
755
632
  if (link && !linkOpen) {
756
633
  entity.type = 'LINK';
757
634
  entity.mutability = 'MUTABLE';
758
635
  entity.data = link;
759
-
760
636
  const decodedText = _he.default.decode(link.text);
761
-
762
637
  entity.text = decodedText;
763
638
  entityRange.length += decodedText.length;
764
639
  linkNumber++;
765
640
  }
766
-
767
641
  linkOpen = !linkOpen;
768
642
  }
769
643
  }
770
644
  }
645
+
771
646
  /**
772
647
  * Mutate state to replace blocks with our custom versions
773
648
  * Return a list of entity keys that should be removed
774
649
  */
775
-
776
-
777
650
  function replaceBlocks(state, replacementBlocks) {
778
651
  let entityKeysToExclude = [];
779
-
780
652
  for (let i = 0; i < state.blocks.length; i++) {
781
653
  const block = state.blocks[i];
782
-
783
654
  for (const entityKey of Object.keys(state.entityMap)) {
784
655
  let removeBlockEntityKeys = false;
785
656
  const blockEntityKeys = block.entityRanges.map(range => range.key);
786
-
787
657
  if (blockEntityKeys.includes(Number(entityKey))) {
788
658
  var _entity$data10;
789
-
790
659
  const entity = state.entityMap[entityKey];
791
660
  const markerKey = (_entity$data10 = entity.data) === null || _entity$data10 === void 0 ? void 0 : _entity$data10.marker;
792
661
  const replacementBlock = replacementBlocks[markerKey];
793
-
794
662
  if (replacementBlock && !removeBlockEntityKeys) {
795
663
  const originalBlock = state.blocks[i];
796
664
  const originalBlockText = state.blocks[i].text;
797
665
  replacementBlock.text = originalBlockText !== '' ? originalBlockText : replacementBlock.text;
798
666
  replacementBlock.inlineStyleRanges = originalBlock.inlineStyleRanges;
799
- const removeBlockEntities = entity.data.type === 'image' || entity.data.type === 'oembed'; // eslint-disable-next-line max-depth
800
-
667
+ const removeBlockEntities = entity.data.type === 'image' || entity.data.type === 'oembed';
668
+ // eslint-disable-next-line max-depth
801
669
  if (!removeBlockEntities) {
802
670
  replacementBlock.entityRanges = originalBlock.entityRanges.filter(entityRange => {
803
671
  var _state$entityMap$enti;
804
-
805
672
  return ((_state$entityMap$enti = state.entityMap[entityRange.key].data) === null || _state$entityMap$enti === void 0 ? void 0 : _state$entityMap$enti.marker) === undefined;
806
673
  });
807
674
  }
808
-
809
- state.blocks[i] = replacementBlock; // eslint-disable-next-line max-depth
810
-
675
+ state.blocks[i] = replacementBlock;
676
+ // eslint-disable-next-line max-depth
811
677
  if (removeBlockEntities) {
812
678
  removeBlockEntityKeys = true;
813
679
  }
814
680
  }
815
681
  }
816
-
817
682
  if (removeBlockEntityKeys) {
818
683
  entityKeysToExclude = entityKeysToExclude.concat(blockEntityKeys);
819
684
  }
820
685
  }
821
686
  }
822
-
823
687
  return entityKeysToExclude;
824
688
  }
825
-
826
689
  const blockStarts = [{
827
690
  regex: ' <TSImage',
828
691
  replacement: ' <TSImage'
@@ -850,59 +713,51 @@ const blockEnds = [{
850
713
  regex: '```',
851
714
  replacement: '```'
852
715
  }];
853
-
854
716
  const getLookBehindValue = (end, str) => {
855
717
  if (end.lookBehind) {
856
718
  // eslint-disable-next-line security-node/non-literal-reg-expr
857
719
  const behindMatch = new RegExp(end.lookBehind).exec(str);
858
-
859
720
  if (behindMatch) {
860
721
  return behindMatch[0];
861
722
  }
862
723
  }
863
-
864
724
  return '';
865
725
  };
726
+
866
727
  /**
867
728
  * Make sure there is a place to put the cursor around block-level items such as images and oembeds
868
729
  */
869
-
870
-
871
730
  function insertBreaksAroundBlocks(mdx) {
872
731
  for (const start of blockStarts) {
873
732
  // eslint-disable-next-line security-node/non-literal-reg-expr
874
733
  mdx = mdx.replace(new RegExp(`^${start.regex}`, 'g'), `<br/>\n\n${start.replacement}`);
875
734
  }
876
-
877
735
  for (const end of blockEnds) {
878
736
  // eslint-disable-next-line security-node/non-literal-reg-expr
879
737
  mdx = mdx.replace(new RegExp(`${end.lookBehind ?? ''}${end.regex}\\s*$`, 'g'), match => {
880
738
  return `${getLookBehindValue(end, match)}${end.replacement}\n\n<br/>`;
881
739
  });
882
-
883
740
  for (const start of blockStarts) {
884
- mdx = mdx.replace( // eslint-disable-next-line security-node/non-literal-reg-expr
741
+ mdx = mdx.replace(
742
+ // eslint-disable-next-line security-node/non-literal-reg-expr
885
743
  new RegExp(`${end.lookBehind ?? ''}${end.regex}\\s*${start.regex}`, 'g'), match => {
886
744
  return `${getLookBehindValue(end, match)}${end.replacement ?? 'FFF'}\n\n<br/>\n\n${start.replacement}`;
887
745
  });
888
746
  }
889
747
  }
890
-
891
748
  return mdx;
892
749
  }
893
-
894
750
  function mdxToDraftjs(mdx) {
895
751
  const replacementBlocks = {};
896
752
  const entities = {};
897
753
  const oembedKeyToHtml = {};
898
- let currentOembedKey; // Start really high to avoid conflicts with keys created by markdown-draft-js
754
+ let currentOembedKey;
899
755
 
756
+ // Start really high to avoid conflicts with keys created by markdown-draft-js
900
757
  let currentEntityKey = 1000000;
901
-
902
758
  function entityKeyGenerator() {
903
759
  return currentEntityKey++;
904
760
  }
905
-
906
761
  mdx = insertBreaksAroundBlocks(mdx);
907
762
  const links = [];
908
763
  const result = fromMd(mdx, {
@@ -910,21 +765,15 @@ function mdxToDraftjs(mdx) {
910
765
  if (item === undefined) {
911
766
  return getDraftjsEmpty();
912
767
  }
913
-
914
768
  const br = mdxToBr(item);
915
-
916
769
  if (br) {
917
770
  return br;
918
771
  }
919
-
920
772
  const linkData = mdxToLinkData(item);
921
-
922
773
  if (linkData) {
923
774
  links.push(linkData);
924
775
  }
925
-
926
776
  const image = mdxToDraftjsImage(item, entityKeyGenerator);
927
-
928
777
  if (image) {
929
778
  replacementBlocks[image.contentBlock.key] = image.contentBlock;
930
779
  Object.assign(entities, image.entities);
@@ -935,9 +784,7 @@ function mdxToDraftjs(mdx) {
935
784
  }
936
785
  };
937
786
  }
938
-
939
787
  const oembed = mdxToDraftjsOembed(item, entityKeyGenerator);
940
-
941
788
  if (oembed) {
942
789
  currentOembedKey = Object.keys(oembed.entities)[0];
943
790
  replacementBlocks[oembed.contentBlock.key] = oembed.contentBlock;
@@ -949,9 +796,7 @@ function mdxToDraftjs(mdx) {
949
796
  }
950
797
  };
951
798
  }
952
-
953
799
  const pullquote = mdxToDraftjsPullquote(item);
954
-
955
800
  if (pullquote) {
956
801
  replacementBlocks[pullquote.contentBlock.key] = pullquote.contentBlock;
957
802
  Object.assign(entities, pullquote.entities);
@@ -962,20 +807,16 @@ function mdxToDraftjs(mdx) {
962
807
  }
963
808
  };
964
809
  }
965
-
966
810
  return getDraftjsEmpty(item.level);
967
811
  }
968
-
969
812
  }, {
970
813
  htmlblock(item) {
971
814
  if (item && currentOembedKey) {
972
815
  oembedKeyToHtml[currentOembedKey] = item.content.replace(`</${mdxShortcodePrefix('Oembed')}>\n`, '');
973
816
  currentOembedKey = undefined;
974
817
  }
975
-
976
818
  return getDraftjsRemovalMarker();
977
819
  }
978
-
979
820
  });
980
821
  addLinks(result, links);
981
822
  const entityKeysToExclude = replaceBlocks(result, replacementBlocks);
@@ -983,11 +824,11 @@ function mdxToDraftjs(mdx) {
983
824
  (0, _forEach.default)(oembedKeyToHtml, (html, key) => {
984
825
  entities[key].data.html = html;
985
826
  });
986
- result.entityMap = (0, _pickBy.default)({ ...result.entityMap,
827
+ result.entityMap = (0, _pickBy.default)({
828
+ ...result.entityMap,
987
829
  ...entities
988
830
  }, (entity, key) => {
989
831
  var _entity$data11;
990
-
991
832
  return ((_entity$data11 = entity.data) === null || _entity$data11 === void 0 ? void 0 : _entity$data11.marker) === undefined && !entityKeysToExclude.includes(Number(key));
992
833
  });
993
834
  return result;