@lexical/markdown 0.1.18 → 0.1.21

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.
@@ -6,9 +6,9 @@
6
6
  */
7
7
  'use strict';
8
8
 
9
- var lexical = require('lexical');
10
9
  var code = require('@lexical/code');
11
10
  var list = require('@lexical/list');
11
+ var lexical = require('lexical');
12
12
  var link = require('@lexical/link');
13
13
  var richText = require('@lexical/rich-text');
14
14
  var text = require('@lexical/text');
@@ -23,7 +23,7 @@ var text = require('@lexical/text');
23
23
  */
24
24
  /*
25
25
  How to add a new syntax to capture and transform.
26
- 1. Create a new enumeration by adding to AutoFormatKind.
26
+ 1. Create a new enumeration by adding to MarkdownFormatKind.
27
27
  2. Add a new criteria with a regEx pattern. See markdownStrikethrough as an example.
28
28
  3. Add your block criteria (e.g. '# ') to allMarkdownCriteria or
29
29
  your text criteria (e.g. *MyItalic*) to allMarkdownCriteriaForTextNodes.
@@ -52,7 +52,7 @@ const triggers = [spaceTrigger
52
52
  ]; // Todo: speed up performance by having non-capture group variations of the regex.
53
53
 
54
54
  const autoFormatBase = {
55
- autoFormatKind: null,
55
+ markdownFormatKind: null,
56
56
  regEx: /(?:)/,
57
57
  regExForAutoFormatting: /(?:)/,
58
58
  requiresParagraphStart: false
@@ -61,89 +61,89 @@ const paragraphStartBase = { ...autoFormatBase,
61
61
  requiresParagraphStart: true
62
62
  };
63
63
  const markdownHeader1 = { ...paragraphStartBase,
64
- autoFormatKind: 'paragraphH1',
64
+ markdownFormatKind: 'paragraphH1',
65
65
  regEx: /^(?:#)/,
66
66
  regExForAutoFormatting: /^(?:# )/
67
67
  };
68
68
  const markdownHeader2 = { ...paragraphStartBase,
69
- autoFormatKind: 'paragraphH2',
69
+ markdownFormatKind: 'paragraphH2',
70
70
  regEx: /^(?:##)/,
71
71
  regExForAutoFormatting: /^(?:## )/
72
72
  };
73
73
  const markdownHeader3 = { ...paragraphStartBase,
74
- autoFormatKind: 'paragraphH2',
74
+ markdownFormatKind: 'paragraphH2',
75
75
  regEx: /^(?:###)/,
76
76
  regExForAutoFormatting: /^(?:### )/
77
77
  };
78
78
  const markdownBlockQuote = { ...paragraphStartBase,
79
- autoFormatKind: 'paragraphBlockQuote',
79
+ markdownFormatKind: 'paragraphBlockQuote',
80
80
  regEx: /^(?:>)/,
81
81
  regExForAutoFormatting: /^(?:> )/
82
82
  };
83
83
  const markdownUnorderedListDash = { ...paragraphStartBase,
84
- autoFormatKind: 'paragraphUnorderedList',
84
+ markdownFormatKind: 'paragraphUnorderedList',
85
85
  regEx: /^(?:- )/,
86
86
  regExForAutoFormatting: /^(?:- )/
87
87
  };
88
88
  const markdownUnorderedListAsterisk = { ...paragraphStartBase,
89
- autoFormatKind: 'paragraphUnorderedList',
89
+ markdownFormatKind: 'paragraphUnorderedList',
90
90
  regEx: /^(?:\* )/,
91
91
  regExForAutoFormatting: /^(?:\* )/
92
92
  };
93
93
  const markdownCodeBlock = { ...paragraphStartBase,
94
- autoFormatKind: 'paragraphCodeBlock',
94
+ markdownFormatKind: 'paragraphCodeBlock',
95
95
  regEx: /^(```)$/,
96
96
  regExForAutoFormatting: /^(```)([a-z]*)( )/
97
97
  };
98
98
  const markdownOrderedList = { ...paragraphStartBase,
99
- autoFormatKind: 'paragraphOrderedList',
99
+ markdownFormatKind: 'paragraphOrderedList',
100
100
  regEx: /^(\d+)\.\s/,
101
101
  regExForAutoFormatting: /^(\d+)\.\s/
102
102
  };
103
103
  const markdownHorizontalRule = { ...paragraphStartBase,
104
- autoFormatKind: 'horizontalRule',
104
+ markdownFormatKind: 'horizontalRule',
105
105
  regEx: /^(?:\*\*\*)$/,
106
106
  regExForAutoFormatting: /^(?:\*\*\* )/
107
107
  };
108
108
  const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
109
- autoFormatKind: 'horizontalRule',
109
+ markdownFormatKind: 'horizontalRule',
110
110
  regEx: /^(?:---)$/,
111
111
  regExForAutoFormatting: /^(?:--- )/
112
112
  };
113
113
  const markdownItalic = { ...autoFormatBase,
114
- autoFormatKind: 'italic',
114
+ markdownFormatKind: 'italic',
115
115
  regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*)/,
116
116
  regExForAutoFormatting: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/
117
117
  };
118
118
  const markdownBold = { ...autoFormatBase,
119
- autoFormatKind: 'bold',
119
+ markdownFormatKind: 'bold',
120
120
  regEx: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)/,
121
121
  regExForAutoFormatting: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/
122
122
  };
123
123
  const markdownBoldWithUnderlines = { ...autoFormatBase,
124
- autoFormatKind: 'bold',
124
+ markdownFormatKind: 'bold',
125
125
  regEx: /(__)(\s*)([^__]*)(\s*)(__)/,
126
126
  regExForAutoFormatting: /(__)(\s*)([^__]*)(\s*)(__)(\s)$/
127
127
  };
128
128
  const markdownBoldItalic = { ...autoFormatBase,
129
- autoFormatKind: 'bold_italic',
129
+ markdownFormatKind: 'bold_italic',
130
130
  regEx: /(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)/,
131
131
  regExForAutoFormatting: /(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)(\s)$/
132
132
  }; // Markdown does not support underline, but we can allow folks to use
133
133
  // the HTML tags for underline.
134
134
 
135
135
  const fakeMarkdownUnderline = { ...autoFormatBase,
136
- autoFormatKind: 'underline',
136
+ markdownFormatKind: 'underline',
137
137
  regEx: /(\<u\>)(\s*\b)([^\<]*)(\b\s*)(\<\/u\>)/,
138
138
  regExForAutoFormatting: /(\<u\>)(\s*\b)([^\<]*)(\b\s*)(\<\/u\>)(\s)$/
139
139
  };
140
140
  const markdownStrikethrough = { ...autoFormatBase,
141
- autoFormatKind: 'strikethrough',
141
+ markdownFormatKind: 'strikethrough',
142
142
  regEx: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~)/,
143
143
  regExForAutoFormatting: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/
144
144
  };
145
145
  const markdownLink = { ...autoFormatBase,
146
- autoFormatKind: 'link',
146
+ markdownFormatKind: 'link',
147
147
  regEx: /(\[)(.+)(\]\()([^ ]+)(?: \"(?:.+)\")?(\))/,
148
148
  regExForAutoFormatting: /(\[)(.+)(\]\()([^ ]+)(?: \"(?:.+)\")?(\))(\s)$/
149
149
  };
@@ -151,11 +151,13 @@ const allMarkdownCriteriaForTextNodes = [markdownBoldItalic, markdownItalic, mar
151
151
  const allMarkdownCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, markdownHorizontalRule, markdownHorizontalRuleUsingDashes, ...allMarkdownCriteriaForTextNodes];
152
152
  function getInitialScanningContext(editor, isAutoFormatting, textNodeWithOffset, triggerState) {
153
153
  return {
154
+ currentElementNode: null,
154
155
  editor,
155
156
  isAutoFormatting,
157
+ isWithinCodeBlock: false,
156
158
  joinedText: null,
157
159
  markdownCriteria: {
158
- autoFormatKind: 'noTransformation',
160
+ markdownFormatKind: 'noTransformation',
159
161
  regEx: /(?:)/,
160
162
  // Empty reg ex.
161
163
  regExForAutoFormatting: /(?:)/,
@@ -172,7 +174,7 @@ function getInitialScanningContext(editor, isAutoFormatting, textNodeWithOffset,
172
174
  function resetScanningContext(scanningContext) {
173
175
  scanningContext.joinedText = '';
174
176
  scanningContext.markdownCriteria = {
175
- autoFormatKind: 'noTransformation',
177
+ markdownFormatKind: 'noTransformation',
176
178
  regEx: /(?:)/,
177
179
  // Empty reg ex.
178
180
  regExForAutoFormatting: /(?:)/,
@@ -186,6 +188,13 @@ function resetScanningContext(scanningContext) {
186
188
  scanningContext.textNodeWithOffset = null;
187
189
  return scanningContext;
188
190
  }
191
+ function getCodeBlockCriteria() {
192
+ return markdownCodeBlock;
193
+ }
194
+ function getPatternMatchResultsForCodeBlock(scanningContext, text) {
195
+ const markdownCriteria = getCodeBlockCriteria();
196
+ return getPatternMatchResultsWithRegEx(text, true, false, scanningContext.isAutoFormatting ? markdownCriteria.regExForAutoFormatting : markdownCriteria.regEx);
197
+ }
189
198
 
190
199
  function getPatternMatchResultsWithRegEx(textToSearch, matchMustAppearAtStartOfString, matchMustAppearAtEndOfString, regEx) {
191
200
  const patternMatchResults = {
@@ -233,7 +242,7 @@ function getPatternMatchResultsForParagraphs(markdownCriteria, scanningContext)
233
242
 
234
243
  if (textNodeWithOffset.node.getPreviousSibling() === null) {
235
244
  const textToSearch = textNodeWithOffset.node.getTextContent();
236
- return getPatternMatchResultsWithRegEx(textToSearch, true, false, markdownCriteria.regExForAutoFormatting);
245
+ return getPatternMatchResultsWithRegEx(textToSearch, true, false, scanningContext.isAutoFormatting ? markdownCriteria.regExForAutoFormatting : markdownCriteria.regEx);
237
246
  }
238
247
 
239
248
  return null;
@@ -259,38 +268,51 @@ function getPatternMatchResultsForText(markdownCriteria, scanningContext) {
259
268
 
260
269
  function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNode) {
261
270
  let newNode = null;
271
+ const shouldDelete = false;
262
272
  const children = element.getChildren();
263
273
  const markdownCriteria = scanningContext.markdownCriteria;
264
274
  const patternMatchResults = scanningContext.patternMatchResults;
265
275
 
266
- if (markdownCriteria.autoFormatKind != null) {
267
- switch (markdownCriteria.autoFormatKind) {
276
+ if (markdownCriteria.markdownFormatKind != null) {
277
+ switch (markdownCriteria.markdownFormatKind) {
268
278
  case 'paragraphH1':
269
279
  {
270
280
  newNode = richText.$createHeadingNode('h1');
271
281
  newNode.append(...children);
272
- return newNode;
282
+ return {
283
+ newNode,
284
+ shouldDelete
285
+ };
273
286
  }
274
287
 
275
288
  case 'paragraphH2':
276
289
  {
277
290
  newNode = richText.$createHeadingNode('h2');
278
291
  newNode.append(...children);
279
- return newNode;
292
+ return {
293
+ newNode,
294
+ shouldDelete
295
+ };
280
296
  }
281
297
 
282
298
  case 'paragraphH3':
283
299
  {
284
300
  newNode = richText.$createHeadingNode('h3');
285
301
  newNode.append(...children);
286
- return newNode;
302
+ return {
303
+ newNode,
304
+ shouldDelete
305
+ };
287
306
  }
288
307
 
289
308
  case 'paragraphBlockQuote':
290
309
  {
291
310
  newNode = richText.$createQuoteNode();
292
311
  newNode.append(...children);
293
- return newNode;
312
+ return {
313
+ newNode,
314
+ shouldDelete
315
+ };
294
316
  }
295
317
 
296
318
  case 'paragraphUnorderedList':
@@ -299,7 +321,10 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
299
321
  const listItem = list.$createListItemNode();
300
322
  listItem.append(...children);
301
323
  newNode.append(listItem);
302
- return newNode;
324
+ return {
325
+ newNode,
326
+ shouldDelete
327
+ };
303
328
  }
304
329
 
305
330
  case 'paragraphOrderedList':
@@ -310,12 +335,55 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
310
335
  const listItem = list.$createListItemNode();
311
336
  listItem.append(...children);
312
337
  newNode.append(listItem);
313
- return newNode;
338
+ return {
339
+ newNode,
340
+ shouldDelete
341
+ };
314
342
  }
315
343
 
316
344
  case 'paragraphCodeBlock':
317
345
  {
318
346
  // Toggle code and paragraph nodes.
347
+ if (scanningContext.isAutoFormatting === false) {
348
+ const shouldToggle = scanningContext.patternMatchResults.regExCaptureGroups.length > 0;
349
+
350
+ if (shouldToggle) {
351
+ scanningContext.isWithinCodeBlock = scanningContext.isWithinCodeBlock !== true; // When toggling, always clear the code block element node.
352
+
353
+ scanningContext.currentElementNode = null;
354
+ return {
355
+ newNode: null,
356
+ shouldDelete: true
357
+ };
358
+ }
359
+
360
+ if (scanningContext.isWithinCodeBlock) {
361
+ // Create the code block and return it to the caller.
362
+ if (scanningContext.currentElementNode == null) {
363
+ const newCodeBlockNode = code.$createCodeNode();
364
+ newCodeBlockNode.append(...children);
365
+ scanningContext.currentElementNode = newCodeBlockNode;
366
+ return {
367
+ newNode: newCodeBlockNode,
368
+ shouldDelete: false
369
+ };
370
+ } // Build up the code block with a line break and the children.
371
+
372
+
373
+ if (scanningContext.currentElementNode != null) {
374
+ const codeBlockNode = scanningContext.currentElementNode;
375
+ const lineBreakNode = lexical.$createLineBreakNode();
376
+ codeBlockNode.append(lineBreakNode);
377
+ codeBlockNode.append(...children);
378
+ }
379
+ }
380
+
381
+ return {
382
+ newNode: null,
383
+ shouldDelete: true
384
+ };
385
+ }
386
+
319
387
  if (scanningContext.triggerState != null && scanningContext.triggerState.isCodeBlock) {
320
388
  newNode = lexical.$createParagraphNode();
321
389
  } else {
@@ -328,53 +396,70 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
328
396
  }
329
397
 
330
398
  newNode.append(...children);
331
- return newNode;
399
+ return {
400
+ newNode,
401
+ shouldDelete
402
+ };
332
403
  }
333
404
 
334
405
  case 'horizontalRule':
335
406
  {
336
- // return null for newNode. Insert the HR here.
337
- const horizontalRuleNode = createHorizontalRuleNode();
338
- element.insertBefore(horizontalRuleNode);
407
+ if (createHorizontalRuleNode != null) {
408
+ // return null for newNode. Insert the HR here.
409
+ const horizontalRuleNode = createHorizontalRuleNode();
410
+ element.insertBefore(horizontalRuleNode);
411
+ }
412
+
339
413
  break;
340
414
  }
341
415
  }
342
416
  }
343
417
 
344
- return newNode;
418
+ return {
419
+ newNode,
420
+ shouldDelete
421
+ };
345
422
  }
346
423
 
347
424
  function transformTextNodeForParagraphs(scanningContext, createHorizontalRuleNode) {
348
425
  const textNodeWithOffset = getTextNodeWithOffsetOrThrow(scanningContext);
349
426
  const element = textNodeWithOffset.node.getParentOrThrow();
350
- const text = scanningContext.patternMatchResults.regExCaptureGroups[0].text; // Remove the text which we matched.
351
427
 
352
- const textNode = textNodeWithOffset.node.spliceText(0, text.length, '', true);
428
+ if (scanningContext.patternMatchResults.regExCaptureGroups.length > 0) {
429
+ const text = scanningContext.patternMatchResults.regExCaptureGroups[0].text; // Remove the text which we matched.
353
430
 
354
- if (textNode.getTextContent() === '') {
355
- textNode.selectPrevious();
356
- textNode.remove();
431
+ const textNode = textNodeWithOffset.node.spliceText(0, text.length, '', true);
432
+
433
+ if (textNode.getTextContent() === '') {
434
+ textNode.selectPrevious();
435
+ textNode.remove();
436
+ }
357
437
  } // Transform the current element kind to the new element kind.
358
438
 
359
439
 
360
- const elementNode = getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNode);
440
+ const {
441
+ newNode,
442
+ shouldDelete
443
+ } = getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNode);
361
444
 
362
- if (elementNode !== null) {
363
- element.replace(elementNode);
445
+ if (shouldDelete) {
446
+ element.remove();
447
+ } else if (newNode !== null) {
448
+ element.replace(newNode);
364
449
  }
365
450
  }
366
451
  function transformTextNodeForText(scanningContext) {
367
452
  const markdownCriteria = scanningContext.markdownCriteria;
368
453
 
369
- if (markdownCriteria.autoFormatKind != null) {
370
- const formatting = getTextFormatType(markdownCriteria.autoFormatKind);
454
+ if (markdownCriteria.markdownFormatKind != null) {
455
+ const formatting = getTextFormatType(markdownCriteria.markdownFormatKind);
371
456
 
372
457
  if (formatting != null) {
373
458
  transformTextNodeWithFormatting(formatting, scanningContext);
374
459
  return;
375
460
  }
376
461
 
377
- if (markdownCriteria.autoFormatKind === 'link') {
462
+ if (markdownCriteria.markdownFormatKind === 'link') {
378
463
  transformTextNodeWithLink(scanningContext);
379
464
  }
380
465
  }
@@ -466,13 +551,13 @@ function getJoinedTextLength(patternMatchResults) {
466
551
  return patternMatchResults.regExCaptureGroups[lastGroupIndex].offsetInParent + patternMatchResults.regExCaptureGroups[lastGroupIndex].text.length;
467
552
  }
468
553
 
469
- function getTextFormatType(autoFormatKind) {
470
- switch (autoFormatKind) {
554
+ function getTextFormatType(markdownFormatKind) {
555
+ switch (markdownFormatKind) {
471
556
  case 'italic':
472
557
  case 'bold':
473
558
  case 'underline':
474
559
  case 'strikethrough':
475
- return [autoFormatKind];
560
+ return [markdownFormatKind];
476
561
 
477
562
  case 'bold_italic':
478
563
  {
@@ -689,7 +774,7 @@ function getCriteriaWithPatternMatchResults(markdownCriteriaArray, scanningConte
689
774
  for (let i = 0; i < count; i++) {
690
775
  const markdownCriteria = markdownCriteriaArray[i]; // Skip code block nodes, unless the autoFormatKind calls for toggling the code block.
691
776
 
692
- if (currentTriggerState != null && currentTriggerState.isCodeBlock === false || markdownCriteria.autoFormatKind === 'paragraphCodeBlock') {
777
+ if (currentTriggerState != null && currentTriggerState.isCodeBlock === false || markdownCriteria.markdownFormatKind === 'paragraphCodeBlock') {
693
778
  const patternMatchResults = getPatternMatchResultsForCriteria(markdownCriteria, scanningContext);
694
779
 
695
780
  if (patternMatchResults != null) {
@@ -776,7 +861,7 @@ function findScanningContext(editor, currentTriggerState, priorTriggerState) {
776
861
  const currentTextContentLength = currentTriggerState.textContent.length;
777
862
  const triggerOffset = currentTriggerState.anchorOffset - triggerStringLength;
778
863
 
779
- if ((currentTriggerState.hasParentNode === true && currentTriggerState.isSimpleText && currentTriggerState.isSelectionCollapsed && currentTriggerState.nodeKey === priorTriggerState.nodeKey && currentTriggerState.anchorOffset !== priorTriggerState.anchorOffset && triggerOffset >= 0 && triggerOffset + triggerStringLength <= currentTextContentLength && currentTriggerState.textContent.substr(triggerOffset, triggerStringLength) === triggerString && // Some code differentiation needed if trigger kind is not a simple space character.
864
+ if ((currentTriggerState.hasParentNode === true && currentTriggerState.isSimpleText && currentTriggerState.isSelectionCollapsed && currentTriggerState.anchorOffset !== priorTriggerState.anchorOffset && triggerOffset >= 0 && triggerOffset + triggerStringLength <= currentTextContentLength && currentTriggerState.textContent.substr(triggerOffset, triggerStringLength) === triggerString && // Some code differentiation needed if trigger kind is not a simple space character.
780
865
  currentTriggerState.textContent !== priorTriggerState.textContent) === false) {
781
866
  return null;
782
867
  }
@@ -794,17 +879,62 @@ function findScanningContext(editor, currentTriggerState, priorTriggerState) {
794
879
  *
795
880
  */
