@lexical/markdown 0.14.5 → 0.15.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 +19 -16
- package/LexicalMarkdown.dev.mjs +19 -16
- package/LexicalMarkdown.js +2 -0
- package/LexicalMarkdown.js.flow +2 -0
- package/LexicalMarkdown.mjs +2 -0
- package/LexicalMarkdown.node.mjs +2 -0
- package/LexicalMarkdown.prod.js +26 -23
- package/LexicalMarkdown.prod.mjs +3 -1
- package/package.json +8 -8
package/LexicalMarkdown.dev.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
8
|
+
|
|
7
9
|
'use strict';
|
|
8
10
|
|
|
9
11
|
var lexical = require('lexical');
|
|
@@ -20,6 +22,7 @@ var link = require('@lexical/link');
|
|
|
20
22
|
* LICENSE file in the root directory of this source tree.
|
|
21
23
|
*
|
|
22
24
|
*/
|
|
25
|
+
|
|
23
26
|
function indexBy(list, callback) {
|
|
24
27
|
const index = {};
|
|
25
28
|
for (const item of list) {
|
|
@@ -49,6 +52,7 @@ const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/;
|
|
|
49
52
|
* LICENSE file in the root directory of this source tree.
|
|
50
53
|
*
|
|
51
54
|
*/
|
|
55
|
+
|
|
52
56
|
function createMarkdownExport(transformers) {
|
|
53
57
|
const byType = transformersByType(transformers);
|
|
54
58
|
|
|
@@ -189,20 +193,15 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
|
|
|
189
193
|
* LICENSE file in the root directory of this source tree.
|
|
190
194
|
*
|
|
191
195
|
*/
|
|
196
|
+
|
|
192
197
|
const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
|
|
193
|
-
CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
|
194
|
-
CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
|
|
195
198
|
CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
|
|
196
199
|
const IS_SAFARI = CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
|
|
197
200
|
const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
198
|
-
const IS_ANDROID = CAN_USE_DOM && /Android/.test(navigator.userAgent);
|
|
199
201
|
|
|
200
202
|
// Keep these in case we need to use them in the future.
|
|
201
203
|
// export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
|
|
202
204
|
const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
|
|
203
|
-
// export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
|
|
204
|
-
|
|
205
|
-
CAN_USE_DOM && IS_ANDROID && IS_CHROME;
|
|
206
205
|
const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
|
|
207
206
|
|
|
208
207
|
/**
|
|
@@ -212,8 +211,9 @@ const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.user
|
|
|
212
211
|
* LICENSE file in the root directory of this source tree.
|
|
213
212
|
*
|
|
214
213
|
*/
|
|
214
|
+
|
|
215
215
|
const MARKDOWN_EMPTY_LINE_REG_EXP = /^\s{0,3}$/;
|
|
216
|
-
const CODE_BLOCK_REG_EXP =
|
|
216
|
+
const CODE_BLOCK_REG_EXP = /^[ \t]*```(\w{1,10})?\s?$/;
|
|
217
217
|
function createMarkdownImport(transformers) {
|
|
218
218
|
const byType = transformersByType(transformers);
|
|
219
219
|
const textFormatTransformersIndex = createTextFormatTransformersIndex(byType.textFormat);
|
|
@@ -228,12 +228,12 @@ function createMarkdownImport(transformers) {
|
|
|
228
228
|
// is ignored for further processing
|
|
229
229
|
// TODO:
|
|
230
230
|
// Abstract it to be dynamic as other transformers (add multiline match option)
|
|
231
|
-
const [codeBlockNode, shiftedIndex] = importCodeBlock(lines, i, root);
|
|
231
|
+
const [codeBlockNode, shiftedIndex] = $importCodeBlock(lines, i, root);
|
|
232
232
|
if (codeBlockNode != null) {
|
|
233
233
|
i = shiftedIndex;
|
|
234
234
|
continue;
|
|
235
235
|
}
|
|
236
|
-
importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch);
|
|
236
|
+
$importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch);
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
// Removing empty paragraphs as md does not really
|
|
@@ -256,7 +256,7 @@ function isEmptyParagraph(node) {
|
|
|
256
256
|
const firstChild = node.getFirstChild();
|
|
257
257
|
return firstChild == null || node.getChildrenSize() === 1 && lexical.$isTextNode(firstChild) && MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent());
|
|
258
258
|
}
|
|
259
|
-
function importBlocks(lineText, rootNode, elementTransformers, textFormatTransformersIndex, textMatchTransformers) {
|
|
259
|
+
function $importBlocks(lineText, rootNode, elementTransformers, textFormatTransformersIndex, textMatchTransformers) {
|
|
260
260
|
const lineTextTrimmed = lineText.trim();
|
|
261
261
|
const textNode = lexical.$createTextNode(lineTextTrimmed);
|
|
262
262
|
const elementNode = lexical.$createParagraphNode();
|
|
@@ -297,7 +297,7 @@ function importBlocks(lineText, rootNode, elementTransformers, textFormatTransfo
|
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
299
|
}
|
|
300
|
-
function importCodeBlock(lines, startLineIndex, rootNode) {
|
|
300
|
+
function $importCodeBlock(lines, startLineIndex, rootNode) {
|
|
301
301
|
const openMatch = lines[startLineIndex].match(CODE_BLOCK_REG_EXP);
|
|
302
302
|
if (openMatch) {
|
|
303
303
|
let endLineIndex = startLineIndex;
|
|
@@ -466,6 +466,7 @@ function createTextFormatTransformersIndex(textTransformers) {
|
|
|
466
466
|
* LICENSE file in the root directory of this source tree.
|
|
467
467
|
*
|
|
468
468
|
*/
|
|
469
|
+
|
|
469
470
|
function runElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers) {
|
|
470
471
|
const grandParentNode = parentNode.getParent();
|
|
471
472
|
if (!lexical.$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
|
|
@@ -530,7 +531,7 @@ function runTextMatchTransformers(anchorNode, anchorOffset, transformersByTrigge
|
|
|
530
531
|
}
|
|
531
532
|
return false;
|
|
532
533
|
}
|
|
533
|
-
function runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
|
|
534
|
+
function $runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
|
|
534
535
|
const textContent = anchorNode.getTextContent();
|
|
535
536
|
const closeTagEndIndex = anchorOffset - 1;
|
|
536
537
|
const closeChar = textContent[closeTagEndIndex];
|
|
@@ -682,14 +683,14 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
682
683
|
}
|
|
683
684
|
}
|
|
684
685
|
}
|
|
685
|
-
const transform = (parentNode, anchorNode, anchorOffset) => {
|
|
686
|
+
const $transform = (parentNode, anchorNode, anchorOffset) => {
|
|
686
687
|
if (runElementTransformers(parentNode, anchorNode, anchorOffset, byType.element)) {
|
|
687
688
|
return;
|
|
688
689
|
}
|
|
689
690
|
if (runTextMatchTransformers(anchorNode, anchorOffset, textMatchTransformersIndex)) {
|
|
690
691
|
return;
|
|
691
692
|
}
|
|
692
|
-
runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
|
|
693
|
+
$runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
|
|
693
694
|
};
|
|
694
695
|
return editor.registerUpdateListener(({
|
|
695
696
|
tags,
|
|
@@ -726,7 +727,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
726
727
|
if (parentNode === null || code.$isCodeNode(parentNode)) {
|
|
727
728
|
return;
|
|
728
729
|
}
|
|
729
|
-
transform(parentNode, anchorNode, selection.anchor.offset);
|
|
730
|
+
$transform(parentNode, anchorNode, selection.anchor.offset);
|
|
730
731
|
});
|
|
731
732
|
});
|
|
732
733
|
}
|
|
@@ -738,6 +739,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
738
739
|
* LICENSE file in the root directory of this source tree.
|
|
739
740
|
*
|
|
740
741
|
*/
|
|
742
|
+
|
|
741
743
|
const createBlockNode = createNode => {
|
|
742
744
|
return (parentNode, children, match) => {
|
|
743
745
|
const node = createNode(match);
|
|
@@ -870,7 +872,7 @@ const CODE = {
|
|
|
870
872
|
const textContent = node.getTextContent();
|
|
871
873
|
return '```' + (node.getLanguage() || '') + (textContent ? '\n' + textContent : '') + '\n' + '```';
|
|
872
874
|
},
|
|
873
|
-
regExp:
|
|
875
|
+
regExp: /^[ \t]*```(\w{1,10})?\s/,
|
|
874
876
|
replace: createBlockNode(match => {
|
|
875
877
|
return code.$createCodeNode(match ? match[1] : undefined);
|
|
876
878
|
}),
|
|
@@ -996,6 +998,7 @@ const LINK = {
|
|
|
996
998
|
* LICENSE file in the root directory of this source tree.
|
|
997
999
|
*
|
|
998
1000
|
*/
|
|
1001
|
+
|
|
999
1002
|
const ELEMENT_TRANSFORMERS = [HEADING, QUOTE, CODE, UNORDERED_LIST, ORDERED_LIST];
|
|
1000
1003
|
|
|
1001
1004
|
// Order of text format transformers matters:
|
package/LexicalMarkdown.dev.mjs
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
8
|
+
|
|
7
9
|
import { $getRoot, $isElementNode, $isDecoratorNode, $isLineBreakNode, $isTextNode, $getSelection, $isParagraphNode, $createTextNode, $createParagraphNode, $createLineBreakNode, $isRangeSelection, $isRootOrShadowRoot, $createRangeSelection, $setSelection } from 'lexical';
|
|
8
10
|
import { $createCodeNode, $isCodeNode, CodeNode } from '@lexical/code';
|
|
9
11
|
import { $isListNode, $isListItemNode, ListNode, ListItemNode, $createListItemNode, $createListNode } from '@lexical/list';
|
|
@@ -18,6 +20,7 @@ import { LinkNode, $isLinkNode, $createLinkNode } from '@lexical/link';
|
|
|
18
20
|
* LICENSE file in the root directory of this source tree.
|
|
19
21
|
*
|
|
20
22
|
*/
|
|
23
|
+
|
|
21
24
|
function indexBy(list, callback) {
|
|
22
25
|
const index = {};
|
|
23
26
|
for (const item of list) {
|
|
@@ -47,6 +50,7 @@ const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/;
|
|
|
47
50
|
* LICENSE file in the root directory of this source tree.
|
|
48
51
|
*
|
|
49
52
|
*/
|
|
53
|
+
|
|
50
54
|
function createMarkdownExport(transformers) {
|
|
51
55
|
const byType = transformersByType(transformers);
|
|
52
56
|
|
|
@@ -187,20 +191,15 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
|
|
|
187
191
|
* LICENSE file in the root directory of this source tree.
|
|
188
192
|
*
|
|
189
193
|
*/
|
|
194
|
+
|
|
190
195
|
const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
|
|
191
|
-
CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
|
192
|
-
CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
|
|
193
196
|
CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
|
|
194
197
|
const IS_SAFARI = CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
|
|
195
198
|
const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
196
|
-
const IS_ANDROID = CAN_USE_DOM && /Android/.test(navigator.userAgent);
|
|
197
199
|
|
|
198
200
|
// Keep these in case we need to use them in the future.
|
|
199
201
|
// export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
|
|
200
202
|
const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
|
|
201
|
-
// export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
|
|
202
|
-
|
|
203
|
-
CAN_USE_DOM && IS_ANDROID && IS_CHROME;
|
|
204
203
|
const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
|
|
205
204
|
|
|
206
205
|
/**
|
|
@@ -210,8 +209,9 @@ const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.user
|
|
|
210
209
|
* LICENSE file in the root directory of this source tree.
|
|
211
210
|
*
|
|
212
211
|
*/
|
|
212
|
+
|
|
213
213
|
const MARKDOWN_EMPTY_LINE_REG_EXP = /^\s{0,3}$/;
|
|
214
|
-
const CODE_BLOCK_REG_EXP =
|
|
214
|
+
const CODE_BLOCK_REG_EXP = /^[ \t]*```(\w{1,10})?\s?$/;
|
|
215
215
|
function createMarkdownImport(transformers) {
|
|
216
216
|
const byType = transformersByType(transformers);
|
|
217
217
|
const textFormatTransformersIndex = createTextFormatTransformersIndex(byType.textFormat);
|
|
@@ -226,12 +226,12 @@ function createMarkdownImport(transformers) {
|
|
|
226
226
|
// is ignored for further processing
|
|
227
227
|
// TODO:
|
|
228
228
|
// Abstract it to be dynamic as other transformers (add multiline match option)
|
|
229
|
-
const [codeBlockNode, shiftedIndex] = importCodeBlock(lines, i, root);
|
|
229
|
+
const [codeBlockNode, shiftedIndex] = $importCodeBlock(lines, i, root);
|
|
230
230
|
if (codeBlockNode != null) {
|
|
231
231
|
i = shiftedIndex;
|
|
232
232
|
continue;
|
|
233
233
|
}
|
|
234
|
-
importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch);
|
|
234
|
+
$importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch);
|
|
235
235
|
}
|
|
236
236
|
|
|
237
237
|
// Removing empty paragraphs as md does not really
|
|
@@ -254,7 +254,7 @@ function isEmptyParagraph(node) {
|
|
|
254
254
|
const firstChild = node.getFirstChild();
|
|
255
255
|
return firstChild == null || node.getChildrenSize() === 1 && $isTextNode(firstChild) && MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent());
|
|
256
256
|
}
|
|
257
|
-
function importBlocks(lineText, rootNode, elementTransformers, textFormatTransformersIndex, textMatchTransformers) {
|
|
257
|
+
function $importBlocks(lineText, rootNode, elementTransformers, textFormatTransformersIndex, textMatchTransformers) {
|
|
258
258
|
const lineTextTrimmed = lineText.trim();
|
|
259
259
|
const textNode = $createTextNode(lineTextTrimmed);
|
|
260
260
|
const elementNode = $createParagraphNode();
|
|
@@ -295,7 +295,7 @@ function importBlocks(lineText, rootNode, elementTransformers, textFormatTransfo
|
|
|
295
295
|
}
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
|
-
function importCodeBlock(lines, startLineIndex, rootNode) {
|
|
298
|
+
function $importCodeBlock(lines, startLineIndex, rootNode) {
|
|
299
299
|
const openMatch = lines[startLineIndex].match(CODE_BLOCK_REG_EXP);
|
|
300
300
|
if (openMatch) {
|
|
301
301
|
let endLineIndex = startLineIndex;
|
|
@@ -464,6 +464,7 @@ function createTextFormatTransformersIndex(textTransformers) {
|
|
|
464
464
|
* LICENSE file in the root directory of this source tree.
|
|
465
465
|
*
|
|
466
466
|
*/
|
|
467
|
+
|
|
467
468
|
function runElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers) {
|
|
468
469
|
const grandParentNode = parentNode.getParent();
|
|
469
470
|
if (!$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
|
|
@@ -528,7 +529,7 @@ function runTextMatchTransformers(anchorNode, anchorOffset, transformersByTrigge
|
|
|
528
529
|
}
|
|
529
530
|
return false;
|
|
530
531
|
}
|
|
531
|
-
function runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
|
|
532
|
+
function $runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
|
|
532
533
|
const textContent = anchorNode.getTextContent();
|
|
533
534
|
const closeTagEndIndex = anchorOffset - 1;
|
|
534
535
|
const closeChar = textContent[closeTagEndIndex];
|
|
@@ -680,14 +681,14 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
680
681
|
}
|
|
681
682
|
}
|
|
682
683
|
}
|
|
683
|
-
const transform = (parentNode, anchorNode, anchorOffset) => {
|
|
684
|
+
const $transform = (parentNode, anchorNode, anchorOffset) => {
|
|
684
685
|
if (runElementTransformers(parentNode, anchorNode, anchorOffset, byType.element)) {
|
|
685
686
|
return;
|
|
686
687
|
}
|
|
687
688
|
if (runTextMatchTransformers(anchorNode, anchorOffset, textMatchTransformersIndex)) {
|
|
688
689
|
return;
|
|
689
690
|
}
|
|
690
|
-
runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
|
|
691
|
+
$runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
|
|
691
692
|
};
|
|
692
693
|
return editor.registerUpdateListener(({
|
|
693
694
|
tags,
|
|
@@ -724,7 +725,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
724
725
|
if (parentNode === null || $isCodeNode(parentNode)) {
|
|
725
726
|
return;
|
|
726
727
|
}
|
|
727
|
-
transform(parentNode, anchorNode, selection.anchor.offset);
|
|
728
|
+
$transform(parentNode, anchorNode, selection.anchor.offset);
|
|
728
729
|
});
|
|
729
730
|
});
|
|
730
731
|
}
|
|
@@ -736,6 +737,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
|
|
|
736
737
|
* LICENSE file in the root directory of this source tree.
|
|
737
738
|
*
|
|
738
739
|
*/
|
|
740
|
+
|
|
739
741
|
const createBlockNode = createNode => {
|
|
740
742
|
return (parentNode, children, match) => {
|
|
741
743
|
const node = createNode(match);
|
|
@@ -868,7 +870,7 @@ const CODE = {
|
|
|
868
870
|
const textContent = node.getTextContent();
|
|
869
871
|
return '```' + (node.getLanguage() || '') + (textContent ? '\n' + textContent : '') + '\n' + '```';
|
|
870
872
|
},
|
|
871
|
-
regExp:
|
|
873
|
+
regExp: /^[ \t]*```(\w{1,10})?\s/,
|
|
872
874
|
replace: createBlockNode(match => {
|
|
873
875
|
return $createCodeNode(match ? match[1] : undefined);
|
|
874
876
|
}),
|
|
@@ -994,6 +996,7 @@ const LINK = {
|
|
|
994
996
|
* LICENSE file in the root directory of this source tree.
|
|
995
997
|
*
|
|
996
998
|
*/
|
|
999
|
+
|
|
997
1000
|
const ELEMENT_TRANSFORMERS = [HEADING, QUOTE, CODE, UNORDERED_LIST, ORDERED_LIST];
|
|
998
1001
|
|
|
999
1002
|
// Order of text format transformers matters:
|
package/LexicalMarkdown.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
8
|
+
|
|
7
9
|
'use strict'
|
|
8
10
|
const LexicalMarkdown = process.env.NODE_ENV === 'development' ? require('./LexicalMarkdown.dev.js') : require('./LexicalMarkdown.prod.js');
|
|
9
11
|
module.exports = LexicalMarkdown;
|
package/LexicalMarkdown.js.flow
CHANGED
|
@@ -68,12 +68,14 @@ declare export function registerMarkdownShortcuts(
|
|
|
68
68
|
declare export function $convertFromMarkdownString(
|
|
69
69
|
markdown: string,
|
|
70
70
|
transformers?: Array<Transformer>,
|
|
71
|
+
node?: ElementNode,
|
|
71
72
|
): void;
|
|
72
73
|
|
|
73
74
|
// TODO:
|
|
74
75
|
// transformers should be required argument, breaking change
|
|
75
76
|
declare export function $convertToMarkdownString(
|
|
76
77
|
transformers?: Array<Transformer>,
|
|
78
|
+
node?: ElementNode,
|
|
77
79
|
): string;
|
|
78
80
|
|
|
79
81
|
declare export var BOLD_ITALIC_STAR: TextFormatTransformer;
|
package/LexicalMarkdown.mjs
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
8
|
+
|
|
7
9
|
import * as modDev from './LexicalMarkdown.dev.mjs';
|
|
8
10
|
import * as modProd from './LexicalMarkdown.prod.mjs';
|
|
9
11
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
package/LexicalMarkdown.node.mjs
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
8
|
+
|
|
7
9
|
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalMarkdown.dev.mjs') : import('./LexicalMarkdown.prod.mjs'));
|
|
8
10
|
export const $convertFromMarkdownString = mod.$convertFromMarkdownString;
|
|
9
11
|
export const $convertToMarkdownString = mod.$convertToMarkdownString;
|
package/LexicalMarkdown.prod.js
CHANGED
|
@@ -3,33 +3,36 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
7
|
-
|
|
8
|
+
|
|
9
|
+
'use strict';var k=require("lexical"),t=require("@lexical/code"),A=require("@lexical/list"),F=require("@lexical/rich-text"),aa=require("@lexical/utils"),G=require("@lexical/link");function H(a,b){let c={};for(let d of a)a=b(d),c[a]?c[a].push(d):c[a]=[d];return c}function I(a){a=H(a,b=>b.type);return{element:a.element||[],textFormat:a["text-format"]||[],textMatch:a["text-match"]||[]}}let J=/[!-/:-@[-`{-~\s]/;
|
|
8
10
|
function ba(a){let b=I(a),c=b.textFormat.filter(d=>1===d.format.length);return d=>{let e=[];d=(d||k.$getRoot()).getChildren();for(let f of d)d=ca(f,b.element,c,b.textMatch),null!=d&&e.push(d);return e.join("\n\n")}}function ca(a,b,c,d){for(let e of b)if(b=e.export(a,f=>K(f,c,d)),null!=b)return b;return k.$isElementNode(a)?K(a,c,d):k.$isDecoratorNode(a)?a.getTextContent():null}
|
|
9
11
|
function K(a,b,c){let d=[];a=a.getChildren();a:for(let e of a){for(let f of c)if(a=f.export(e,g=>K(g,b,c),(g,l)=>L(g,l,b)),null!=a){d.push(a);continue a}k.$isLineBreakNode(e)?d.push("\n"):k.$isTextNode(e)?d.push(L(e,e.getTextContent(),b)):k.$isElementNode(e)?d.push(K(e,b,c)):k.$isDecoratorNode(e)&&d.push(e.getTextContent())}return d.join("")}
|
|
10
12
|
function L(a,b,c){let d=b.trim(),e=d,f=new Set;for(let l of c){c=l.format[0];let r=l.tag;if(M(a,c)&&!f.has(c)){f.add(c);var g=N(a,!0);M(g,c)||(e=r+e);g=N(a,!1);M(g,c)||(e+=r)}}return b.replace(d,()=>e)}
|
|
11
13
|
function N(a,b){let c=b?a.getPreviousSibling():a.getNextSibling();c||(a=a.getParentOrThrow(),a.isInline()&&(c=b?a.getPreviousSibling():a.getNextSibling()));for(;c;){if(k.$isElementNode(c)){if(!c.isInline())break;a=b?c.getLastDescendant():c.getFirstDescendant();if(k.$isTextNode(a))return a;c=b?c.getPreviousSibling():c.getNextSibling()}if(k.$isTextNode(c))return c;if(!k.$isElementNode(c))break}return null}function M(a,b){return k.$isTextNode(a)&&a.hasFormat(b)}
|
|
12
|
-
let O="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,da=O&&"documentMode"in document?document.documentMode:null;O
|
|
13
|
-
let P=O&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),Q=O&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,ea=O
|
|
14
|
-
function ha(a){let b=I(a),c=ia(b.textFormat);return(d,e)=>{d=d.split("\n");var f=d.length;e=e||k.$getRoot();e.clear();for(let h=0;h<f;h++){var g=d[h];a:{var l=d,r=h;var q=e;var y=l[r].match(
|
|
15
|
-
g.append(y);m.append(g);for(let {regExp:x,replace:u}of w)if(m=q.match(x)){y.setTextContent(q.slice(m[0].length));u(g,[y],m,!0);break}
|
|
14
|
+
let O="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,da=O&&"documentMode"in document?document.documentMode:null;O&&"InputEvent"in window&&!da?"getTargetRanges"in new window.InputEvent("input"):!1;
|
|
15
|
+
let P=O&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),Q=O&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,ea=O&&/^(?=.*Chrome).*/i.test(navigator.userAgent),R=O&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!ea,fa=/^\s{0,3}$/,S=/^[ \t]*```(\w{1,10})?\s?$/;
|
|
16
|
+
function ha(a){let b=I(a),c=ia(b.textFormat);return(d,e)=>{d=d.split("\n");var f=d.length;e=e||k.$getRoot();e.clear();for(let h=0;h<f;h++){var g=d[h];a:{var l=d,r=h;var q=e;var y=l[r].match(S);if(y)for(var p=r,m=l.length;++p<m;)if(l[p].match(S)){y=t.$createCodeNode(y[1]);l=k.$createTextNode(l.slice(r+1,p).join("\n"));y.append(l);q.append(y);q=[y,p];break a}q=[null,r]}let [n,v]=q;if(null!=n)h=v;else{q=g;m=e;var w=b.element;p=c;l=b.textMatch;r=q.trim();y=k.$createTextNode(r);g=k.$createParagraphNode();
|
|
17
|
+
g.append(y);m.append(g);for(let {regExp:x,replace:u}of w)if(m=q.match(x)){y.setTextContent(q.slice(m[0].length));u(g,[y],m,!0);break}T(y,p,l);g.isAttached()&&0<r.length&&(q=g.getPreviousSibling(),k.$isParagraphNode(q)||F.$isQuoteNode(q)||A.$isListNode(q))&&(p=q,A.$isListNode(q)&&(q=q.getLastDescendant(),p=null==q?null:aa.$findMatchingParent(q,A.$isListItemNode)),null!=p&&0<p.getTextContentSize()&&(p.splice(p.getChildrenSize(),0,[k.$createLineBreakNode(),...g.getChildren()]),g.remove()))}}d=e.getChildren();
|
|
16
18
|
for(let h of d)d=h,k.$isParagraphNode(d)?(f=d.getFirstChild(),d=null==f||1===d.getChildrenSize()&&k.$isTextNode(f)&&fa.test(f.getTextContent())):d=!1,d&&1<e.getChildrenSize()&&h.remove();null!==k.$getSelection()&&e.selectEnd()}}
|
|
17
|
-
function
|
|
18
|
-
function
|
|
19
|
+
function T(a,b,c){var d=a.getTextContent();let e=ja(d,b);if(e){var f,g;if(e[0]===d)var l=a;else{d=e.index||0;let r=d+e[0].length;0===d?[l,f]=a.splitText(r):[g,l,f]=a.splitText(d,r)}l.setTextContent(e[2]);if(a=b.transformersByTag[e[1]])for(let r of a.format)l.hasFormat(r)||l.toggleFormat(r);l.hasFormat("code")||T(l,b,c);g&&T(g,b,c);f&&T(f,b,c)}else U(a,c)}
|
|
20
|
+
function U(a,b){a:for(;a;){for(let c of b){let d=a.getTextContent().match(c.importRegExp);if(!d)continue;let e=d.index||0,f=e+d[0].length,g,l;0===e?[g,a]=a.splitText(f):[,g,l]=a.splitText(e,f);l&&U(l,b);c.replace(g,d);continue a}break}}
|
|
19
21
|
function ja(a,b){var c=a.match(b.openTagsRegExp);if(null==c)return null;for(let f of c){var d=f.replace(/^\s/,"");c=b.fullMatchRegExpByTag[d];if(null!=c&&(c=a.match(c),d=b.transformersByTag[d],null!=c&&null!=d)){if(!1!==d.intraword)return c;var {index:e=0}=c;d=a[e-1];e=a[e+c[0].length];if(!(d&&!J.test(d)||e&&!J.test(e)))return c}}return null}
|
|
20
|
-
function ia(a){let b={},c={},d=[];for(let e of a){({tag:a}=e);b[a]=e;let f=a.replace(/(\*|\^|\+)/g,"\\$1");d.push(f);c[a]=P||Q||
|
|
21
|
-
function W(a
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
c
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
exports.
|
|
31
|
-
exports.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
function ia(a){let b={},c={},d=[];for(let e of a){({tag:a}=e);b[a]=e;let f=a.replace(/(\*|\^|\+)/g,"\\$1");d.push(f);c[a]=P||Q||R?new RegExp(`(${f})(?![${f}\\s])(.*?[^${f}\\s])${f}(?!${f})`):new RegExp(`(?<![\\\\${f}])(${f})((\\\\${f})?.*?[^${f}\\s](\\\\${f})?)((?<!\\\\)|(?<=\\\\\\\\))(${f})(?![\\\\${f}])`)}return{fullMatchRegExpByTag:c,openTagsRegExp:new RegExp((P||Q||R?"":"(?<![\\\\])")+"("+d.join("|")+")","g"),transformersByTag:b}}var V;
|
|
23
|
+
function W(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}V=W&&W.__esModule&&Object.prototype.hasOwnProperty.call(W,"default")?W["default"]:W;
|
|
24
|
+
function ka(a,b,c){let d=c.length;for(;b>=d;b--){let e=b-d;if(la(a,e,c,0,d)&&" "!==a[e+d])return e}return-1}function la(a,b,c,d,e){for(let f=0;f<e;f++)if(a[b+f]!==c[d+f])return!1;return!0}
|
|
25
|
+
let ma=a=>(b,c,d)=>{d=a(d);d.append(...c);b.replace(d);d.select(0,0)},X=a=>(b,c,d)=>{var e=b.getPreviousSibling(),f=b.getNextSibling();const g=A.$createListItemNode("check"===a?"x"===d[3]:void 0);A.$isListNode(f)&&f.getListType()===a?(e=f.getFirstChild(),null!==e?e.insertBefore(g):f.append(g),b.remove()):A.$isListNode(e)&&e.getListType()===a?(e.append(g),b.remove()):(f=A.$createListNode(a,"number"===a?Number(d[2]):void 0),f.append(g),b.replace(f));g.append(...c);g.select(0,0);c=d[1];b=c.match(/\t/g);
|
|
26
|
+
c=c.match(/ /g);d=0;b&&(d+=b.length);c&&(d+=Math.floor(c.length/4));(b=d)&&g.setIndent(b)},Y=(a,b,c)=>{const d=[];var e=a.getChildren();let f=0;for(const l of e)if(A.$isListItemNode(l)){if(1===l.getChildrenSize()&&(e=l.getFirstChild(),A.$isListNode(e))){d.push(Y(e,b,c+1));continue}e=" ".repeat(4*c);var g=a.getListType();g="number"===g?`${a.getStart()+f}. `:"check"===g?`- [${l.getChecked()?"x":" "}] `:"- ";d.push(e+g+b(l));f++}return d.join("\n")},na={dependencies:[F.HeadingNode],export:(a,b)=>{if(!F.$isHeadingNode(a))return null;
|
|
27
|
+
const c=Number(a.getTag().slice(1));return"#".repeat(c)+" "+b(a)},regExp:/^(#{1,6})\s/,replace:ma(a=>F.$createHeadingNode("h"+a[1].length)),type:"element"},oa={dependencies:[F.QuoteNode],export:(a,b)=>{if(!F.$isQuoteNode(a))return null;a=b(a).split("\n");b=[];for(const c of a)b.push("> "+c);return b.join("\n")},regExp:/^>\s/,replace:(a,b,c,d)=>{if(d&&(c=a.getPreviousSibling(),F.$isQuoteNode(c))){c.splice(c.getChildrenSize(),0,[k.$createLineBreakNode(),...b]);c.select(0,0);a.remove();return}c=F.$createQuoteNode();
|
|
28
|
+
c.append(...b);a.replace(c);c.select(0,0)},type:"element"},pa={dependencies:[t.CodeNode],export:a=>{if(!t.$isCodeNode(a))return null;const b=a.getTextContent();return"```"+(a.getLanguage()||"")+(b?"\n"+b:"")+"\n```"},regExp:/^[ \t]*```(\w{1,10})?\s/,replace:ma(a=>t.$createCodeNode(a?a[1]:void 0)),type:"element"},qa={dependencies:[A.ListNode,A.ListItemNode],export:(a,b)=>A.$isListNode(a)?Y(a,b,0):null,regExp:/^(\s*)[-*+]\s/,replace:X("bullet"),type:"element"},ra={dependencies:[A.ListNode,A.ListItemNode],
|
|
29
|
+
export:(a,b)=>A.$isListNode(a)?Y(a,b,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:X("check"),type:"element"},sa={dependencies:[A.ListNode,A.ListItemNode],export:(a,b)=>A.$isListNode(a)?Y(a,b,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:X("number"),type:"element"},ta={format:["code"],tag:"`",type:"text-format"},ua={format:["highlight"],tag:"==",type:"text-format"},va={format:["bold","italic"],tag:"***",type:"text-format"},xa={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},
|
|
30
|
+
ya={format:["bold"],tag:"**",type:"text-format"},za={format:["bold"],intraword:!1,tag:"__",type:"text-format"},Aa={format:["strikethrough"],tag:"~~",type:"text-format"},Ba={format:["italic"],tag:"*",type:"text-format"},Ca={format:["italic"],intraword:!1,tag:"_",type:"text-format"},Da={dependencies:[G.LinkNode],export:(a,b,c)=>{if(!G.$isLinkNode(a))return null;b=(b=a.getTitle())?`[${a.getTextContent()}](${a.getURL()} "${b}")`:`[${a.getTextContent()}](${a.getURL()})`;const d=a.getFirstChild();return 1===
|
|
31
|
+
a.getChildrenSize()&&k.$isTextNode(d)?c(d,b):b},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(a,b)=>{const [,c,d,e]=b;b=G.$createLinkNode(d,{title:e});const f=k.$createTextNode(c);f.setFormat(a.getFormat());b.append(f);a.replace(b)},trigger:")",type:"text-match"},Ea=[na,oa,pa,qa,sa],Fa=[ta,va,xa,ya,za,ua,Ba,Ca,Aa],Ga=[Da],Z=[...Ea,...Fa,...Ga];
|
|
32
|
+
exports.$convertFromMarkdownString=function(a,b=Z,c){return ha(b)(a,c)};exports.$convertToMarkdownString=function(a=Z,b){return ba(a)(b)};exports.BOLD_ITALIC_STAR=va;exports.BOLD_ITALIC_UNDERSCORE=xa;exports.BOLD_STAR=ya;exports.BOLD_UNDERSCORE=za;exports.CHECK_LIST=ra;exports.CODE=pa;exports.ELEMENT_TRANSFORMERS=Ea;exports.HEADING=na;exports.HIGHLIGHT=ua;exports.INLINE_CODE=ta;exports.ITALIC_STAR=Ba;exports.ITALIC_UNDERSCORE=Ca;exports.LINK=Da;exports.ORDERED_LIST=sa;exports.QUOTE=oa;
|
|
33
|
+
exports.STRIKETHROUGH=Aa;exports.TEXT_FORMAT_TRANSFORMERS=Fa;exports.TEXT_MATCH_TRANSFORMERS=Ga;exports.TRANSFORMERS=Z;exports.UNORDERED_LIST=qa;
|
|
34
|
+
exports.registerMarkdownShortcuts=function(a,b=Z){let c=I(b),d=H(c.textFormat,({tag:f})=>f[f.length-1]),e=H(c.textMatch,({trigger:f})=>f);for(let f of b)if(b=f.type,"element"===b||"text-match"===b){b=f.dependencies;for(let g of b)a.hasNode(g)||V(173,g.getType())}return a.registerUpdateListener(({tags:f,dirtyLeaves:g,editorState:l,prevEditorState:r})=>{if(!f.has("collaboration")&&!f.has("historic")&&!a.isComposing()){var q=l.read(k.$getSelection);f=r.read(k.$getSelection);if(k.$isRangeSelection(f)&&
|
|
35
|
+
k.$isRangeSelection(q)&&q.isCollapsed()){r=q.anchor.key;var y=q.anchor.offset,p=l._nodeMap.get(r);!k.$isTextNode(p)||!g.has(r)||1!==y&&y>f.anchor.offset+1||a.update(()=>{if(!p.hasFormat("code")){var m=p.getParent();if(null!==m&&!t.$isCodeNode(m)){var w=q.anchor.offset;b:{var h=c.element,n=m.getParent();if(k.$isRootOrShadowRoot(n)&&m.getFirstChild()===p&&(n=p.getTextContent()," "===n[w-1]))for(let {regExp:C,replace:D}of h)if((h=n.match(C))&&h[0].length===w){n=p.getNextSiblings();let [E,wa]=p.splitText(w);
|
|
36
|
+
E.remove();n=wa?[wa,...n]:n;D(m,n,h,!1);m=!0;break b}m=!1}if(!m){b:{h=p.getTextContent();m=e[h[w-1]];if(null!=m){w<h.length&&(h=h.slice(0,w));for(x of m)if(m=h.match(x.regExp),null!==m){h=m.index||0;n=h+m[0].length;var v=void 0;0===h?[v]=p.splitText(n):[,v]=p.splitText(h,n);v.selectNext(0,0);x.replace(v,m);var x=!0;break b}}x=!1}if(!x)b:{n=p.getTextContent();--w;var u=n[w];if(x=d[u])for(let C of x){var {tag:B}=C;x=B.length;let D=w-x+1;if(!(1<x&&!la(n,D,B,0,x)||" "===n[D-1])&&(v=n[w+1],!1!==C.intraword||
|
|
37
|
+
!v||J.test(v))){m=v=p;h=ka(n,D,B);for(var z=m;0>h&&(z=z.getPreviousSibling())&&!k.$isLineBreakNode(z);)k.$isTextNode(z)&&(h=z.getTextContent(),m=z,h=ka(h,h.length,B));if(!(0>h||m===v&&h+x===D||(B=m.getTextContent(),0<h&&B[h-1]===u||(z=B[h-1],!1===C.intraword&&z&&!J.test(z))))){n=v.getTextContent();n=n.slice(0,D)+n.slice(w+1);v.setTextContent(n);n=m===v?n:B;m.setTextContent(n.slice(0,h)+n.slice(h+x));n=k.$getSelection();u=k.$createRangeSelection();k.$setSelection(u);w=w-x*(m===v?2:1)+1;u.anchor.set(m.__key,
|
|
38
|
+
h,"text");u.focus.set(v.__key,w,"text");for(let E of C.format)u.hasFormat(E)||u.formatText(E);u.anchor.set(u.focus.key,u.focus.offset,u.focus.type);for(let E of C.format)u.hasFormat(E)&&u.toggleFormat(E);k.$isRangeSelection(n)&&(u.format=n.format);break b}}}}}}}})}}})}
|
package/LexicalMarkdown.prod.mjs
CHANGED
|
@@ -3,5 +3,7 @@
|
|
|
3
3
|
*
|
|
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
|
*/
|
|
7
|
-
import{$getRoot as t,$isElementNode as e,$isDecoratorNode as n,$isLineBreakNode as o,$isTextNode as r,$getSelection as i,$isParagraphNode as s,$createTextNode as c,$createParagraphNode as l,$createLineBreakNode as a,$isRangeSelection as f,$isRootOrShadowRoot as u,$createRangeSelection as g,$setSelection as p}from"lexical";import{$createCodeNode as d,$isCodeNode as m,CodeNode as h}from"@lexical/code";import{$isListNode as x,$isListItemNode as T,ListNode as C,ListItemNode as y,$createListItemNode as v,$createListNode as w}from"@lexical/list";import{$isQuoteNode as S,HeadingNode as b,$isHeadingNode as E,QuoteNode as $,$createQuoteNode as F,$createHeadingNode as P}from"@lexical/rich-text";import{$findMatchingParent as k}from"@lexical/utils";import{LinkNode as M,$isLinkNode as R,$createLinkNode as _}from"@lexical/link";function L(t,e){const n={};for(const o of t){const t=e(o);n[t]?n[t].push(o):n[t]=[o]}return n}function A(t){const e=L(t,(t=>t.type));return{element:e.element||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const N=/[!-/:-@[-`{-~\s]/;function z(t,o,r,i){for(const e of o){const n=e.export(t,(t=>j(t,r,i)));if(null!=n)return n}return e(t)?j(t,r,i):n(t)?t.getTextContent():null}function j(t,i,s){const c=[],l=t.getChildren();t:for(const t of l){for(const e of s){const n=e.export(t,(t=>j(t,i,s)),((t,e)=>B(t,e,i)));if(null!=n){c.push(n);continue t}}o(t)?c.push("\n"):r(t)?c.push(B(t,t.getTextContent(),i)):e(t)?c.push(j(t,i,s)):n(t)&&c.push(t.getTextContent())}return c.join("")}function B(t,e,n){const o=e.trim();let r=o;const i=new Set;for(const e of n){const n=e.format[0],o=e.tag;if(D(t,n)&&!i.has(n)){i.add(n);D(I(t,!0),n)||(r=o+r);D(I(t,!1),n)||(r+=o)}}return e.replace(o,(()=>r))}function I(t,n){let o=n?t.getPreviousSibling():t.getNextSibling();if(!o){const e=t.getParentOrThrow();e.isInline()&&(o=n?e.getPreviousSibling():e.getNextSibling())}for(;o;){if(e(o)){if(!o.isInline())break;const t=n?o.getLastDescendant():o.getFirstDescendant();if(r(t))return t;o=n?o.getPreviousSibling():o.getNextSibling()}if(r(o))return o;if(!e(o))return null}return null}function D(t,e){return r(t)&&t.hasFormat(e)}const U="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,K=U&&"documentMode"in document?document.documentMode:null;U&&/Mac|iPod|iPhone|iPad/.test(navigator.platform),U&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent),U&&"InputEvent"in window&&!K&&new window.InputEvent("input");const O=U&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),V=U&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,W=(U&&/Android/.test(navigator.userAgent),U&&/^(?=.*Chrome).*/i.test(navigator.userAgent)),q=U&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!W,G=/^\s{0,3}$/,H=/^```(\w{1,10})?\s?$/;function J(e){const n=A(e),o=function(t){const e={},n={},o=[],r="(?<![\\\\])";for(const r of t){const{tag:t}=r;e[t]=r;const i=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),n[t]=O||V||q?new RegExp(`(${i})(?![${i}\\s])(.*?[^${i}\\s])${i}(?!${i})`):new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp((O||V||q?"":`${r}`)+"("+o.join("|")+")","g"),transformersByTag:e}}(n.textFormat);return(e,r)=>{const s=e.split("\n"),c=s.length,l=r||t();l.clear();for(let t=0;t<c;t++){const e=s[t],[r,i]=Y(s,t,l);null==r?X(e,l,n.element,o,n.textMatch):t=i}const a=l.getChildren();for(const t of a)Q(t)&&l.getChildrenSize()>1&&t.remove();null!==i()&&l.selectEnd()}}function Q(t){if(!s(t))return!1;const e=t.getFirstChild();return null==e||1===t.getChildrenSize()&&r(e)&&G.test(e.getTextContent())}function X(t,e,n,o,r){const i=t.trim(),f=c(i),u=l();u.append(f),e.append(u);for(const{regExp:e,replace:o}of n){const n=t.match(e);if(n){f.setTextContent(t.slice(n[0].length)),o(u,[f],n,!0);break}}if(Z(f,o,r),u.isAttached()&&i.length>0){const t=u.getPreviousSibling();if(s(t)||S(t)||x(t)){let e=t;if(x(t)){const n=t.getLastDescendant();e=null==n?null:k(n,T)}null!=e&&e.getTextContentSize()>0&&(e.splice(e.getChildrenSize(),0,[a(),...u.getChildren()]),u.remove())}}}function Y(t,e,n){const o=t[e].match(H);if(o){let r=e;const i=t.length;for(;++r<i;){if(t[r].match(H)){const i=d(o[1]),s=c(t.slice(e+1,r).join("\n"));return i.append(s),n.append(i),[i,r]}}}return[null,e]}function Z(t,e,n){const o=t.getTextContent(),r=function(t,e){const n=t.match(e.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=e.fullMatchRegExpByTag[n];if(null==r)continue;const i=t.match(r),s=e.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:e=0}=i,n=t[e-1],o=t[e+i[0].length];if((!n||N.test(n))&&(!o||N.test(o)))return i}}return null}(o,e);if(!r)return void tt(t,n);let i,s,c;if(r[0]===o)i=t;else{const e=r.index||0,n=e+r[0].length;0===e?[i,s]=t.splitText(n):[c,i,s]=t.splitText(e,n)}i.setTextContent(r[2]);const l=e.transformersByTag[r[1]];if(l)for(const t of l.format)i.hasFormat(t)||i.toggleFormat(t);i.hasFormat("code")||Z(i,e,n),c&&Z(c,e,n),s&&Z(s,e,n)}function tt(t,e){let n=t;t:for(;n;){for(const t of e){const o=n.getTextContent().match(t.importRegExp);if(!o)continue;const r=o.index||0,i=r+o[0].length;let s,c;0===r?[s,n]=n.splitText(i):[,s,c]=n.splitText(r,i),c&&tt(c,e),t.replace(s,o);continue t}break}}function et(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(nt(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function nt(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function ot(t,e=$t){const n=A(e),s=L(n.textFormat,(({tag:t})=>t[t.length-1])),c=L(n.textMatch,(({trigger:t})=>t));for(const n of e){const e=n.type;if("element"===e||"text-match"===e){const e=n.dependencies;for(const n of e)if(!t.hasNode(n))throw Error(`MarkdownShortcuts: missing dependency ${n.getType()} for transformer. Ensure node dependency is included in editor initial config.`)}}const l=(t,e,l)=>{(function(t,e,n,o){const r=t.getParent();if(!u(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExp:r,replace:s}of o){const o=i.match(r);if(o&&o[0].length===n){const r=e.getNextSiblings(),[i,c]=e.splitText(n);return i.remove(),s(t,c?[c,...r]:r,o,!1),!0}}return!1})(t,e,l,n.element)||function(t,e,n){let o=t.getTextContent();const r=n[o[e-1]];if(null==r)return!1;e<o.length&&(o=o.slice(0,e));for(const e of r){const n=o.match(e.regExp);if(null===n)continue;const r=n.index||0,i=r+n[0].length;let s;return 0===r?[s]=t.splitText(i):[,s]=t.splitText(r,i),s.selectNext(0,0),e.replace(s,n),!0}return!1}(e,l,c)||function(t,e,n){const s=t.getTextContent(),c=e-1,l=s[c],a=n[l];if(!a)return!1;for(const e of a){const{tag:n}=e,a=n.length,u=c-a+1;if(a>1&&!nt(s,u,n,0,a))continue;if(" "===s[u-1])continue;const d=s[c+1];if(!1===e.intraword&&d&&!N.test(d))continue;const m=t;let h=m,x=et(s,u,n),T=h;for(;x<0&&(T=T.getPreviousSibling())&&!o(T);)if(r(T)){const t=T.getTextContent();h=T,x=et(t,t.length,n)}if(x<0)continue;if(h===m&&x+a===u)continue;const C=h.getTextContent();if(x>0&&C[x-1]===l)continue;const y=C[x-1];if(!1===e.intraword&&y&&!N.test(y))continue;const v=m.getTextContent(),w=v.slice(0,u)+v.slice(c+1);m.setTextContent(w);const S=h===m?w:C;h.setTextContent(S.slice(0,x)+S.slice(x+a));const b=i(),E=g();p(E);const $=c-a*(h===m?2:1)+1;E.anchor.set(h.__key,x,"text"),E.focus.set(m.__key,$,"text");for(const t of e.format)E.hasFormat(t)||E.formatText(t);E.anchor.set(E.focus.key,E.focus.offset,E.focus.type);for(const t of e.format)E.hasFormat(t)&&E.toggleFormat(t);return f(b)&&(E.format=b.format),!0}}(e,l,s)};return t.registerUpdateListener((({tags:e,dirtyLeaves:n,editorState:o,prevEditorState:s})=>{if(e.has("collaboration")||e.has("historic"))return;if(t.isComposing())return;const c=o.read(i),a=s.read(i);if(!f(a)||!f(c)||!c.isCollapsed())return;const u=c.anchor.key,g=c.anchor.offset,p=o._nodeMap.get(u);!r(p)||!n.has(u)||1!==g&&g>a.anchor.offset+1||t.update((()=>{if(p.hasFormat("code"))return;const t=p.getParent();null===t||m(t)||l(t,p,c.anchor.offset)}))}))}const rt=t=>(e,n,o)=>{const r=t(o);r.append(...n),e.replace(r),r.select(0,0)};const it=t=>(e,n,o)=>{const r=e.getPreviousSibling(),i=e.getNextSibling(),s=v("check"===t?"x"===o[3]:void 0);if(x(i)&&i.getListType()===t){const t=i.getFirstChild();null!==t?t.insertBefore(s):i.append(s),e.remove()}else if(x(r)&&r.getListType()===t)r.append(s),e.remove();else{const n=w(t,"number"===t?Number(o[2]):void 0);n.append(s),e.replace(n)}s.append(...n),s.select(0,0);const c=function(t){const e=t.match(/\t/g),n=t.match(/ /g);let o=0;return e&&(o+=e.length),n&&(o+=Math.floor(n.length/4)),o}(o[1]);c&&s.setIndent(c)},st=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(T(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(x(t)){o.push(st(t,e,n+1));continue}}const r=" ".repeat(4*n),c=t.getListType(),l="number"===c?`${t.getStart()+i}. `:"check"===c?`- [${s.getChecked()?"x":" "}] `:"- ";o.push(r+l+e(s)),i++}return o.join("\n")},ct={dependencies:[b],export:(t,e)=>{if(!E(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:/^(#{1,6})\s/,replace:rt((t=>{const e="h"+t[1].length;return P(e)})),type:"element"},lt={dependencies:[$],export:(t,e)=>{if(!S(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:/^>\s/,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(S(n))return n.splice(n.getChildrenSize(),0,[a(),...e]),n.select(0,0),void t.remove()}const r=F();r.append(...e),t.replace(r),r.select(0,0)},type:"element"},at={dependencies:[h],export:t=>{if(!m(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExp:/^```(\w{1,10})?\s/,replace:rt((t=>d(t?t[1]:void 0))),type:"element"},ft={dependencies:[C,y],export:(t,e)=>x(t)?st(t,e,0):null,regExp:/^(\s*)[-*+]\s/,replace:it("bullet"),type:"element"},ut={dependencies:[C,y],export:(t,e)=>x(t)?st(t,e,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:it("check"),type:"element"},gt={dependencies:[C,y],export:(t,e)=>x(t)?st(t,e,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:it("number"),type:"element"},pt={format:["code"],tag:"`",type:"text-format"},dt={format:["highlight"],tag:"==",type:"text-format"},mt={format:["bold","italic"],tag:"***",type:"text-format"},ht={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},xt={format:["bold"],tag:"**",type:"text-format"},Tt={format:["bold"],intraword:!1,tag:"__",type:"text-format"},Ct={format:["strikethrough"],tag:"~~",type:"text-format"},yt={format:["italic"],tag:"*",type:"text-format"},vt={format:["italic"],intraword:!1,tag:"_",type:"text-format"},wt={dependencies:[M],export:(t,e,n)=>{if(!R(t))return null;const o=t.getTitle(),i=o?`[${t.getTextContent()}](${t.getURL()} "${o}")`:`[${t.getTextContent()}](${t.getURL()})`,s=t.getFirstChild();return 1===t.getChildrenSize()&&r(s)?n(s,i):i},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,e)=>{const[,n,o,r]=e,i=_(o,{title:r}),s=c(n);s.setFormat(t.getFormat()),i.append(s),t.replace(i)},trigger:")",type:"text-match"},St=[ct,lt,at,ft,gt],bt=[pt,mt,ht,xt,Tt,dt,yt,vt,Ct],Et=[wt],$t=[...St,...bt,...Et];function Ft(t,e=$t,n){return J(e)(t,n)}function Pt(e=$t,n){const o=function(e){const n=A(e),o=n.textFormat.filter((t=>1===t.format.length));return e=>{const r=[],i=(e||t()).getChildren();for(const t of i){const e=z(t,n.element,o,n.textMatch);null!=e&&r.push(e)}return r.join("\n\n")}}(e);return o(n)}export{Ft as $convertFromMarkdownString,Pt as $convertToMarkdownString,mt as BOLD_ITALIC_STAR,ht as BOLD_ITALIC_UNDERSCORE,xt as BOLD_STAR,Tt as BOLD_UNDERSCORE,ut as CHECK_LIST,at as CODE,St as ELEMENT_TRANSFORMERS,ct as HEADING,dt as HIGHLIGHT,pt as INLINE_CODE,yt as ITALIC_STAR,vt as ITALIC_UNDERSCORE,wt as LINK,gt as ORDERED_LIST,lt as QUOTE,Ct as STRIKETHROUGH,bt as TEXT_FORMAT_TRANSFORMERS,Et as TEXT_MATCH_TRANSFORMERS,$t as TRANSFORMERS,ft as UNORDERED_LIST,ot as registerMarkdownShortcuts};
|
|
8
|
+
|
|
9
|
+
import{$getRoot as t,$isElementNode as e,$isDecoratorNode as n,$isLineBreakNode as o,$isTextNode as r,$getSelection as i,$isParagraphNode as s,$createTextNode as c,$createParagraphNode as l,$createLineBreakNode as a,$isRangeSelection as f,$isRootOrShadowRoot as u,$createRangeSelection as g,$setSelection as p}from"lexical";import{$createCodeNode as d,$isCodeNode as m,CodeNode as h}from"@lexical/code";import{$isListNode as x,$isListItemNode as T,ListNode as C,ListItemNode as y,$createListItemNode as v,$createListNode as w}from"@lexical/list";import{$isQuoteNode as b,HeadingNode as S,$isHeadingNode as $,QuoteNode as E,$createQuoteNode as F,$createHeadingNode as P}from"@lexical/rich-text";import{$findMatchingParent as k}from"@lexical/utils";import{LinkNode as M,$isLinkNode as _,$createLinkNode as L}from"@lexical/link";function R(t,e){const n={};for(const o of t){const t=e(o);n[t]?n[t].push(o):n[t]=[o]}return n}function N(t){const e=R(t,(t=>t.type));return{element:e.element||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const j=/[!-/:-@[-`{-~\s]/;function z(t,o,r,i){for(const e of o){const n=e.export(t,(t=>A(t,r,i)));if(null!=n)return n}return e(t)?A(t,r,i):n(t)?t.getTextContent():null}function A(t,i,s){const c=[],l=t.getChildren();t:for(const t of l){for(const e of s){const n=e.export(t,(t=>A(t,i,s)),((t,e)=>B(t,e,i)));if(null!=n){c.push(n);continue t}}o(t)?c.push("\n"):r(t)?c.push(B(t,t.getTextContent(),i)):e(t)?c.push(A(t,i,s)):n(t)&&c.push(t.getTextContent())}return c.join("")}function B(t,e,n){const o=e.trim();let r=o;const i=new Set;for(const e of n){const n=e.format[0],o=e.tag;if(U(t,n)&&!i.has(n)){i.add(n);U(I(t,!0),n)||(r=o+r);U(I(t,!1),n)||(r+=o)}}return e.replace(o,(()=>r))}function I(t,n){let o=n?t.getPreviousSibling():t.getNextSibling();if(!o){const e=t.getParentOrThrow();e.isInline()&&(o=n?e.getPreviousSibling():e.getNextSibling())}for(;o;){if(e(o)){if(!o.isInline())break;const t=n?o.getLastDescendant():o.getFirstDescendant();if(r(t))return t;o=n?o.getPreviousSibling():o.getNextSibling()}if(r(o))return o;if(!e(o))return null}return null}function U(t,e){return r(t)&&t.hasFormat(e)}const D="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,O=D&&"documentMode"in document?document.documentMode:null;D&&"InputEvent"in window&&!O&&new window.InputEvent("input");const K=D&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),V=D&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,W=D&&/^(?=.*Chrome).*/i.test(navigator.userAgent),q=D&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!W,G=/^\s{0,3}$/,H=/^[ \t]*```(\w{1,10})?\s?$/;function J(e){const n=N(e),o=function(t){const e={},n={},o=[],r="(?<![\\\\])";for(const r of t){const{tag:t}=r;e[t]=r;const i=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),n[t]=K||V||q?new RegExp(`(${i})(?![${i}\\s])(.*?[^${i}\\s])${i}(?!${i})`):new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp((K||V||q?"":`${r}`)+"("+o.join("|")+")","g"),transformersByTag:e}}(n.textFormat);return(e,r)=>{const s=e.split("\n"),c=s.length,l=r||t();l.clear();for(let t=0;t<c;t++){const e=s[t],[r,i]=Y(s,t,l);null==r?X(e,l,n.element,o,n.textMatch):t=i}const a=l.getChildren();for(const t of a)Q(t)&&l.getChildrenSize()>1&&t.remove();null!==i()&&l.selectEnd()}}function Q(t){if(!s(t))return!1;const e=t.getFirstChild();return null==e||1===t.getChildrenSize()&&r(e)&&G.test(e.getTextContent())}function X(t,e,n,o,r){const i=t.trim(),f=c(i),u=l();u.append(f),e.append(u);for(const{regExp:e,replace:o}of n){const n=t.match(e);if(n){f.setTextContent(t.slice(n[0].length)),o(u,[f],n,!0);break}}if(Z(f,o,r),u.isAttached()&&i.length>0){const t=u.getPreviousSibling();if(s(t)||b(t)||x(t)){let e=t;if(x(t)){const n=t.getLastDescendant();e=null==n?null:k(n,T)}null!=e&&e.getTextContentSize()>0&&(e.splice(e.getChildrenSize(),0,[a(),...u.getChildren()]),u.remove())}}}function Y(t,e,n){const o=t[e].match(H);if(o){let r=e;const i=t.length;for(;++r<i;){if(t[r].match(H)){const i=d(o[1]),s=c(t.slice(e+1,r).join("\n"));return i.append(s),n.append(i),[i,r]}}}return[null,e]}function Z(t,e,n){const o=t.getTextContent(),r=function(t,e){const n=t.match(e.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=e.fullMatchRegExpByTag[n];if(null==r)continue;const i=t.match(r),s=e.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:e=0}=i,n=t[e-1],o=t[e+i[0].length];if((!n||j.test(n))&&(!o||j.test(o)))return i}}return null}(o,e);if(!r)return void tt(t,n);let i,s,c;if(r[0]===o)i=t;else{const e=r.index||0,n=e+r[0].length;0===e?[i,s]=t.splitText(n):[c,i,s]=t.splitText(e,n)}i.setTextContent(r[2]);const l=e.transformersByTag[r[1]];if(l)for(const t of l.format)i.hasFormat(t)||i.toggleFormat(t);i.hasFormat("code")||Z(i,e,n),c&&Z(c,e,n),s&&Z(s,e,n)}function tt(t,e){let n=t;t:for(;n;){for(const t of e){const o=n.getTextContent().match(t.importRegExp);if(!o)continue;const r=o.index||0,i=r+o[0].length;let s,c;0===r?[s,n]=n.splitText(i):[,s,c]=n.splitText(r,i),c&&tt(c,e),t.replace(s,o);continue t}break}}function et(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var nt=et((function(t){const e=new URLSearchParams;e.append("code",t);for(let t=1;t<arguments.length;t++)e.append("v",arguments[t]);throw Error(`Minified Lexical error #${t}; visit https://lexical.dev/docs/error?${e} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));function ot(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(rt(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function rt(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function it(t,e=Pt){const n=N(e),s=R(n.textFormat,(({tag:t})=>t[t.length-1])),c=R(n.textMatch,(({trigger:t})=>t));for(const n of e){const e=n.type;if("element"===e||"text-match"===e){const e=n.dependencies;for(const n of e)t.hasNode(n)||nt(173,n.getType())}}const l=(t,e,l)=>{(function(t,e,n,o){const r=t.getParent();if(!u(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExp:r,replace:s}of o){const o=i.match(r);if(o&&o[0].length===n){const r=e.getNextSiblings(),[i,c]=e.splitText(n);return i.remove(),s(t,c?[c,...r]:r,o,!1),!0}}return!1})(t,e,l,n.element)||function(t,e,n){let o=t.getTextContent();const r=n[o[e-1]];if(null==r)return!1;e<o.length&&(o=o.slice(0,e));for(const e of r){const n=o.match(e.regExp);if(null===n)continue;const r=n.index||0,i=r+n[0].length;let s;return 0===r?[s]=t.splitText(i):[,s]=t.splitText(r,i),s.selectNext(0,0),e.replace(s,n),!0}return!1}(e,l,c)||function(t,e,n){const s=t.getTextContent(),c=e-1,l=s[c],a=n[l];if(!a)return!1;for(const e of a){const{tag:n}=e,a=n.length,u=c-a+1;if(a>1&&!rt(s,u,n,0,a))continue;if(" "===s[u-1])continue;const d=s[c+1];if(!1===e.intraword&&d&&!j.test(d))continue;const m=t;let h=m,x=ot(s,u,n),T=h;for(;x<0&&(T=T.getPreviousSibling())&&!o(T);)if(r(T)){const t=T.getTextContent();h=T,x=ot(t,t.length,n)}if(x<0)continue;if(h===m&&x+a===u)continue;const C=h.getTextContent();if(x>0&&C[x-1]===l)continue;const y=C[x-1];if(!1===e.intraword&&y&&!j.test(y))continue;const v=m.getTextContent(),w=v.slice(0,u)+v.slice(c+1);m.setTextContent(w);const b=h===m?w:C;h.setTextContent(b.slice(0,x)+b.slice(x+a));const S=i(),$=g();p($);const E=c-a*(h===m?2:1)+1;$.anchor.set(h.__key,x,"text"),$.focus.set(m.__key,E,"text");for(const t of e.format)$.hasFormat(t)||$.formatText(t);$.anchor.set($.focus.key,$.focus.offset,$.focus.type);for(const t of e.format)$.hasFormat(t)&&$.toggleFormat(t);return f(S)&&($.format=S.format),!0}}(e,l,s)};return t.registerUpdateListener((({tags:e,dirtyLeaves:n,editorState:o,prevEditorState:s})=>{if(e.has("collaboration")||e.has("historic"))return;if(t.isComposing())return;const c=o.read(i),a=s.read(i);if(!f(a)||!f(c)||!c.isCollapsed())return;const u=c.anchor.key,g=c.anchor.offset,p=o._nodeMap.get(u);!r(p)||!n.has(u)||1!==g&&g>a.anchor.offset+1||t.update((()=>{if(p.hasFormat("code"))return;const t=p.getParent();null===t||m(t)||l(t,p,c.anchor.offset)}))}))}const st=t=>(e,n,o)=>{const r=t(o);r.append(...n),e.replace(r),r.select(0,0)};const ct=t=>(e,n,o)=>{const r=e.getPreviousSibling(),i=e.getNextSibling(),s=v("check"===t?"x"===o[3]:void 0);if(x(i)&&i.getListType()===t){const t=i.getFirstChild();null!==t?t.insertBefore(s):i.append(s),e.remove()}else if(x(r)&&r.getListType()===t)r.append(s),e.remove();else{const n=w(t,"number"===t?Number(o[2]):void 0);n.append(s),e.replace(n)}s.append(...n),s.select(0,0);const c=function(t){const e=t.match(/\t/g),n=t.match(/ /g);let o=0;return e&&(o+=e.length),n&&(o+=Math.floor(n.length/4)),o}(o[1]);c&&s.setIndent(c)},lt=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(T(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(x(t)){o.push(lt(t,e,n+1));continue}}const r=" ".repeat(4*n),c=t.getListType(),l="number"===c?`${t.getStart()+i}. `:"check"===c?`- [${s.getChecked()?"x":" "}] `:"- ";o.push(r+l+e(s)),i++}return o.join("\n")},at={dependencies:[S],export:(t,e)=>{if(!$(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:/^(#{1,6})\s/,replace:st((t=>{const e="h"+t[1].length;return P(e)})),type:"element"},ft={dependencies:[E],export:(t,e)=>{if(!b(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:/^>\s/,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(b(n))return n.splice(n.getChildrenSize(),0,[a(),...e]),n.select(0,0),void t.remove()}const r=F();r.append(...e),t.replace(r),r.select(0,0)},type:"element"},ut={dependencies:[h],export:t=>{if(!m(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExp:/^[ \t]*```(\w{1,10})?\s/,replace:st((t=>d(t?t[1]:void 0))),type:"element"},gt={dependencies:[C,y],export:(t,e)=>x(t)?lt(t,e,0):null,regExp:/^(\s*)[-*+]\s/,replace:ct("bullet"),type:"element"},pt={dependencies:[C,y],export:(t,e)=>x(t)?lt(t,e,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:ct("check"),type:"element"},dt={dependencies:[C,y],export:(t,e)=>x(t)?lt(t,e,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:ct("number"),type:"element"},mt={format:["code"],tag:"`",type:"text-format"},ht={format:["highlight"],tag:"==",type:"text-format"},xt={format:["bold","italic"],tag:"***",type:"text-format"},Tt={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},Ct={format:["bold"],tag:"**",type:"text-format"},yt={format:["bold"],intraword:!1,tag:"__",type:"text-format"},vt={format:["strikethrough"],tag:"~~",type:"text-format"},wt={format:["italic"],tag:"*",type:"text-format"},bt={format:["italic"],intraword:!1,tag:"_",type:"text-format"},St={dependencies:[M],export:(t,e,n)=>{if(!_(t))return null;const o=t.getTitle(),i=o?`[${t.getTextContent()}](${t.getURL()} "${o}")`:`[${t.getTextContent()}](${t.getURL()})`,s=t.getFirstChild();return 1===t.getChildrenSize()&&r(s)?n(s,i):i},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,e)=>{const[,n,o,r]=e,i=L(o,{title:r}),s=c(n);s.setFormat(t.getFormat()),i.append(s),t.replace(i)},trigger:")",type:"text-match"},$t=[at,ft,ut,gt,dt],Et=[mt,xt,Tt,Ct,yt,ht,wt,bt,vt],Ft=[St],Pt=[...$t,...Et,...Ft];function kt(t,e=Pt,n){return J(e)(t,n)}function Mt(e=Pt,n){const o=function(e){const n=N(e),o=n.textFormat.filter((t=>1===t.format.length));return e=>{const r=[],i=(e||t()).getChildren();for(const t of i){const e=z(t,n.element,o,n.textMatch);null!=e&&r.push(e)}return r.join("\n\n")}}(e);return o(n)}export{kt as $convertFromMarkdownString,Mt as $convertToMarkdownString,xt as BOLD_ITALIC_STAR,Tt as BOLD_ITALIC_UNDERSCORE,Ct as BOLD_STAR,yt as BOLD_UNDERSCORE,pt as CHECK_LIST,ut as CODE,$t as ELEMENT_TRANSFORMERS,at as HEADING,ht as HIGHLIGHT,mt as INLINE_CODE,wt as ITALIC_STAR,bt as ITALIC_UNDERSCORE,St as LINK,dt as ORDERED_LIST,ft as QUOTE,vt as STRIKETHROUGH,Et as TEXT_FORMAT_TRANSFORMERS,Ft as TEXT_MATCH_TRANSFORMERS,Pt as TRANSFORMERS,gt as UNORDERED_LIST,it as registerMarkdownShortcuts};
|
package/package.json
CHANGED
|
@@ -8,17 +8,17 @@
|
|
|
8
8
|
"markdown"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.15.0",
|
|
12
12
|
"main": "LexicalMarkdown.js",
|
|
13
13
|
"types": "index.d.ts",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@lexical/code": "0.
|
|
16
|
-
"@lexical/link": "0.
|
|
17
|
-
"@lexical/list": "0.
|
|
18
|
-
"@lexical/rich-text": "0.
|
|
19
|
-
"@lexical/text": "0.
|
|
20
|
-
"@lexical/utils": "0.
|
|
21
|
-
"lexical": "0.
|
|
15
|
+
"@lexical/code": "0.15.0",
|
|
16
|
+
"@lexical/link": "0.15.0",
|
|
17
|
+
"@lexical/list": "0.15.0",
|
|
18
|
+
"@lexical/rich-text": "0.15.0",
|
|
19
|
+
"@lexical/text": "0.15.0",
|
|
20
|
+
"@lexical/utils": "0.15.0",
|
|
21
|
+
"lexical": "0.15.0"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|