@lexical/markdown 0.1.21 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LexicalMarkdown.dev.js +162 -85
- package/LexicalMarkdown.prod.js +30 -26
- package/package.json +8 -8
package/LexicalMarkdown.dev.js
CHANGED
|
@@ -41,7 +41,7 @@ const SEPARATOR_LENGTH = SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES.length;
|
|
|
41
41
|
const spaceTrigger = {
|
|
42
42
|
triggerKind: 'space_trigger',
|
|
43
43
|
triggerString: '\u0020'
|
|
44
|
-
}; //
|
|
44
|
+
}; // Future todo: add support for ``` + carriage return either inside or not inside code block. Should toggle between.
|
|
45
45
|
// const codeBlockTrigger : AutoFormatTrigger = {
|
|
46
46
|
// triggerKind: 'codeBlock_trigger',
|
|
47
47
|
// triggerString: '```', // + new paragraph element or new code block element.
|
|
@@ -49,7 +49,7 @@ const spaceTrigger = {
|
|
|
49
49
|
|
|
50
50
|
const triggers = [spaceTrigger
|
|
51
51
|
/*, codeBlockTrigger*/
|
|
52
|
-
]; // Todo: speed up performance by having non-capture group variations of the regex.
|
|
52
|
+
]; // Future Todo: speed up performance by having non-capture group variations of the regex.
|
|
53
53
|
|
|
54
54
|
const autoFormatBase = {
|
|
55
55
|
markdownFormatKind: null,
|
|
@@ -62,33 +62,33 @@ const paragraphStartBase = { ...autoFormatBase,
|
|
|
62
62
|
};
|
|
63
63
|
const markdownHeader1 = { ...paragraphStartBase,
|
|
64
64
|
markdownFormatKind: 'paragraphH1',
|
|
65
|
-
regEx: /^(?:#)/,
|
|
65
|
+
regEx: /^(?:# )/,
|
|
66
66
|
regExForAutoFormatting: /^(?:# )/
|
|
67
67
|
};
|
|
68
68
|
const markdownHeader2 = { ...paragraphStartBase,
|
|
69
69
|
markdownFormatKind: 'paragraphH2',
|
|
70
|
-
regEx: /^(?:##)/,
|
|
70
|
+
regEx: /^(?:## )/,
|
|
71
71
|
regExForAutoFormatting: /^(?:## )/
|
|
72
72
|
};
|
|
73
73
|
const markdownHeader3 = { ...paragraphStartBase,
|
|
74
74
|
markdownFormatKind: 'paragraphH2',
|
|
75
|
-
regEx: /^(?:###)/,
|
|
75
|
+
regEx: /^(?:### )/,
|
|
76
76
|
regExForAutoFormatting: /^(?:### )/
|
|
77
77
|
};
|
|
78
78
|
const markdownBlockQuote = { ...paragraphStartBase,
|
|
79
79
|
markdownFormatKind: 'paragraphBlockQuote',
|
|
80
|
-
regEx: /^(?:>)/,
|
|
80
|
+
regEx: /^(?:> )/,
|
|
81
81
|
regExForAutoFormatting: /^(?:> )/
|
|
82
82
|
};
|
|
83
83
|
const markdownUnorderedListDash = { ...paragraphStartBase,
|
|
84
84
|
markdownFormatKind: 'paragraphUnorderedList',
|
|
85
|
-
regEx: /^(?:- )/,
|
|
86
|
-
regExForAutoFormatting: /^(?:- )/
|
|
85
|
+
regEx: /^(\s{0,10})(?:- )/,
|
|
86
|
+
regExForAutoFormatting: /^(\s{0,10})(?:- )/
|
|
87
87
|
};
|
|
88
88
|
const markdownUnorderedListAsterisk = { ...paragraphStartBase,
|
|
89
89
|
markdownFormatKind: 'paragraphUnorderedList',
|
|
90
|
-
regEx: /^(?:\* )/,
|
|
91
|
-
regExForAutoFormatting: /^(?:\* )/
|
|
90
|
+
regEx: /^(\s{0,10})(?:\* )/,
|
|
91
|
+
regExForAutoFormatting: /^(\s{0,10})(?:\* )/
|
|
92
92
|
};
|
|
93
93
|
const markdownCodeBlock = { ...paragraphStartBase,
|
|
94
94
|
markdownFormatKind: 'paragraphCodeBlock',
|
|
@@ -97,8 +97,8 @@ const markdownCodeBlock = { ...paragraphStartBase,
|
|
|
97
97
|
};
|
|
98
98
|
const markdownOrderedList = { ...paragraphStartBase,
|
|
99
99
|
markdownFormatKind: 'paragraphOrderedList',
|
|
100
|
-
regEx: /^(\d+)\.\s/,
|
|
101
|
-
regExForAutoFormatting: /^(\d+)\.\s/
|
|
100
|
+
regEx: /^(\s{0,10})(\d+)\.\s/,
|
|
101
|
+
regExForAutoFormatting: /^(\s{0,10})(\d+)\.\s/
|
|
102
102
|
};
|
|
103
103
|
const markdownHorizontalRule = { ...paragraphStartBase,
|
|
104
104
|
markdownFormatKind: 'horizontalRule',
|
|
@@ -110,25 +110,31 @@ const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
|
|
|
110
110
|
regEx: /^(?:---)$/,
|
|
111
111
|
regExForAutoFormatting: /^(?:--- )/
|
|
112
112
|
};
|
|
113
|
+
const markdownInlineCode = { ...autoFormatBase,
|
|
114
|
+
markdownFormatKind: 'code',
|
|
115
|
+
regEx: /(`)([^`]*)(`)/,
|
|
116
|
+
regExForAutoFormatting: /(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/
|
|
117
|
+
};
|
|
113
118
|
const markdownItalic = { ...autoFormatBase,
|
|
114
119
|
markdownFormatKind: 'italic',
|
|
115
|
-
regEx: /(\*)(
|
|
120
|
+
regEx: /(\*)([^\*]*)(\*)/,
|
|
116
121
|
regExForAutoFormatting: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/
|
|
117
122
|
};
|
|
118
123
|
const markdownBold = { ...autoFormatBase,
|
|
119
124
|
markdownFormatKind: 'bold',
|
|
120
|
-
regEx: /(\*\*)(
|
|
125
|
+
regEx: /(\*\*)([^\*\*]*)(\*\*)/,
|
|
121
126
|
regExForAutoFormatting: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/
|
|
122
127
|
};
|
|
123
|
-
const
|
|
128
|
+
const markdownBold2 = { ...autoFormatBase,
|
|
124
129
|
markdownFormatKind: 'bold',
|
|
125
130
|
regEx: /(__)(\s*)([^__]*)(\s*)(__)/,
|
|
126
131
|
regExForAutoFormatting: /(__)(\s*)([^__]*)(\s*)(__)(\s)$/
|
|
127
132
|
};
|
|
128
|
-
const
|
|
129
|
-
markdownFormatKind: '
|
|
130
|
-
regEx: /(
|
|
131
|
-
regExForAutoFormatting: /(
|
|
133
|
+
const markdownItalic2 = { ...autoFormatBase,
|
|
134
|
+
markdownFormatKind: 'italic',
|
|
135
|
+
regEx: /(_)([^_]*)(_)/,
|
|
136
|
+
regExForAutoFormatting: /(_)()([^_]*)()(_)(\s)$/ // Maintain 7 groups.
|
|
137
|
+
|
|
132
138
|
}; // Markdown does not support underline, but we can allow folks to use
|
|
133
139
|
// the HTML tags for underline.
|
|
134
140
|
|
|
@@ -139,15 +145,39 @@ const fakeMarkdownUnderline = { ...autoFormatBase,
|
|
|
139
145
|
};
|
|
140
146
|
const markdownStrikethrough = { ...autoFormatBase,
|
|
141
147
|
markdownFormatKind: 'strikethrough',
|
|
142
|
-
regEx: /(~~)(
|
|
148
|
+
regEx: /(~~)([^~~]*)(~~)/,
|
|
143
149
|
regExForAutoFormatting: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/
|
|
144
150
|
};
|
|
151
|
+
const markdownStrikethroughItalicBold = { ...autoFormatBase,
|
|
152
|
+
markdownFormatKind: 'strikethrough_italic_bold',
|
|
153
|
+
regEx: /(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)/,
|
|
154
|
+
regExForAutoFormatting: /(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)(\s)$/
|
|
155
|
+
};
|
|
156
|
+
const markdownItalicbold = { ...autoFormatBase,
|
|
157
|
+
markdownFormatKind: 'italic_bold',
|
|
158
|
+
regEx: /(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)/,
|
|
159
|
+
regExForAutoFormatting: /(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)(\s)$/
|
|
160
|
+
};
|
|
161
|
+
const markdownStrikethroughItalic = { ...autoFormatBase,
|
|
162
|
+
markdownFormatKind: 'strikethrough_italic',
|
|
163
|
+
regEx: /(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)/,
|
|
164
|
+
regExForAutoFormatting: /(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)(\s)$/
|
|
165
|
+
};
|
|
166
|
+
const markdownStrikethroughBold = { ...autoFormatBase,
|
|
167
|
+
markdownFormatKind: 'strikethrough_bold',
|
|
168
|
+
regEx: /(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)/,
|
|
169
|
+
regExForAutoFormatting: /(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)(\s)$/
|
|
170
|
+
};
|
|
145
171
|
const markdownLink = { ...autoFormatBase,
|
|
146
172
|
markdownFormatKind: 'link',
|
|
147
173
|
regEx: /(\[)(.+)(\]\()([^ ]+)(?: \"(?:.+)\")?(\))/,
|
|
148
174
|
regExForAutoFormatting: /(\[)(.+)(\]\()([^ ]+)(?: \"(?:.+)\")?(\))(\s)$/
|
|
149
175
|
};
|
|
150
|
-
const allMarkdownCriteriaForTextNodes = [
|
|
176
|
+
const allMarkdownCriteriaForTextNodes = [// Place the combination formats ahead of the individual formats.
|
|
177
|
+
// Combos
|
|
178
|
+
markdownStrikethroughItalicBold, markdownItalicbold, markdownStrikethroughItalic, markdownStrikethroughBold, // Individuals
|
|
179
|
+
markdownInlineCode, markdownItalic, markdownBold, markdownBold2, markdownItalic2, // Must appear after markdownBold2.
|
|
180
|
+
fakeMarkdownUnderline, markdownStrikethrough, markdownLink];
|
|
151
181
|
const allMarkdownCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, markdownHorizontalRule, markdownHorizontalRuleUsingDashes, ...allMarkdownCriteriaForTextNodes];
|
|
152
182
|
function getInitialScanningContext(editor, isAutoFormatting, textNodeWithOffset, triggerState) {
|
|
153
183
|
return {
|
|
@@ -226,6 +256,9 @@ function getPatternMatchResultsWithRegEx(textToSearch, matchMustAppearAtStartOfS
|
|
|
226
256
|
return null;
|
|
227
257
|
}
|
|
228
258
|
|
|
259
|
+
function hasPatternMatchResults(scanningContext) {
|
|
260
|
+
return scanningContext.patternMatchResults.regExCaptureGroups.length > 0;
|
|
261
|
+
}
|
|
229
262
|
function getTextNodeWithOffsetOrThrow(scanningContext) {
|
|
230
263
|
const textNodeWithOffset = scanningContext.textNodeWithOffset;
|
|
231
264
|
|
|
@@ -317,27 +350,24 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
|
|
|
317
350
|
|
|
318
351
|
case 'paragraphUnorderedList':
|
|
319
352
|
{
|
|
320
|
-
|
|
321
|
-
const listItem = list.$createListItemNode();
|
|
322
|
-
listItem.append(...children);
|
|
323
|
-
newNode.append(listItem);
|
|
353
|
+
createListOrMergeWithPrevious(element, children, patternMatchResults, 'ul');
|
|
324
354
|
return {
|
|
325
|
-
newNode,
|
|
326
|
-
shouldDelete
|
|
355
|
+
newNode: null,
|
|
356
|
+
shouldDelete: false
|
|
327
357
|
};
|
|
328
358
|
}
|
|
329
359
|
|
|
330
360
|
case 'paragraphOrderedList':
|
|
331
361
|
{
|
|
332
|
-
const startAsString = patternMatchResults.regExCaptureGroups.length > 1 ? patternMatchResults.regExCaptureGroups[patternMatchResults.regExCaptureGroups.length - 1].text : '1';
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
362
|
+
const startAsString = patternMatchResults.regExCaptureGroups.length > 1 ? patternMatchResults.regExCaptureGroups[patternMatchResults.regExCaptureGroups.length - 1].text : '1'; // For conversion, don't use start number.
|
|
363
|
+
// For short-cuts aka autoFormatting, use start number.
|
|
364
|
+
// Later, this should be surface dependent and externalized.
|
|
365
|
+
|
|
366
|
+
const start = scanningContext.isAutoFormatting ? parseInt(startAsString, 10) : undefined;
|
|
367
|
+
createListOrMergeWithPrevious(element, children, patternMatchResults, 'ol', start);
|
|
338
368
|
return {
|
|
339
|
-
newNode,
|
|
340
|
-
shouldDelete
|
|
369
|
+
newNode: null,
|
|
370
|
+
shouldDelete: false
|
|
341
371
|
};
|
|
342
372
|
}
|
|
343
373
|
|
|
@@ -345,7 +375,7 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
|
|
|
345
375
|
{
|
|
346
376
|
// Toggle code and paragraph nodes.
|
|
347
377
|
if (scanningContext.isAutoFormatting === false) {
|
|
348
|
-
const shouldToggle = scanningContext
|
|
378
|
+
const shouldToggle = hasPatternMatchResults(scanningContext);
|
|
349
379
|
|
|
350
380
|
if (shouldToggle) {
|
|
351
381
|
scanningContext.isWithinCodeBlock = scanningContext.isWithinCodeBlock !== true; // When toggling, always clear the code block element node.
|
|
@@ -374,6 +404,11 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
|
|
|
374
404
|
const codeBlockNode = scanningContext.currentElementNode;
|
|
375
405
|
const lineBreakNode = lexical.$createLineBreakNode();
|
|
376
406
|
codeBlockNode.append(lineBreakNode);
|
|
407
|
+
|
|
408
|
+
if (children.length) {
|
|
409
|
+
codeBlockNode.append(lineBreakNode);
|
|
410
|
+
}
|
|
411
|
+
|
|
377
412
|
codeBlockNode.append(...children);
|
|
378
413
|
}
|
|
379
414
|
}
|
|
@@ -421,18 +456,42 @@ function getNewNodeForCriteria(scanningContext, element, createHorizontalRuleNod
|
|
|
421
456
|
};
|
|
422
457
|
}
|
|
423
458
|
|
|
424
|
-
function
|
|
425
|
-
const
|
|
426
|
-
const
|
|
459
|
+
function createListOrMergeWithPrevious(element, children, patternMatchResults, tag, start) {
|
|
460
|
+
const listItem = list.$createListItemNode();
|
|
461
|
+
const indentMatch = patternMatchResults.regExCaptureGroups[0].text.match(/^\s*/);
|
|
462
|
+
const indent = indentMatch ? Math.floor(indentMatch[0].length / 4) : 0;
|
|
463
|
+
listItem.append(...children); // Checking if previous element is a list, and if so append
|
|
464
|
+
// new list item inside instead of creating new list
|
|
427
465
|
|
|
428
|
-
|
|
429
|
-
const text = scanningContext.patternMatchResults.regExCaptureGroups[0].text; // Remove the text which we matched.
|
|
466
|
+
const prevElement = element.getPreviousSibling();
|
|
430
467
|
|
|
431
|
-
|
|
468
|
+
if (list.$isListNode(prevElement) && prevElement.getTag() === tag) {
|
|
469
|
+
prevElement.append(listItem);
|
|
470
|
+
element.remove();
|
|
471
|
+
} else {
|
|
472
|
+
const list$1 = list.$createListNode(tag, start);
|
|
473
|
+
list$1.append(listItem);
|
|
474
|
+
element.replace(list$1);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (indent) {
|
|
478
|
+
listItem.setIndent(indent);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
432
481
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
482
|
+
function transformTextNodeForElementNode(elementNode, scanningContext, createHorizontalRuleNode) {
|
|
483
|
+
if (scanningContext.textNodeWithOffset != null) {
|
|
484
|
+
const textNodeWithOffset = getTextNodeWithOffsetOrThrow(scanningContext);
|
|
485
|
+
|
|
486
|
+
if (hasPatternMatchResults(scanningContext)) {
|
|
487
|
+
const text = scanningContext.patternMatchResults.regExCaptureGroups[0].text; // Remove the text which we matched.
|
|
488
|
+
|
|
489
|
+
const textNode = textNodeWithOffset.node.spliceText(0, text.length, '', true);
|
|
490
|
+
|
|
491
|
+
if (textNode.getTextContent() === '') {
|
|
492
|
+
textNode.selectPrevious();
|
|
493
|
+
textNode.remove();
|
|
494
|
+
}
|
|
436
495
|
}
|
|
437
496
|
} // Transform the current element kind to the new element kind.
|
|
438
497
|
|
|
@@ -440,12 +499,12 @@ function transformTextNodeForParagraphs(scanningContext, createHorizontalRuleNod
|
|
|
440
499
|
const {
|
|
441
500
|
newNode,
|
|
442
501
|
shouldDelete
|
|
443
|
-
} = getNewNodeForCriteria(scanningContext,
|
|
502
|
+
} = getNewNodeForCriteria(scanningContext, elementNode, createHorizontalRuleNode);
|
|
444
503
|
|
|
445
504
|
if (shouldDelete) {
|
|
446
|
-
|
|
505
|
+
elementNode.remove();
|
|
447
506
|
} else if (newNode !== null) {
|
|
448
|
-
|
|
507
|
+
elementNode.replace(newNode);
|
|
449
508
|
}
|
|
450
509
|
}
|
|
451
510
|
function transformTextNodeForText(scanningContext) {
|
|
@@ -557,11 +616,27 @@ function getTextFormatType(markdownFormatKind) {
|
|
|
557
616
|
case 'bold':
|
|
558
617
|
case 'underline':
|
|
559
618
|
case 'strikethrough':
|
|
619
|
+
case 'code':
|
|
560
620
|
return [markdownFormatKind];
|
|
561
621
|
|
|
562
|
-
case '
|
|
622
|
+
case 'strikethrough_italic_bold':
|
|
563
623
|
{
|
|
564
|
-
return ['
|
|
624
|
+
return ['strikethrough', 'italic', 'bold'];
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
case 'italic_bold':
|
|
628
|
+
{
|
|
629
|
+
return ['italic', 'bold'];
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
case 'strikethrough_italic':
|
|
633
|
+
{
|
|
634
|
+
return ['strikethrough', 'italic'];
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
case 'strikethrough_bold':
|
|
638
|
+
{
|
|
639
|
+
return ['strikethrough', 'bold'];
|
|
565
640
|
}
|
|
566
641
|
}
|
|
567
642
|
|
|
@@ -728,7 +803,8 @@ function getAllMarkdownCriteria() {
|
|
|
728
803
|
}
|
|
729
804
|
function transformTextNodeForMarkdownCriteria(scanningContext, createHorizontalRuleNode) {
|
|
730
805
|
if (scanningContext.markdownCriteria.requiresParagraphStart === true) {
|
|
731
|
-
|
|
806
|
+
const elementNode = getTextNodeWithOffsetOrThrow(scanningContext).node.getParentOrThrow();
|
|
807
|
+
transformTextNodeForElementNode(elementNode, scanningContext, createHorizontalRuleNode);
|
|
732
808
|
} else {
|
|
733
809
|
transformTextNodeForText(scanningContext);
|
|
734
810
|
}
|
|
@@ -908,58 +984,59 @@ function convertStringToLexical(text, editor) {
|
|
|
908
984
|
function convertElementNodeContainingMarkdown(scanningContext, elementNode, createHorizontalRuleNode) {
|
|
909
985
|
const textContent = elementNode.getTextContent(); // Handle paragraph nodes below.
|
|
910
986
|
|
|
911
|
-
if (lexical.$isParagraphNode(elementNode)
|
|
987
|
+
if (lexical.$isParagraphNode(elementNode)) {
|
|
912
988
|
const paragraphNode = elementNode;
|
|
913
989
|
const firstChild = paragraphNode.getFirstChild();
|
|
914
990
|
const firstChildIsTextNode = lexical.$isTextNode(firstChild); // Handle conversion to code block.
|
|
915
991
|
|
|
916
992
|
if (scanningContext.isWithinCodeBlock === true) {
|
|
917
|
-
if (
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
};
|
|
925
|
-
const patternMatchResults = getPatternMatchResultsForCodeBlock(scanningContext, textContent);
|
|
993
|
+
if (firstChild != null && firstChildIsTextNode) {
|
|
994
|
+
// Test if we encounter ending code block.
|
|
995
|
+
scanningContext.textNodeWithOffset = {
|
|
996
|
+
node: firstChild,
|
|
997
|
+
offset: 0
|
|
998
|
+
};
|
|
999
|
+
const patternMatchResults = getPatternMatchResultsForCodeBlock(scanningContext, textContent);
|
|
926
1000
|
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
1001
|
+
if (patternMatchResults != null) {
|
|
1002
|
+
// Toggle transform to or from code block.
|
|
1003
|
+
scanningContext.patternMatchResults = patternMatchResults;
|
|
1004
|
+
}
|
|
930
1005
|
}
|
|
931
1006
|
|
|
932
1007
|
scanningContext.markdownCriteria = getCodeBlockCriteria(); // Perform text transformation here.
|
|
933
1008
|
|
|
934
|
-
|
|
1009
|
+
transformTextNodeForElementNode(elementNode, scanningContext, createHorizontalRuleNode);
|
|
935
1010
|
return;
|
|
936
1011
|
}
|
|
937
1012
|
|
|
938
|
-
|
|
939
|
-
|
|
1013
|
+
if (elementNode.getChildren().length) {
|
|
1014
|
+
const allCriteria = getAllMarkdownCriteria();
|
|
1015
|
+
const count = allCriteria.length;
|
|
940
1016
|
|
|
941
|
-
|
|
942
|
-
|
|
1017
|
+
for (let i = 0; i < count; i++) {
|
|
1018
|
+
const criteria = allCriteria[i];
|
|
943
1019
|
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1020
|
+
if (criteria.requiresParagraphStart === true) {
|
|
1021
|
+
if (!(firstChild != null && firstChildIsTextNode)) {
|
|
1022
|
+
throw Error(`Expect paragraph containing only text nodes.`);
|
|
1023
|
+
}
|
|
948
1024
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1025
|
+
scanningContext.textNodeWithOffset = {
|
|
1026
|
+
node: firstChild,
|
|
1027
|
+
offset: 0
|
|
1028
|
+
};
|
|
1029
|
+
scanningContext.joinedText = paragraphNode.getTextContent();
|
|
1030
|
+
const patternMatchResults = getPatternMatchResultsForParagraphs(criteria, scanningContext);
|
|
955
1031
|
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1032
|
+
if (patternMatchResults != null) {
|
|
1033
|
+
// Lazy fill-in the particular format criteria and any matching result information.
|
|
1034
|
+
scanningContext.markdownCriteria = criteria;
|
|
1035
|
+
scanningContext.patternMatchResults = patternMatchResults; // Perform text transformation here.
|
|
960
1036
|
|
|
961
|
-
|
|
962
|
-
|
|
1037
|
+
transformTextNodeForElementNode(elementNode, scanningContext, createHorizontalRuleNode);
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
963
1040
|
}
|
|
964
1041
|
}
|
|
965
1042
|
}
|
package/LexicalMarkdown.prod.js
CHANGED
|
@@ -4,29 +4,33 @@
|
|
|
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 k=require("@lexical/code"),
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
regEx:/(
|
|
11
|
-
|
|
12
|
-
{...A,markdownFormatKind:"
|
|
13
|
-
|
|
14
|
-
function
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
b
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
a=
|
|
21
|
-
function
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
7
|
+
var k=require("@lexical/code"),m=require("@lexical/list"),p=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 x=[{triggerKind:"space_trigger",triggerString:" "}],y={markdownFormatKind:null,regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:!1},A={...y,requiresParagraphStart:!0},B={...A,markdownFormatKind:"paragraphCodeBlock",regEx:/^(```)$/,regExForAutoFormatting:/^(```)([a-z]*)( )/},C=[{...y,markdownFormatKind:"strikethrough_italic_bold",regEx:/(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)/,regExForAutoFormatting:/(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)(\s)$/},
|
|
9
|
+
{...y,markdownFormatKind:"italic_bold",regEx:/(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)/,regExForAutoFormatting:/(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)(\s)$/},{...y,markdownFormatKind:"strikethrough_italic",regEx:/(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)/,regExForAutoFormatting:/(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)(\s)$/},{...y,markdownFormatKind:"strikethrough_bold",regEx:/(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)/,regExForAutoFormatting:/(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)(\s)$/},
|
|
10
|
+
{...y,markdownFormatKind:"code",regEx:/(`)([^`]*)(`)/,regExForAutoFormatting:/(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/},{...y,markdownFormatKind:"italic",regEx:/(\*)([^\*]*)(\*)/,regExForAutoFormatting:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/},{...y,markdownFormatKind:"bold",regEx:/(\*\*)([^\*\*]*)(\*\*)/,regExForAutoFormatting:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/},{...y,markdownFormatKind:"bold",regEx:/(__)(\s*)([^__]*)(\s*)(__)/,regExForAutoFormatting:/(__)(\s*)([^__]*)(\s*)(__)(\s)$/},{...y,markdownFormatKind:"italic",
|
|
11
|
+
regEx:/(_)([^_]*)(_)/,regExForAutoFormatting:/(_)()([^_]*)()(_)(\s)$/},{...y,markdownFormatKind:"underline",regEx:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)/,regExForAutoFormatting:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)(\s)$/},{...y,markdownFormatKind:"strikethrough",regEx:/(~~)([^~~]*)(~~)/,regExForAutoFormatting:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/},{...y,markdownFormatKind:"link",regEx:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))/,regExForAutoFormatting:/(\[)(.+)(\]\()([^ ]+)(?: "(?:.+)")?(\))(\s)$/}],D=[{...A,
|
|
12
|
+
markdownFormatKind:"paragraphH1",regEx:/^(?:# )/,regExForAutoFormatting:/^(?:# )/},{...A,markdownFormatKind:"paragraphH2",regEx:/^(?:## )/,regExForAutoFormatting:/^(?:## )/},{...A,markdownFormatKind:"paragraphH2",regEx:/^(?:### )/,regExForAutoFormatting:/^(?:### )/},{...A,markdownFormatKind:"paragraphBlockQuote",regEx:/^(?:> )/,regExForAutoFormatting:/^(?:> )/},{...A,markdownFormatKind:"paragraphUnorderedList",regEx:/^(\s{0,10})(?:- )/,regExForAutoFormatting:/^(\s{0,10})(?:- )/},{...A,markdownFormatKind:"paragraphUnorderedList",
|
|
13
|
+
regEx:/^(\s{0,10})(?:\* )/,regExForAutoFormatting:/^(\s{0,10})(?:\* )/},{...A,markdownFormatKind:"paragraphOrderedList",regEx:/^(\s{0,10})(\d+)\.\s/,regExForAutoFormatting:/^(\s{0,10})(\d+)\.\s/},B,{...A,markdownFormatKind:"horizontalRule",regEx:/^(?:\*\*\*)$/,regExForAutoFormatting:/^(?:\*\*\* )/},{...A,markdownFormatKind:"horizontalRule",regEx:/^(?:---)$/,regExForAutoFormatting:/^(?:--- )/},...C];
|
|
14
|
+
function E(b,d,e,a){return{currentElementNode:null,editor:b,isAutoFormatting:d,isWithinCodeBlock:!1,joinedText:null,markdownCriteria:{markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null},patternMatchResults:{regExCaptureGroups:[]},textNodeWithOffset:e,triggerState:a}}
|
|
15
|
+
function F(b,d,e,a){const c={regExCaptureGroups:[]};a=b.match(a);if(null!==a&&0<a.length&&(!1===d||0===a.index)&&(!1===e||a.index+a[0].length===b.length)){b=a.length;d=a.index;for(e=0;e<b;e++){const f=a[e];c.regExCaptureGroups.push({offsetInParent:d,text:f});0<e&&(d+=f.length)}return c}return null}function I(b){b=b.textNodeWithOffset;null==b&&w(82);return b}
|
|
16
|
+
function J(b,d){var e=I(d);return null===e.node.getPreviousSibling()?(e=e.node.getTextContent(),F(e,!0,!1,d.isAutoFormatting?b.regExForAutoFormatting:b.regEx)):null}
|
|
17
|
+
function K(b,d,e){var a=null,c=d.getChildren();const f=b.markdownCriteria,g=b.patternMatchResults;if(null!=f.markdownFormatKind)switch(f.markdownFormatKind){case "paragraphH1":a=u.$createHeadingNode("h1");a.append(...c);break;case "paragraphH2":a=u.$createHeadingNode("h2");a.append(...c);break;case "paragraphH3":a=u.$createHeadingNode("h3");a.append(...c);break;case "paragraphBlockQuote":a=u.$createQuoteNode();a.append(...c);break;case "paragraphUnorderedList":return L(d,c,g,"ul"),{newNode:null,shouldDelete:!1};
|
|
18
|
+
case "paragraphOrderedList":return a=1<g.regExCaptureGroups.length?g.regExCaptureGroups[g.regExCaptureGroups.length-1].text:"1",b=b.isAutoFormatting?parseInt(a,10):void 0,L(d,c,g,"ol",b),{newNode:null,shouldDelete:!1};case "paragraphCodeBlock":if(!1===b.isAutoFormatting){if(0<b.patternMatchResults.regExCaptureGroups.length)return b.isWithinCodeBlock=!0!==b.isWithinCodeBlock,b.currentElementNode=null,{newNode:null,shouldDelete:!0};if(b.isWithinCodeBlock){if(null==b.currentElementNode)return d=k.$createCodeNode(),
|
|
19
|
+
d.append(...c),b.currentElementNode=d,{newNode:d,shouldDelete:!1};null!=b.currentElementNode&&(d=b.currentElementNode,b=p.$createLineBreakNode(),d.append(b),c.length&&d.append(b),d.append(...c))}return{newNode:null,shouldDelete:!0}}null!=b.triggerState&&b.triggerState.isCodeBlock?a=p.$createParagraphNode():(a=k.$createCodeNode(),d=3<=g.regExCaptureGroups.length?g.regExCaptureGroups[2].text:null,null!=d&&0<d.length&&a.setLanguage(d));a.append(...c);break;case "horizontalRule":null!=e&&(c=e(),d.insertBefore(c))}return{newNode:a,
|
|
20
|
+
shouldDelete:!1}}function L(b,d,e,a,c){const f=m.$createListItemNode();e=(e=e.regExCaptureGroups[0].text.match(/^\s*/))?Math.floor(e[0].length/4):0;f.append(...d);d=b.getPreviousSibling();m.$isListNode(d)&&d.getTag()===a?(d.append(f),b.remove()):(a=m.$createListNode(a,c),a.append(f),b.replace(a));e&&f.setIndent(e)}
|
|
21
|
+
function M(b,d,e){if(null!=d.textNodeWithOffset){var a=I(d);0<d.patternMatchResults.regExCaptureGroups.length&&(a=a.node.spliceText(0,d.patternMatchResults.regExCaptureGroups[0].text.length,"",!0),""===a.getTextContent()&&(a.selectPrevious(),a.remove()))}const {newNode:c,shouldDelete:f}=K(d,b,e);f?b.remove():null!==c&&b.replace(c)}
|
|
22
|
+
function N(b){switch(b){case "italic":case "bold":case "underline":case "strikethrough":case "code":return[b];case "strikethrough_italic_bold":return["strikethrough","italic","bold"];case "italic_bold":return["italic","bold"];case "strikethrough_italic":return["strikethrough","italic"];case "strikethrough_bold":return["strikethrough","bold"]}return null}
|
|
23
|
+
function Q(b,d,e,a,c){var f=c.patternMatchResults;const g=f.regExCaptureGroups;var h=g.length;if(b>=h||d>=h)return null;c=I(c).node.getParentOrThrow();h=f.regExCaptureGroups.length;2>h?f=0:(--h,f=f.regExCaptureGroups[h].offsetInParent+f.regExCaptureGroups[h].text.length);b=g[b];d=g[d];a=a?d.offsetInParent+d.text.length:d.offsetInParent;e=v.$findNodeWithOffsetFromJoinedText(e?b.offsetInParent+b.text.length:b.offsetInParent,f,1,c);a=v.$findNodeWithOffsetFromJoinedText(a,f,1,c);if(null==e||null==a)return null;
|
|
24
|
+
c=p.$createRangeSelection();c.anchor.set(e.node.getKey(),e.offset,"text");c.focus.set(a.node.getKey(),a.offset,"text");return c}function R(b,d,e){const a=e.patternMatchResults.regExCaptureGroups;e=Q(b,d,!1,!0,e);if(null!=e&&(p.$setSelection(e),e=p.$getSelection(),null!=e&&p.$isRangeSelection(e)&&!1===e.isCollapsed())){e.removeText();e=0;const c=a.length;for(let f=b;f<c;f++){const g=a[f];f>b&&(g.offsetInParent-=e);f<=d&&(e+=g.text.length,g.text="")}}}
|
|
25
|
+
function S(b){var d=b.patternMatchResults.regExCaptureGroups.length;2>d||(--d,b=Q(d,d,!0,!0,b),null!=b&&p.$setSelection(b))}
|
|
26
|
+
function T(b,d,e){b.update(()=>{if(!0===d.markdownCriteria.requiresParagraphStart){var a=I(d).node.getParentOrThrow();M(a,d,e)}else if(a=d.markdownCriteria,null!=a.markdownFormatKind){var c=N(a.markdownFormatKind);if(null!=c){if(a=c,7===d.patternMatchResults.regExCaptureGroups.length){R(5,5,d);R(1,1,d);c=d.patternMatchResults.regExCaptureGroups;3<c.length||w(65);if(0!==c[3].text.length&&(c=Q(3,3,!1,!0,d),null!=c&&(p.$setSelection(c),c=p.$getSelection(),p.$isRangeSelection(c))))for(var f=0;f<a.length;f++)c.formatText(a[f]);
|
|
27
|
+
S(d)}}else if("link"===a.markdownFormatKind&&(a=d.patternMatchResults.regExCaptureGroups,7===a.length&&(f=a[2].text,a=a[4].text,0!==f.length&&0!==a.length))){R(1,5,d);c=d.patternMatchResults.regExCaptureGroups;if(!(1>=c.length)){f={offsetInParent:c[1].offsetInParent,text:f};var g=Q(1,1,!1,!1,d);if(null!=g&&(p.$setSelection(g),g=p.$getSelection(),null!=g&&p.$isRangeSelection(g)&&g.isCollapsed())){g.insertText(f.text);c.splice(1,0,f);f=f.text.length;g=c.length;for(let h=2;h<g;h++)c[h].offsetInParent+=
|
|
28
|
+
f}}c=Q(1,1,!1,!0,d);null!=c&&(p.$setSelection(c),d.editor.dispatchCommand(t.TOGGLE_LINK_COMMAND,a),S(d))}}},{tag:"history-push"})}
|
|
29
|
+
function U(b,d){let e=null;b.getEditorState().read(()=>{var a=p.$getSelection();if(p.$isRangeSelection(a)){var c=a.anchor.getNode();a=p.$isTextNode(c)?{node:c,offset:a.anchor.offset}:null}else a=null;if(null!==a){a=E(b,!0,a,d);a:{c=!1===d.isParentAListItemNode?D:C;const h=a.triggerState,r=c.length;for(let n=0;n<r;n++){const l=c[n];if(null!=h&&!1===h.isCodeBlock||"paragraphCodeBlock"===l.markdownFormatKind){var f=l,g=a;if(!0===f.requiresParagraphStart)f=J(f,g);else{if(null==g.joinedText){const q=I(g).node.getParentOrThrow();
|
|
30
|
+
p.$isElementNode(q)?null==g.joinedText&&(g.joinedText=v.$joinTextNodesInElementNode(q,"\u0004",I(g))):w(52,q.__key)}f=F(g.joinedText,!1,!0,f.regExForAutoFormatting)}if(null!=f){c={markdownCriteria:l,patternMatchResults:f};break a}}}c={markdownCriteria:null,patternMatchResults:null}}null!==c.markdownCriteria&&null!==c.patternMatchResults&&(e=a,e.markdownCriteria=c.markdownCriteria,e.patternMatchResults=c.patternMatchResults)}});return e}
|
|
31
|
+
function V(b){let d=null;b.read(()=>{const e=p.$getSelection();if(p.$isRangeSelection(e)&&e.isCollapsed()){var a=e.anchor.getNode(),c=a.getParent(),f=m.$isListItemNode(c);d={anchorOffset:e.anchor.offset,hasParentNode:null!==c,isCodeBlock:k.$isCodeNode(a),isParentAListItemNode:f,isSelectionCollapsed:!0,isSimpleText:p.$isTextNode(a)&&a.isSimpleText(),nodeKey:a.getKey(),textContent:a.getTextContent()}}});return d}
|
|
32
|
+
exports.$convertFromMarkdownString=function(b,d,e){if(b.length){var a=[];b=b.split("\n");var c=b.length;for(var f=0;f<c;f++)0<b[f].length?a.push(p.$createParagraphNode().append(p.$createTextNode(b[f]))):a.push(p.$createParagraphNode());a.length?(b=p.$getRoot(),b.clear(),b.append(...a),a=b):a=null}else a=null;if(null!=a)for(d=E(d,!1,null,null),a=p.$getRoot(),b=!1,c=0;!b;){b=!0;f=a.getChildren();const q=f.length;for(let z=c;z<q;z++){var g=f[z];if(p.$isElementNode(g)){var h=d,r=e,n=g.getTextContent();
|
|
33
|
+
if(p.$isParagraphNode(g)){var l=g.getFirstChild();const O=p.$isTextNode(l);if(!0===h.isWithinCodeBlock)null!=l&&O&&(h.textNodeWithOffset={node:l,offset:0},l=F(n,!0,!1,h.isAutoFormatting?B.regExForAutoFormatting:B.regEx),null!=l&&(h.patternMatchResults=l)),h.markdownCriteria=B,M(g,h,r);else if(g.getChildren().length){n=D.length;for(let G=0;G<n;G++){const H=D[G];if(!0===H.requiresParagraphStart){null!=l&&O||w(80);h.textNodeWithOffset={node:l,offset:0};h.joinedText=g.getTextContent();const P=J(H,h);
|
|
34
|
+
if(null!=P){h.markdownCriteria=H;h.patternMatchResults=P;M(g,h,r);break}}}}}}h=d;h.joinedText="";h.markdownCriteria={markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null};h.patternMatchResults={regExCaptureGroups:[]};h.triggerState=null;h.textNodeWithOffset=null;if(a.getChildren().length!==q){c=z;b=!1;break}}}};
|
|
35
|
+
exports.registerMarkdownShortcuts=function(b,d){let e=null;return b.registerUpdateListener(({tags:a})=>{if(!1===a.has("historic")){a=V(b.getEditorState());if(null==a)var c=null;else a:{c=a;var f=e;if(null==c||null==f)c=null;else{var g=x.length;for(let h=0;h<g;h++){const r=x[h].triggerString,n=r.length,l=c.textContent.length,q=c.anchorOffset-n;if(!1===(!0===c.hasParentNode&&c.isSimpleText&&c.isSelectionCollapsed&&c.anchorOffset!==f.anchorOffset&&0<=q&&q+n<=l&&c.textContent.substr(q,n)===r&&c.textContent!==
|
|
36
|
+
f.textContent)){c=null;break a}}c=U(b,c)}}null!=c&&T(b,c,d);e=a}else e=null})};
|
package/package.json
CHANGED
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
"markdown"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.2.0",
|
|
12
12
|
"main": "LexicalMarkdown.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.
|
|
14
|
+
"lexical": "0.2.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.
|
|
18
|
-
"@lexical/code": "0.
|
|
19
|
-
"@lexical/text": "0.
|
|
20
|
-
"@lexical/rich-text": "0.
|
|
21
|
-
"@lexical/list": "0.
|
|
22
|
-
"@lexical/link": "0.
|
|
17
|
+
"@lexical/utils": "0.2.0",
|
|
18
|
+
"@lexical/code": "0.2.0",
|
|
19
|
+
"@lexical/text": "0.2.0",
|
|
20
|
+
"@lexical/rich-text": "0.2.0",
|
|
21
|
+
"@lexical/list": "0.2.0",
|
|
22
|
+
"@lexical/link": "0.2.0"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|