796
881
  function convertStringToLexical(text, editor) {
797
- const nodes = text.split('\n').map(splitText => lexical.$createParagraphNode().append(lexical.$createTextNode(splitText)));
798
- const root = lexical.$getRoot();
799
- root.clear();
800
- root.append(...nodes);
882
+ if (!text.length) {
883
+ return null;
884
+ }
885
+
886
+ const nodes = [];
887
+ const splitLines = text.split('\n');
888
+ const splitLinesCount = splitLines.length;
889
+
890
+ for (let i = 0; i < splitLinesCount; i++) {
891
+ if (splitLines[i].length > 0) {
892
+ nodes.push(lexical.$createParagraphNode().append(lexical.$createTextNode(splitLines[i])));
893
+ } else {
894
+ nodes.push(lexical.$createParagraphNode());
895
+ }
896
+ }
897
+
898
+ if (nodes.length) {
899
+ const root = lexical.$getRoot();
900
+ root.clear();
901
+ root.append(...nodes);
902
+ return root;
903
+ }
904
+
905
+ return null;
801
906
  }
802
907
 
803
- function convertElementNodeContainingMarkdown(scanningContext, elementNode) {
804
- // Handle code block to be done.
805
- // Handle paragraph nodes below.
806
- if (lexical.$isParagraphNode(elementNode)) {
908
+ function convertElementNodeContainingMarkdown(scanningContext, elementNode, createHorizontalRuleNode) {
909
+ const textContent = elementNode.getTextContent(); // Handle paragraph nodes below.
910
+
911
+ if (lexical.$isParagraphNode(elementNode) && elementNode.getChildren().length) {
807
912
  const paragraphNode = elementNode;
913
+ const firstChild = paragraphNode.getFirstChild();
914
+ const firstChildIsTextNode = lexical.$isTextNode(firstChild); // Handle conversion to code block.
915
+
916
+ if (scanningContext.isWithinCodeBlock === true) {
917
+ if (!(firstChild != null && firstChildIsTextNode)) {
918
+ throw Error(`Expect paragraph containing only text nodes.`);
919
+ }
920
+
921
+ scanningContext.textNodeWithOffset = {
922
+ node: firstChild,
923
+ offset: 0
924
+ };
925
+ const patternMatchResults = getPatternMatchResultsForCodeBlock(scanningContext, textContent);
926
+
927
+ if (patternMatchResults != null) {
928
+ // Toggle transform to or from code block.
929
+ scanningContext.patternMatchResults = patternMatchResults;
930
+ }
931
+
932
+ scanningContext.markdownCriteria = getCodeBlockCriteria(); // Perform text transformation here.
933
+
934
+ transformTextNodeForParagraphs(scanningContext, createHorizontalRuleNode);
935
+ return;
936
+ }
937
+
808
938
  const allCriteria = getAllMarkdownCriteria();
809
939
  const count = allCriteria.length;
810
940
 
@@ -812,9 +942,7 @@ function convertElementNodeContainingMarkdown(scanningContext, elementNode) {
812
942
  const criteria = allCriteria[i];
813
943
 
814
944
  if (criteria.requiresParagraphStart === true) {
815
- const firstChild = paragraphNode.getFirstChild();
816
-
817
- if (!lexical.$isTextNode(firstChild)) {
945
+ if (!(firstChild != null && firstChildIsTextNode)) {
818
946
  throw Error(`Expect paragraph containing only text nodes.`);
819
947
  }
820
948
 
@@ -828,28 +956,47 @@ function convertElementNodeContainingMarkdown(scanningContext, elementNode) {
828
956
  if (patternMatchResults != null) {
829
957
  // Lazy fill-in the particular format criteria and any matching result information.
830
958
  scanningContext.markdownCriteria = criteria;
831
- scanningContext.patternMatchResults = patternMatchResults; // Todo: perform text transformation here.
959
+ scanningContext.patternMatchResults = patternMatchResults; // Perform text transformation here.
960
+
961
+ transformTextNodeForParagraphs(scanningContext, createHorizontalRuleNode);
962
+ return;
832
963
  }
833
964
  }
834
965
  }
835
966
  }
836
967
  }
837
968
 
838
- function convertMarkdownForElementNodes(elementNodes, editor) {
969
+ function convertMarkdownForElementNodes(editor, createHorizontalRuleNode) {
839
970
  // Please see the declaration of ScanningContext for a detailed explanation.
840
971
  const scanningContext = getInitialScanningContext(editor, false, null, null);
841
- const count = elementNodes.length;
972
+ const root = lexical.$getRoot();
973
+ let done = false;
974
+ let startIndex = 0;
842
975
 
843
- for (let i = 0; i < count; i++) {
844
- const elementNode = elementNodes[i];
976
+ while (!done) {
977
+ done = true;
978
+ const elementNodes = root.getChildren();
979
+ const countOfElementNodes = elementNodes.length;
845
980
 
846
- if (lexical.$isElementNode(elementNode) && elementNode.getTextContent().length && elementNode.getChildren().length) {
847
- convertElementNodeContainingMarkdown(scanningContext, elementNode);
848
- } // Reset the scanning information that relates to the particular element node.
981
+ for (let i = startIndex; i < countOfElementNodes; i++) {
982
+ const elementNode = elementNodes[i];
849
983
 
984
+ if (lexical.$isElementNode(elementNode)) {
985
+ convertElementNodeContainingMarkdown(scanningContext, elementNode, createHorizontalRuleNode);
986
+ } // Reset the scanning information that relates to the particular element node.
987
+
988
+
989
+ resetScanningContext(scanningContext);
990
+
991
+ if (root.getChildren().length !== countOfElementNodes) {
992
+ // The conversion added or removed an from root's children.
993
+ startIndex = i;
994
+ done = false;
995
+ break;
996
+ }
997
+ }
998
+ } // while
850
999
 
851
- resetScanningContext(scanningContext);
852
- }
853
1000
  }
854
1001
 
855
1002
  /**
@@ -885,8 +1032,9 @@ function registerMarkdownShortcuts(editor, createHorizontalRuleNode) {
885
1032
  });
886
1033
  }
887
1034
  function $convertFromMarkdownString(markdownString, editor, createHorizontalRuleNode) {
888
- convertStringToLexical(markdownString);
889
- convertMarkdownForElementNodes(lexical.$getRoot().getChildren(), editor);
1035
+ if (convertStringToLexical(markdownString) != null) {
1036
+ convertMarkdownForElementNodes(editor, createHorizontalRuleNode);
1037
+ }
890
1038
  }
891
1039
 
892
1040
  exports.$convertFromMarkdownString = $convertFromMarkdownString;
@@ -4,26 +4,29 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var h=require("lexical"),n=require("@lexical/code"),p=require("@lexical/list"),r=require("@lexical/link"),t=require("@lexical/rich-text"),u=require("@lexical/text");function w(e){throw Error(`Minified Lexical error #${e}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
- const x=[{triggerKind:"space_trigger",triggerString:" "}],y={autoFormatKind:null,regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:!1},z={...y,requiresParagraphStart:!0},A=[{...y,autoFormatKind:"bold_italic",regEx:/(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)/,regExForAutoFormatting:/(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)(\s)$/},{...y,autoFormatKind:"italic",regEx:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)/,regExForAutoFormatting:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/},{...y,autoFormatKind:"bold",
9
- regEx:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)/,regExForAutoFormatting:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/},{...y,autoFormatKind:"bold",regEx:/(__)(\s*)([^__]*)(\s*)(__)/,regExForAutoFormatting:/(__)(\s*)([^__]*)(\s*)(__)(\s)$/},{...y,autoFormatKind:"underline",regEx:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)/,regExForAutoFormatting:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)(\s)$/},{...y,autoFormatKind:"strikethrough",regEx:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)/,regExForAutoFormatting:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/},
10
- {...y,autoFormatKind:"link",regEx:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))/,regExForAutoFormatting:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))(\s)$/}],B=[{...z,autoFormatKind:"paragraphH1",regEx:/^(?:#)/,regExForAutoFormatting:/^(?:# )/},{...z,autoFormatKind:"paragraphH2",regEx:/^(?:##)/,regExForAutoFormatting:/^(?:## )/},{...z,autoFormatKind:"paragraphH2",regEx:/^(?:###)/,regExForAutoFormatting:/^(?:### )/},{...z,autoFormatKind:"paragraphBlockQuote",regEx:/^(?:>)/,regExForAutoFormatting:/^(?:> )/},
11
- {...z,autoFormatKind:"paragraphUnorderedList",regEx:/^(?:- )/,regExForAutoFormatting:/^(?:- )/},{...z,autoFormatKind:"paragraphUnorderedList",regEx:/^(?:\* )/,regExForAutoFormatting:/^(?:\* )/},{...z,autoFormatKind:"paragraphOrderedList",regEx:/^(\d+)\.\s/,regExForAutoFormatting:/^(\d+)\.\s/},{...z,autoFormatKind:"paragraphCodeBlock",regEx:/^(```)$/,regExForAutoFormatting:/^(```)([a-z]*)( )/},{...z,autoFormatKind:"horizontalRule",regEx:/^(?:\*\*\*)$/,regExForAutoFormatting:/^(?:\*\*\* )/},{...z,autoFormatKind:"horizontalRule",
12
- regEx:/^(?:---)$/,regExForAutoFormatting:/^(?:--- )/},...A];function C(e,c,f,a){return{editor:e,isAutoFormatting:c,joinedText:null,markdownCriteria:{autoFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null},patternMatchResults:{regExCaptureGroups:[]},textNodeWithOffset:f,triggerState:a}}
13
- function D(e,c,f,a){const b={regExCaptureGroups:[]};a=e.match(a);if(null!==a&&0<a.length&&(!1===c||0===a.index)&&(!1===f||a.index+a[0].length===e.length)){e=a.length;c=a.index;for(f=0;f<e;f++){const d=a[f];b.regExCaptureGroups.push({offsetInParent:c,text:d});0<f&&(c+=d.length)}return b}return null}function E(e){e=e.textNodeWithOffset;null==e&&w(82);return e}function F(e,c){c=E(c);return null===c.node.getPreviousSibling()?(c=c.node.getTextContent(),D(c,!0,!1,e.regExForAutoFormatting)):null}
14
- function G(e,c,f,a,b){var d=b.patternMatchResults;const g=d.regExCaptureGroups;var k=g.length;if(e>=k||c>=k)return null;b=E(b).node.getParentOrThrow();k=d.regExCaptureGroups.length;2>k?d=0:(--k,d=d.regExCaptureGroups[k].offsetInParent+d.regExCaptureGroups[k].text.length);e=g[e];c=g[c];a=a?c.offsetInParent+c.text.length:c.offsetInParent;f=u.$findNodeWithOffsetFromJoinedText(f?e.offsetInParent+e.text.length:e.offsetInParent,d,1,b);a=u.$findNodeWithOffsetFromJoinedText(a,d,1,b);if(null==f||null==a)return null;
15
- b=h.$createRangeSelection();b.anchor.set(f.node.getKey(),f.offset,"text");b.focus.set(a.node.getKey(),a.offset,"text");return b}function H(e,c,f){const a=f.patternMatchResults.regExCaptureGroups;f=G(e,c,!1,!0,f);if(null!=f&&(h.$setSelection(f),f=h.$getSelection(),null!=f&&h.$isRangeSelection(f)&&!1===f.isCollapsed())){f.removeText();f=0;const b=a.length;for(let d=e;d<b;d++){const g=a[d];d>e&&(g.offsetInParent-=f);d<=c&&(f+=g.text.length,g.text="")}}}
16
- function I(e){var c=e.patternMatchResults.regExCaptureGroups.length;2>c||(--c,e=G(c,c,!0,!0,e),null!=e&&h.$setSelection(e))}
17
- function J(e,c,f){e.update(()=>{if(!0===c.markdownCriteria.requiresParagraphStart){var a=E(c),b=a.node.getParentOrThrow();a=a.node.spliceText(0,c.patternMatchResults.regExCaptureGroups[0].text.length,"",!0);""===a.getTextContent()&&(a.selectPrevious(),a.remove());var d=b;a=null;var g=d.getChildren(),k=c.markdownCriteria;const l=c.patternMatchResults;if(null!=k.autoFormatKind)switch(k.autoFormatKind){case "paragraphH1":a=t.$createHeadingNode("h1");a.append(...g);break;case "paragraphH2":a=t.$createHeadingNode("h2");
18
- a.append(...g);break;case "paragraphH3":a=t.$createHeadingNode("h3");a.append(...g);break;case "paragraphBlockQuote":a=t.$createQuoteNode();a.append(...g);break;case "paragraphUnorderedList":a=p.$createListNode("ul");d=p.$createListItemNode();d.append(...g);a.append(d);break;case "paragraphOrderedList":a=parseInt(1<l.regExCaptureGroups.length?l.regExCaptureGroups[l.regExCaptureGroups.length-1].text:"1",10);a=p.$createListNode("ol",a);d=p.$createListItemNode();d.append(...g);a.append(d);break;case "paragraphCodeBlock":null!=
19
- c.triggerState&&c.triggerState.isCodeBlock?a=h.$createParagraphNode():(a=n.$createCodeNode(),d=3<=l.regExCaptureGroups.length?l.regExCaptureGroups[2].text:null,null!=d&&0<d.length&&a.setLanguage(d));a.append(...g);break;case "horizontalRule":g=f(),d.insertBefore(g)}null!==a&&b.replace(a)}else if(b=c.markdownCriteria,null!=b.autoFormatKind){a:{a=b.autoFormatKind;switch(a){case "italic":case "bold":case "underline":case "strikethrough":a=[a];break a;case "bold_italic":a=["bold","italic"];break a}a=
20
- null}if(null!=a){if(b=a,7===c.patternMatchResults.regExCaptureGroups.length){H(5,5,c);H(1,1,c);a=c.patternMatchResults.regExCaptureGroups;3<a.length||w(65);if(0!==a[3].text.length&&(a=G(3,3,!1,!0,c),null!=a&&(h.$setSelection(a),a=h.$getSelection(),h.$isRangeSelection(a))))for(g=0;g<b.length;g++)a.formatText(b[g]);I(c)}}else if("link"===b.autoFormatKind&&(b=c.patternMatchResults.regExCaptureGroups,7===b.length&&(g=b[2].text,b=b[4].text,0!==g.length&&0!==b.length))){H(1,5,c);a=c.patternMatchResults.regExCaptureGroups;
21
- if(!(1>=a.length)&&(g={offsetInParent:a[1].offsetInParent,text:g},d=G(1,1,!1,!1,c),null!=d&&(h.$setSelection(d),d=h.$getSelection(),null!=d&&h.$isRangeSelection(d)&&d.isCollapsed())))for(d.insertText(g.text),a.splice(1,0,g),g=g.text.length,d=a.length,k=2;k<d;k++)a[k].offsetInParent+=g;a=G(1,1,!1,!0,c);null!=a&&(h.$setSelection(a),c.editor.dispatchCommand(r.TOGGLE_LINK_COMMAND,b),I(c))}}},{tag:"history-push"})}
22
- function K(e,c){let f=null;e.getEditorState().read(()=>{var a=h.$getSelection();if(h.$isRangeSelection(a)){var b=a.anchor.getNode();a=h.$isTextNode(b)?{node:b,offset:a.anchor.offset}:null}else a=null;if(null!==a){a=C(e,!0,a,c);a:{b=!1===c.isParentAListItemNode?B:A;const k=a.triggerState,l=b.length;for(let m=0;m<l;m++){const v=b[m];if(null!=k&&!1===k.isCodeBlock||"paragraphCodeBlock"===v.autoFormatKind){var d=v,g=a;if(!0===d.requiresParagraphStart)d=F(d,g);else{if(null==g.joinedText){const q=E(g).node.getParentOrThrow();
23
- h.$isElementNode(q)?null==g.joinedText&&(g.joinedText=u.$joinTextNodesInElementNode(q,"\u0004",E(g))):w(52,q.__key)}d=D(g.joinedText,!1,!0,d.regExForAutoFormatting)}if(null!=d){b={markdownCriteria:v,patternMatchResults:d};break a}}}b={markdownCriteria:null,patternMatchResults:null}}null!==b.markdownCriteria&&null!==b.patternMatchResults&&(f=a,f.markdownCriteria=b.markdownCriteria,f.patternMatchResults=b.patternMatchResults)}});return f}
24
- function L(e){let c=null;e.read(()=>{const f=h.$getSelection();if(h.$isRangeSelection(f)&&f.isCollapsed()){var a=f.anchor.getNode(),b=a.getParent(),d=p.$isListItemNode(b);c={anchorOffset:f.anchor.offset,hasParentNode:null!==b,isCodeBlock:n.$isCodeNode(a),isParentAListItemNode:d,isSelectionCollapsed:!0,isSimpleText:h.$isTextNode(a)&&a.isSimpleText(),nodeKey:a.getKey(),textContent:a.getTextContent()}}});return c}
25
- function M(e){e=e.split("\n").map(f=>h.$createParagraphNode().append(h.$createTextNode(f)));const c=h.$getRoot();c.clear();c.append(...e)}
26
- exports.$convertFromMarkdownString=function(e,c){M(e);e=h.$getRoot().getChildren();c=C(c,!1,null,null);const f=e.length;for(let g=0;g<f;g++){var a=e[g];if(h.$isElementNode(a)&&a.getTextContent().length&&a.getChildren().length){var b=c;if(h.$isParagraphNode(a)){const k=B.length;for(let l=0;l<k;l++){const m=B[l];if(!0===m.requiresParagraphStart){var d=a.getFirstChild();h.$isTextNode(d)||w(80);b.textNodeWithOffset={node:d,offset:0};b.joinedText=a.getTextContent();d=F(m,b);null!=d&&(b.markdownCriteria=
27
- m,b.patternMatchResults=d)}}}}b=c;b.joinedText="";b.markdownCriteria={autoFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null};b.patternMatchResults={regExCaptureGroups:[]};b.triggerState=null;b.textNodeWithOffset=null}};
28
- exports.registerMarkdownShortcuts=function(e,c){let f=null;return e.registerUpdateListener(({tags:a})=>{if(!1===a.has("historic")){a=L(e.getEditorState());if(null==a)var b=null;else a:{b=a;var d=f;if(null==b||null==d)b=null;else{var g=x.length;for(let k=0;k<g;k++){const l=x[k].triggerString,m=l.length,v=b.textContent.length,q=b.anchorOffset-m;if(!1===(!0===b.hasParentNode&&b.isSimpleText&&b.isSelectionCollapsed&&b.nodeKey===d.nodeKey&&b.anchorOffset!==d.anchorOffset&&0<=q&&q+m<=v&&b.textContent.substr(q,
29
- m)===l&&b.textContent!==d.textContent)){b=null;break a}}b=K(e,b)}}null!=b&&J(e,b,c);f=a}else f=null})};
7
+ var k=require("@lexical/code"),p=require("@lexical/list"),q=require("lexical"),t=require("@lexical/link"),u=require("@lexical/rich-text"),v=require("@lexical/text");function w(b){throw Error(`Minified Lexical error #${b}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
+ const y=[{triggerKind:"space_trigger",triggerString:" "}],z={markdownFormatKind:null,regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:!1},A={...z,requiresParagraphStart:!0},B={...A,markdownFormatKind:"paragraphCodeBlock",regEx:/^(```)$/,regExForAutoFormatting:/^(```)([a-z]*)( )/},C=[{...z,markdownFormatKind:"bold_italic",regEx:/(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)/,regExForAutoFormatting:/(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*)(\s)$/},{...z,markdownFormatKind:"italic",regEx:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)/,
9
+ regExForAutoFormatting:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/},{...z,markdownFormatKind:"bold",regEx:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)/,regExForAutoFormatting:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/},{...z,markdownFormatKind:"bold",regEx:/(__)(\s*)([^__]*)(\s*)(__)/,regExForAutoFormatting:/(__)(\s*)([^__]*)(\s*)(__)(\s)$/},{...z,markdownFormatKind:"underline",regEx:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)/,regExForAutoFormatting:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)(\s)$/},{...z,markdownFormatKind:"strikethrough",
10
+ regEx:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)/,regExForAutoFormatting:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/},{...z,markdownFormatKind:"link",regEx:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))/,regExForAutoFormatting:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))(\s)$/}],D=[{...A,markdownFormatKind:"paragraphH1",regEx:/^(?:#)/,regExForAutoFormatting:/^(?:# )/},{...A,markdownFormatKind:"paragraphH2",regEx:/^(?:##)/,regExForAutoFormatting:/^(?:## )/},{...A,markdownFormatKind:"paragraphH2",regEx:/^(?:###)/,regExForAutoFormatting:/^(?:### )/},
11
+ {...A,markdownFormatKind:"paragraphBlockQuote",regEx:/^(?:>)/,regExForAutoFormatting:/^(?:> )/},{...A,markdownFormatKind:"paragraphUnorderedList",regEx:/^(?:- )/,regExForAutoFormatting:/^(?:- )/},{...A,markdownFormatKind:"paragraphUnorderedList",regEx:/^(?:\* )/,regExForAutoFormatting:/^(?:\* )/},{...A,markdownFormatKind:"paragraphOrderedList",regEx:/^(\d+)\.\s/,regExForAutoFormatting:/^(\d+)\.\s/},B,{...A,markdownFormatKind:"horizontalRule",regEx:/^(?:\*\*\*)$/,regExForAutoFormatting:/^(?:\*\*\* )/},
12
+ {...A,markdownFormatKind:"horizontalRule",regEx:/^(?:---)$/,regExForAutoFormatting:/^(?:--- )/},...C];function E(b,e,d,c){return{currentElementNode:null,editor:b,isAutoFormatting:e,isWithinCodeBlock:!1,joinedText:null,markdownCriteria:{markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null},patternMatchResults:{regExCaptureGroups:[]},textNodeWithOffset:d,triggerState:c}}
13
+ function F(b,e,d,c){const a={regExCaptureGroups:[]};c=b.match(c);if(null!==c&&0<c.length&&(!1===e||0===c.index)&&(!1===d||c.index+c[0].length===b.length)){b=c.length;e=c.index;for(d=0;d<b;d++){const f=c[d];a.regExCaptureGroups.push({offsetInParent:e,text:f});0<d&&(e+=f.length)}return a}return null}function I(b){b=b.textNodeWithOffset;null==b&&w(82);return b}
14
+ function J(b,e){var d=I(e);return null===d.node.getPreviousSibling()?(d=d.node.getTextContent(),F(d,!0,!1,e.isAutoFormatting?b.regExForAutoFormatting:b.regEx)):null}
15
+ function K(b,e){var d=I(b);const c=d.node.getParentOrThrow();0<b.patternMatchResults.regExCaptureGroups.length&&(d=d.node.spliceText(0,b.patternMatchResults.regExCaptureGroups[0].text.length,"",!0),""===d.getTextContent()&&(d.selectPrevious(),d.remove()));a:{var a=null;d=c.getChildren();const g=b.markdownCriteria,l=b.patternMatchResults;if(null!=g.markdownFormatKind)switch(g.markdownFormatKind){case "paragraphH1":a=u.$createHeadingNode("h1");a.append(...d);break;case "paragraphH2":a=u.$createHeadingNode("h2");
16
+ a.append(...d);break;case "paragraphH3":a=u.$createHeadingNode("h3");a.append(...d);break;case "paragraphBlockQuote":a=u.$createQuoteNode();a.append(...d);break;case "paragraphUnorderedList":a=p.$createListNode("ul");b=p.$createListItemNode();b.append(...d);a.append(b);break;case "paragraphOrderedList":a=parseInt(1<l.regExCaptureGroups.length?l.regExCaptureGroups[l.regExCaptureGroups.length-1].text:"1",10);a=p.$createListNode("ol",a);b=p.$createListItemNode();b.append(...d);a.append(b);break;case "paragraphCodeBlock":if(!1===
17
+ b.isAutoFormatting){if(0<b.patternMatchResults.regExCaptureGroups.length){b.isWithinCodeBlock=!0!==b.isWithinCodeBlock;b.currentElementNode=null;d={newNode:null,shouldDelete:!0};break a}if(b.isWithinCodeBlock){if(null==b.currentElementNode){a=k.$createCodeNode();a.append(...d);b.currentElementNode=a;d={newNode:a,shouldDelete:!1};break a}null!=b.currentElementNode&&(a=b.currentElementNode,b=q.$createLineBreakNode(),a.append(b),a.append(...d))}d={newNode:null,shouldDelete:!0};break a}null!=b.triggerState&&
18
+ b.triggerState.isCodeBlock?a=q.$createParagraphNode():(a=k.$createCodeNode(),b=3<=l.regExCaptureGroups.length?l.regExCaptureGroups[2].text:null,null!=b&&0<b.length&&a.setLanguage(b));a.append(...d);break;case "horizontalRule":null!=e&&(d=e(),c.insertBefore(d))}d={newNode:a,shouldDelete:!1}}const {newNode:f,shouldDelete:h}=d;h?c.remove():null!==f&&c.replace(f)}
19
+ function L(b,e,d,c,a){var f=a.patternMatchResults;const h=f.regExCaptureGroups;var g=h.length;if(b>=g||e>=g)return null;a=I(a).node.getParentOrThrow();g=f.regExCaptureGroups.length;2>g?f=0:(--g,f=f.regExCaptureGroups[g].offsetInParent+f.regExCaptureGroups[g].text.length);b=h[b];e=h[e];c=c?e.offsetInParent+e.text.length:e.offsetInParent;d=v.$findNodeWithOffsetFromJoinedText(d?b.offsetInParent+b.text.length:b.offsetInParent,f,1,a);c=v.$findNodeWithOffsetFromJoinedText(c,f,1,a);if(null==d||null==c)return null;
20
+ a=q.$createRangeSelection();a.anchor.set(d.node.getKey(),d.offset,"text");a.focus.set(c.node.getKey(),c.offset,"text");return a}function M(b,e,d){const c=d.patternMatchResults.regExCaptureGroups;d=L(b,e,!1,!0,d);if(null!=d&&(q.$setSelection(d),d=q.$getSelection(),null!=d&&q.$isRangeSelection(d)&&!1===d.isCollapsed())){d.removeText();d=0;const a=c.length;for(let f=b;f<a;f++){const h=c[f];f>b&&(h.offsetInParent-=d);f<=e&&(d+=h.text.length,h.text="")}}}
21
+ function P(b){var e=b.patternMatchResults.regExCaptureGroups.length;2>e||(--e,b=L(e,e,!0,!0,b),null!=b&&q.$setSelection(b))}
22
+ function Q(b,e,d){b.update(()=>{if(!0===e.markdownCriteria.requiresParagraphStart)K(e,d);else{var c=e.markdownCriteria;if(null!=c.markdownFormatKind){a:{var a=c.markdownFormatKind;switch(a){case "italic":case "bold":case "underline":case "strikethrough":a=[a];break a;case "bold_italic":a=["bold","italic"];break a}a=null}if(null!=a){if(c=a,7===e.patternMatchResults.regExCaptureGroups.length){M(5,5,e);M(1,1,e);a=e.patternMatchResults.regExCaptureGroups;3<a.length||w(65);if(0!==a[3].text.length&&(a=
23
+ L(3,3,!1,!0,e),null!=a&&(q.$setSelection(a),a=q.$getSelection(),q.$isRangeSelection(a))))for(var f=0;f<c.length;f++)a.formatText(c[f]);P(e)}}else if("link"===c.markdownFormatKind&&(c=e.patternMatchResults.regExCaptureGroups,7===c.length&&(f=c[2].text,c=c[4].text,0!==f.length&&0!==c.length))){M(1,5,e);a=e.patternMatchResults.regExCaptureGroups;if(!(1>=a.length)){f={offsetInParent:a[1].offsetInParent,text:f};var h=L(1,1,!1,!1,e);if(null!=h&&(q.$setSelection(h),h=q.$getSelection(),null!=h&&q.$isRangeSelection(h)&&
24
+ h.isCollapsed())){h.insertText(f.text);a.splice(1,0,f);f=f.text.length;h=a.length;for(let g=2;g<h;g++)a[g].offsetInParent+=f}}a=L(1,1,!1,!0,e);null!=a&&(q.$setSelection(a),e.editor.dispatchCommand(t.TOGGLE_LINK_COMMAND,c),P(e))}}}},{tag:"history-push"})}
25
+ function R(b,e){let d=null;b.getEditorState().read(()=>{var c=q.$getSelection();if(q.$isRangeSelection(c)){var a=c.anchor.getNode();c=q.$isTextNode(a)?{node:a,offset:c.anchor.offset}:null}else c=null;if(null!==c){c=E(b,!0,c,e);a:{a=!1===e.isParentAListItemNode?D:C;const g=c.triggerState,l=a.length;for(let n=0;n<l;n++){const r=a[n];if(null!=g&&!1===g.isCodeBlock||"paragraphCodeBlock"===r.markdownFormatKind){var f=r,h=c;if(!0===f.requiresParagraphStart)f=J(f,h);else{if(null==h.joinedText){const m=I(h).node.getParentOrThrow();
26
+ q.$isElementNode(m)?null==h.joinedText&&(h.joinedText=v.$joinTextNodesInElementNode(m,"\u0004",I(h))):w(52,m.__key)}f=F(h.joinedText,!1,!0,f.regExForAutoFormatting)}if(null!=f){a={markdownCriteria:r,patternMatchResults:f};break a}}}a={markdownCriteria:null,patternMatchResults:null}}null!==a.markdownCriteria&&null!==a.patternMatchResults&&(d=c,d.markdownCriteria=a.markdownCriteria,d.patternMatchResults=a.patternMatchResults)}});return d}
27
+ function S(b){let e=null;b.read(()=>{const d=q.$getSelection();if(q.$isRangeSelection(d)&&d.isCollapsed()){var c=d.anchor.getNode(),a=c.getParent(),f=p.$isListItemNode(a);e={anchorOffset:d.anchor.offset,hasParentNode:null!==a,isCodeBlock:k.$isCodeNode(c),isParentAListItemNode:f,isSelectionCollapsed:!0,isSimpleText:q.$isTextNode(c)&&c.isSimpleText(),nodeKey:c.getKey(),textContent:c.getTextContent()}}});return e}
28
+ exports.$convertFromMarkdownString=function(b,e,d){if(b.length){var c=[];b=b.split("\n");var a=b.length;for(var f=0;f<a;f++)0<b[f].length?c.push(q.$createParagraphNode().append(q.$createTextNode(b[f]))):c.push(q.$createParagraphNode());c.length?(b=q.$getRoot(),b.clear(),b.append(...c),c=b):c=null}else c=null;if(null!=c)for(e=E(e,!1,null,null),c=q.$getRoot(),b=!1,a=0;!b;){b=!0;f=c.getChildren();const r=f.length;for(let m=a;m<r;m++){var h=f[m];if(q.$isElementNode(h)){var g=e,l=h;h=d;var n=l.getTextContent();
29
+ if(q.$isParagraphNode(l)&&l.getChildren().length){const x=l.getFirstChild(),N=q.$isTextNode(x);if(!0===g.isWithinCodeBlock)null!=x&&N||w(80),g.textNodeWithOffset={node:x,offset:0},l=F(n,!0,!1,g.isAutoFormatting?B.regExForAutoFormatting:B.regEx),null!=l&&(g.patternMatchResults=l),g.markdownCriteria=B,K(g,h);else{n=D.length;for(let G=0;G<n;G++){const H=D[G];if(!0===H.requiresParagraphStart){null!=x&&N||w(80);g.textNodeWithOffset={node:x,offset:0};g.joinedText=l.getTextContent();const O=J(H,g);if(null!=
30
+ O){g.markdownCriteria=H;g.patternMatchResults=O;K(g,h);break}}}}}}g=e;g.joinedText="";g.markdownCriteria={markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null};g.patternMatchResults={regExCaptureGroups:[]};g.triggerState=null;g.textNodeWithOffset=null;if(c.getChildren().length!==r){a=m;b=!1;break}}}};
31
+ exports.registerMarkdownShortcuts=function(b,e){let d=null;return b.registerUpdateListener(({tags:c})=>{if(!1===c.has("historic")){c=S(b.getEditorState());if(null==c)var a=null;else a:{a=c;var f=d;if(null==a||null==f)a=null;else{var h=y.length;for(let g=0;g<h;g++){const l=y[g].triggerString,n=l.length,r=a.textContent.length,m=a.anchorOffset-n;if(!1===(!0===a.hasParentNode&&a.isSimpleText&&a.isSelectionCollapsed&&a.anchorOffset!==f.anchorOffset&&0<=m&&m+n<=r&&a.textContent.substr(m,n)===l&&a.textContent!==
32
+ f.textContent)){a=null;break a}}a=R(b,a)}}null!=a&&Q(b,a,e);d=c}else d=null})};
package/package.json CHANGED
@@ -8,18 +8,18 @@
8
8
  "markdown"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.1.18",
11
+ "version": "0.1.21",
12
12
  "main": "LexicalMarkdown.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.1.18"
14
+ "lexical": "0.1.21"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.1.18",
18
- "@lexical/code": "0.1.18",
19
- "@lexical/text": "0.1.18",
20
- "@lexical/rich-text": "0.1.18",
21
- "@lexical/list": "0.1.18",
22
- "@lexical/link": "0.1.18"
17
+ "@lexical/utils": "0.1.21",
18
+ "@lexical/code": "0.1.21",
19
+ "@lexical/text": "0.1.21",
20
+ "@lexical/rich-text": "0.1.21",
21
+ "@lexical/list": "0.1.21",
22
+ "@lexical/link": "0.1.21"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",