@lexical/markdown 0.2.2 → 0.2.5
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.d.ts +1 -0
- package/LexicalMarkdown.dev.js +220 -1
- package/LexicalMarkdown.js.flow +1 -0
- package/LexicalMarkdown.prod.js +32 -27
- package/package.json +8 -8
package/LexicalMarkdown.d.ts
CHANGED
package/LexicalMarkdown.dev.js
CHANGED
|
@@ -61,51 +61,61 @@ const paragraphStartBase = { ...autoFormatBase,
|
|
|
61
61
|
requiresParagraphStart: true
|
|
62
62
|
};
|
|
63
63
|
const markdownHeader1 = { ...paragraphStartBase,
|
|
64
|
+
export: createHeadingExport(1),
|
|
64
65
|
markdownFormatKind: 'paragraphH1',
|
|
65
66
|
regEx: /^(?:# )/,
|
|
66
67
|
regExForAutoFormatting: /^(?:# )/
|
|
67
68
|
};
|
|
68
69
|
const markdownHeader2 = { ...paragraphStartBase,
|
|
70
|
+
export: createHeadingExport(2),
|
|
69
71
|
markdownFormatKind: 'paragraphH2',
|
|
70
72
|
regEx: /^(?:## )/,
|
|
71
73
|
regExForAutoFormatting: /^(?:## )/
|
|
72
74
|
};
|
|
73
75
|
const markdownHeader3 = { ...paragraphStartBase,
|
|
76
|
+
export: createHeadingExport(3),
|
|
74
77
|
markdownFormatKind: 'paragraphH3',
|
|
75
78
|
regEx: /^(?:### )/,
|
|
76
79
|
regExForAutoFormatting: /^(?:### )/
|
|
77
80
|
};
|
|
78
81
|
const markdownHeader4 = { ...paragraphStartBase,
|
|
82
|
+
export: createHeadingExport(4),
|
|
79
83
|
markdownFormatKind: 'paragraphH4',
|
|
80
84
|
regEx: /^(?:#### )/,
|
|
81
85
|
regExForAutoFormatting: /^(?:#### )/
|
|
82
86
|
};
|
|
83
87
|
const markdownHeader5 = { ...paragraphStartBase,
|
|
88
|
+
export: createHeadingExport(5),
|
|
84
89
|
markdownFormatKind: 'paragraphH5',
|
|
85
90
|
regEx: /^(?:##### )/,
|
|
86
91
|
regExForAutoFormatting: /^(?:##### )/
|
|
87
92
|
};
|
|
88
93
|
const markdownBlockQuote = { ...paragraphStartBase,
|
|
94
|
+
export: blockQuoteExport,
|
|
89
95
|
markdownFormatKind: 'paragraphBlockQuote',
|
|
90
96
|
regEx: /^(?:> )/,
|
|
91
97
|
regExForAutoFormatting: /^(?:> )/
|
|
92
98
|
};
|
|
93
99
|
const markdownUnorderedListDash = { ...paragraphStartBase,
|
|
100
|
+
export: listExport,
|
|
94
101
|
markdownFormatKind: 'paragraphUnorderedList',
|
|
95
102
|
regEx: /^(\s{0,10})(?:- )/,
|
|
96
103
|
regExForAutoFormatting: /^(\s{0,10})(?:- )/
|
|
97
104
|
};
|
|
98
105
|
const markdownUnorderedListAsterisk = { ...paragraphStartBase,
|
|
106
|
+
export: listExport,
|
|
99
107
|
markdownFormatKind: 'paragraphUnorderedList',
|
|
100
108
|
regEx: /^(\s{0,10})(?:\* )/,
|
|
101
109
|
regExForAutoFormatting: /^(\s{0,10})(?:\* )/
|
|
102
110
|
};
|
|
103
111
|
const markdownCodeBlock = { ...paragraphStartBase,
|
|
112
|
+
export: codeBlockExport,
|
|
104
113
|
markdownFormatKind: 'paragraphCodeBlock',
|
|
105
114
|
regEx: /^(```)$/,
|
|
106
115
|
regExForAutoFormatting: /^(```)([a-z]*)( )/
|
|
107
116
|
};
|
|
108
117
|
const markdownOrderedList = { ...paragraphStartBase,
|
|
118
|
+
export: listExport,
|
|
109
119
|
markdownFormatKind: 'paragraphOrderedList',
|
|
110
120
|
regEx: /^(\s{0,10})(\d+)\.\s/,
|
|
111
121
|
regExForAutoFormatting: /^(\s{0,10})(\d+)\.\s/
|
|
@@ -121,26 +131,36 @@ const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
|
|
|
121
131
|
regExForAutoFormatting: /^(?:--- )/
|
|
122
132
|
};
|
|
123
133
|
const markdownInlineCode = { ...autoFormatBase,
|
|
134
|
+
exportFormat: 'code',
|
|
135
|
+
exportTag: '`',
|
|
124
136
|
markdownFormatKind: 'code',
|
|
125
|
-
regEx: /(`)([^`]*)(`)/,
|
|
137
|
+
regEx: /(`)(\s*)([^`]*)(\s*)(`)()/,
|
|
126
138
|
regExForAutoFormatting: /(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/
|
|
127
139
|
};
|
|
128
140
|
const markdownBold = { ...autoFormatBase,
|
|
141
|
+
exportFormat: 'bold',
|
|
142
|
+
exportTag: '**',
|
|
129
143
|
markdownFormatKind: 'bold',
|
|
130
144
|
regEx: /(\*\*)(\s*)([^\*\*]*)(\s*)(\*\*)()/,
|
|
131
145
|
regExForAutoFormatting: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/
|
|
132
146
|
};
|
|
133
147
|
const markdownItalic = { ...autoFormatBase,
|
|
148
|
+
exportFormat: 'italic',
|
|
149
|
+
exportTag: '*',
|
|
134
150
|
markdownFormatKind: 'italic',
|
|
135
151
|
regEx: /(\*)(\s*)([^\*]*)(\s*)(\*)()/,
|
|
136
152
|
regExForAutoFormatting: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/
|
|
137
153
|
};
|
|
138
154
|
const markdownBold2 = { ...autoFormatBase,
|
|
155
|
+
exportFormat: 'bold',
|
|
156
|
+
exportTag: '_',
|
|
139
157
|
markdownFormatKind: 'bold',
|
|
140
158
|
regEx: /(__)(\s*)([^__]*)(\s*)(__)()/,
|
|
141
159
|
regExForAutoFormatting: /(__)(\s*)([^__]*)(\s*)(__)(\s)$/
|
|
142
160
|
};
|
|
143
161
|
const markdownItalic2 = { ...autoFormatBase,
|
|
162
|
+
exportFormat: 'italic',
|
|
163
|
+
exportTag: '_',
|
|
144
164
|
markdownFormatKind: 'italic',
|
|
145
165
|
regEx: /(_)()([^_]*)()(_)()/,
|
|
146
166
|
regExForAutoFormatting: /(_)()([^_]*)()(_)(\s)$/ // Maintain 7 groups.
|
|
@@ -149,11 +169,16 @@ const markdownItalic2 = { ...autoFormatBase,
|
|
|
149
169
|
// the HTML tags for underline.
|
|
150
170
|
|
|
151
171
|
const fakeMarkdownUnderline = { ...autoFormatBase,
|
|
172
|
+
exportFormat: 'underline',
|
|
173
|
+
exportTag: '<u>',
|
|
174
|
+
exportTagClose: '</u>',
|
|
152
175
|
markdownFormatKind: 'underline',
|
|
153
176
|
regEx: /(\<u\>)(\s*)([^\<]*)(\s*)(\<\/u\>)()/,
|
|
154
177
|
regExForAutoFormatting: /(\<u\>)(\s*\b)([^\<]*)(\b\s*)(\<\/u\>)(\s)$/
|
|
155
178
|
};
|
|
156
179
|
const markdownStrikethrough = { ...autoFormatBase,
|
|
180
|
+
exportFormat: 'strikethrough',
|
|
181
|
+
exportTag: '~~',
|
|
157
182
|
markdownFormatKind: 'strikethrough',
|
|
158
183
|
regEx: /(~~)(\s*)([^~~]*)(\s*)(~~)()/,
|
|
159
184
|
regExForAutoFormatting: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/
|
|
@@ -851,6 +876,58 @@ function selectAfterFinalCaptureGroup(scanningContext, parentElementNode) {
|
|
|
851
876
|
}
|
|
852
877
|
}
|
|
853
878
|
|
|
879
|
+
function createHeadingExport(level) {
|
|
880
|
+
return (node, exportChildren) => {
|
|
881
|
+
return richText.$isHeadingNode(node) && node.getTag() === 'h' + level ? '#'.repeat(level) + ' ' + exportChildren(node) : null;
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
function listExport(node, exportChildren) {
|
|
886
|
+
return list.$isListNode(node) ? processNestedLists(node, exportChildren, 0) : null;
|
|
887
|
+
} // TODO: should be param
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
const LIST_INDENT_SIZE = 4;
|
|
891
|
+
|
|
892
|
+
function processNestedLists(listNode, exportChildren, depth) {
|
|
893
|
+
const output = [];
|
|
894
|
+
const children = listNode.getChildren();
|
|
895
|
+
let index = 0;
|
|
896
|
+
|
|
897
|
+
for (const listItemNode of children) {
|
|
898
|
+
if (list.$isListItemNode(listItemNode)) {
|
|
899
|
+
if (listItemNode.getChildrenSize() === 1) {
|
|
900
|
+
const firstChild = listItemNode.getFirstChild();
|
|
901
|
+
|
|
902
|
+
if (list.$isListNode(firstChild)) {
|
|
903
|
+
output.push(processNestedLists(firstChild, exportChildren, depth + 1));
|
|
904
|
+
continue;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
const indent = ' '.repeat(depth * LIST_INDENT_SIZE);
|
|
909
|
+
const prefix = listNode.getTag() === 'ul' ? '- ' : `${listNode.getStart() + index}. `;
|
|
910
|
+
output.push(indent + prefix + exportChildren(listItemNode));
|
|
911
|
+
index++;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
return output.join('\n');
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
function blockQuoteExport(node, exportChildren) {
|
|
919
|
+
return richText.$isQuoteNode(node) ? '> ' + exportChildren(node) : null;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
function codeBlockExport(node, exportChildren) {
|
|
923
|
+
if (!code.$isCodeNode(node)) {
|
|
924
|
+
return null;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
const textContent = node.getTextContent();
|
|
928
|
+
return '```' + (node.getLanguage() || '') + (textContent ? '\n' + textContent : '') + '\n' + '```';
|
|
929
|
+
}
|
|
930
|
+
|
|
854
931
|
/**
|
|
855
932
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
856
933
|
*
|
|
@@ -1240,6 +1317,147 @@ function getLastTextNodeInElementNode(elementNode) {
|
|
|
1240
1317
|
return null;
|
|
1241
1318
|
}
|
|
1242
1319
|
|
|
1320
|
+
/**
|
|
1321
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1322
|
+
*
|
|
1323
|
+
* This source code is licensed under the MIT license found in the
|
|
1324
|
+
* LICENSE file in the root directory of this source tree.
|
|
1325
|
+
*
|
|
1326
|
+
*
|
|
1327
|
+
*/
|
|
1328
|
+
function $convertToMarkdownString() {
|
|
1329
|
+
const output = [];
|
|
1330
|
+
const children = lexical.$getRoot().getChildren();
|
|
1331
|
+
|
|
1332
|
+
for (const child of children) {
|
|
1333
|
+
const result = exportTopLevelElementOrDecorator(child);
|
|
1334
|
+
|
|
1335
|
+
if (result != null) {
|
|
1336
|
+
output.push(result);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
return output.join('\n');
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
function exportTopLevelElementOrDecorator(node) {
|
|
1344
|
+
const blockTransformers = getAllMarkdownCriteriaForParagraphs();
|
|
1345
|
+
|
|
1346
|
+
for (const transformer of blockTransformers) {
|
|
1347
|
+
if (transformer.export != null) {
|
|
1348
|
+
const result = transformer.export(node, _node => exportChildren(_node));
|
|
1349
|
+
|
|
1350
|
+
if (result != null) {
|
|
1351
|
+
return result;
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
return lexical.$isElementNode(node) ? exportChildren(node) : null;
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
function exportChildren(node) {
|
|
1360
|
+
const output = [];
|
|
1361
|
+
const children = node.getChildren();
|
|
1362
|
+
|
|
1363
|
+
for (const child of children) {
|
|
1364
|
+
if (lexical.$isLineBreakNode(child)) {
|
|
1365
|
+
output.push('\n');
|
|
1366
|
+
} else if (lexical.$isTextNode(child)) {
|
|
1367
|
+
output.push(exportTextNode(child, child.getTextContent()));
|
|
1368
|
+
} else if (link.$isLinkNode(child)) {
|
|
1369
|
+
const linkContent = `[${child.getTextContent()}](${child.getURL()})`;
|
|
1370
|
+
const firstChild = child.getFirstChild(); // Add text styles only if link has single text node inside. If it's more
|
|
1371
|
+
// then one we either ignore it and have single <a> to cover whole link,
|
|
1372
|
+
// or process them, but then have link cut into multiple <a>.
|
|
1373
|
+
// For now chosing the first option.
|
|
1374
|
+
|
|
1375
|
+
if (child.getChildrenSize() === 1 && lexical.$isTextNode(firstChild)) {
|
|
1376
|
+
output.push(exportTextNode(firstChild, linkContent));
|
|
1377
|
+
} else {
|
|
1378
|
+
output.push(linkContent);
|
|
1379
|
+
}
|
|
1380
|
+
} else if (lexical.$isElementNode(child)) {
|
|
1381
|
+
output.push(exportChildren(child));
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
return output.join('');
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
function exportTextNode(node, textContent, parentNode) {
|
|
1389
|
+
let output = textContent;
|
|
1390
|
+
const applied = new Set();
|
|
1391
|
+
const textTransformers = getAllMarkdownCriteriaForTextNodes();
|
|
1392
|
+
|
|
1393
|
+
for (const transformer of textTransformers) {
|
|
1394
|
+
const {
|
|
1395
|
+
exportFormat: format,
|
|
1396
|
+
exportTag: tag,
|
|
1397
|
+
exportTagClose: tagClose = tag
|
|
1398
|
+
} = transformer;
|
|
1399
|
+
|
|
1400
|
+
if (format != null && tag != null && tagClose != null && hasFormat(node, format) && !applied.has(format)) {
|
|
1401
|
+
// Multiple tags might be used for the same format (*, _)
|
|
1402
|
+
applied.add(format); // Prevent adding extra wrapping tags if it's already
|
|
1403
|
+
// added by a previous sibling (or will be closed by the next one)
|
|
1404
|
+
|
|
1405
|
+
const previousNode = getTextSibling(node, true);
|
|
1406
|
+
|
|
1407
|
+
if (!hasFormat(previousNode, format)) {
|
|
1408
|
+
output = tag + output;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
const nextNode = getTextSibling(node, false);
|
|
1412
|
+
|
|
1413
|
+
if (!hasFormat(nextNode, format)) {
|
|
1414
|
+
output += tagClose;
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
return output;
|
|
1420
|
+
} // Finds text sibling including cases for inline elements
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
function getTextSibling(node, backward) {
|
|
1424
|
+
let sibling = backward ? node.getPreviousSibling() : node.getNextSibling();
|
|
1425
|
+
|
|
1426
|
+
if (!sibling) {
|
|
1427
|
+
const parent = node.getParentOrThrow();
|
|
1428
|
+
|
|
1429
|
+
if (parent.isInline()) {
|
|
1430
|
+
sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling();
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
while (sibling) {
|
|
1435
|
+
if (lexical.$isElementNode(sibling)) {
|
|
1436
|
+
if (!sibling.isInline()) {
|
|
1437
|
+
break;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
const descendant = backward ? sibling.getLastDescendant() : sibling.getFirstDescendant();
|
|
1441
|
+
|
|
1442
|
+
if (lexical.$isTextNode(descendant)) {
|
|
1443
|
+
return descendant;
|
|
1444
|
+
} else {
|
|
1445
|
+
sibling = backward ? sibling.getPreviousSibling() : sibling.getNextSibling();
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
if (lexical.$isTextNode(sibling)) {
|
|
1450
|
+
return sibling;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
return null;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
function hasFormat(node, format) {
|
|
1458
|
+
return lexical.$isTextNode(node) && node.hasFormat(format);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1243
1461
|
/**
|
|
1244
1462
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1245
1463
|
*
|
|
@@ -1279,4 +1497,5 @@ function $convertFromMarkdownString(markdownString, editor, createHorizontalRule
|
|
|
1279
1497
|
}
|
|
1280
1498
|
|
|
1281
1499
|
exports.$convertFromMarkdownString = $convertFromMarkdownString;
|
|
1500
|
+
exports.$convertToMarkdownString = $convertToMarkdownString;
|
|
1282
1501
|
exports.registerMarkdownShortcuts = registerMarkdownShortcuts;
|
package/LexicalMarkdown.js.flow
CHANGED
package/LexicalMarkdown.prod.js
CHANGED
|
@@ -4,37 +4,42 @@
|
|
|
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
|
|
8
|
-
const z=[{triggerKind:"space_trigger",triggerString:" "}],A={markdownFormatKind:null,regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:!1},B={...A,requiresParagraphStart:!0},
|
|
7
|
+
var h=require("@lexical/code"),n=require("@lexical/list"),p=require("lexical"),u=require("@lexical/link"),v=require("@lexical/rich-text"),w=require("@lexical/text");function y(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
|
|
8
|
+
const z=[{triggerKind:"space_trigger",triggerString:" "}],A={markdownFormatKind:null,regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:!1},B={...A,requiresParagraphStart:!0},D={...B,export:C(1),markdownFormatKind:"paragraphH1",regEx:/^(?:# )/,regExForAutoFormatting:/^(?:# )/},E={...B,export:C(2),markdownFormatKind:"paragraphH2",regEx:/^(?:## )/,regExForAutoFormatting:/^(?:## )/},aa={...B,export:C(3),markdownFormatKind:"paragraphH3",regEx:/^(?:### )/,regExForAutoFormatting:/^(?:### )/},
|
|
9
|
+
ba={...B,export:C(4),markdownFormatKind:"paragraphH4",regEx:/^(?:#### )/,regExForAutoFormatting:/^(?:#### )/},ca={...B,export:C(5),markdownFormatKind:"paragraphH5",regEx:/^(?:##### )/,regExForAutoFormatting:/^(?:##### )/},F={...B,export:da,markdownFormatKind:"paragraphCodeBlock",regEx:/^(```)$/,regExForAutoFormatting:/^(```)([a-z]*)( )/},G=[{...A,markdownFormatKind:"strikethrough_italic_bold",regEx:/(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)()/,regExForAutoFormatting:/(~~_\*\*)(\s*\b)([^~~_\*\*][^\*\*_~~]*)(\b\s*)(\*\*_~~)(\s)$/},
|
|
9
10
|
{...A,markdownFormatKind:"italic_bold",regEx:/(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)/,regExForAutoFormatting:/(_\*\*)(\s*\b)([^_\*\*][^\*\*_]*)(\b\s*)(\*\*_)(\s)$/},{...A,markdownFormatKind:"strikethrough_italic",regEx:/(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)/,regExForAutoFormatting:/(~~_)(\s*)([^~~_][^_~~]*)(\s*)(_~~)(\s)$/},{...A,markdownFormatKind:"strikethrough_bold",regEx:/(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)/,regExForAutoFormatting:/(~~\*\*)(\s*\b)([^~~\*\*][^\*\*~~]*)(\b\s*)(\*\*~~)(\s)$/},
|
|
10
|
-
{...A,markdownFormatKind:"code",regEx:/(`)([^`]*)(`)/,regExForAutoFormatting:/(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/},{...A,markdownFormatKind:"bold",regEx:/(\*\*)(\s*)([^\*\*]*)(\s*)(\*\*)()/,regExForAutoFormatting:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/},{...A,markdownFormatKind:"italic",regEx:/(\*)(\s*)([^\*]*)(\s*)(\*)()/,regExForAutoFormatting:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/},
|
|
11
|
-
{...A,
|
|
12
|
-
|
|
13
|
-
regEx:/^(
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
function
|
|
17
|
-
function
|
|
18
|
-
function
|
|
19
|
-
case "paragraphBlockQuote":b=v.$createQuoteNode();b.append(...e);break;case "paragraphUnorderedList":return
|
|
20
|
-
!0!==a.isWithinCodeBlock,a.currentElementNode=null,{newNode:null,shouldDelete:!0};if(a.isWithinCodeBlock){if(null==a.currentElementNode)return d=
|
|
11
|
+
{...A,exportFormat:"code",exportTag:"`",markdownFormatKind:"code",regEx:/(`)(\s*)([^`]*)(\s*)(`)()/,regExForAutoFormatting:/(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/},{...A,exportFormat:"bold",exportTag:"**",markdownFormatKind:"bold",regEx:/(\*\*)(\s*)([^\*\*]*)(\s*)(\*\*)()/,regExForAutoFormatting:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*)(\s)$/},{...A,exportFormat:"italic",exportTag:"*",markdownFormatKind:"italic",regEx:/(\*)(\s*)([^\*]*)(\s*)(\*)()/,regExForAutoFormatting:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*)(\s)$/},
|
|
12
|
+
{...A,exportFormat:"bold",exportTag:"_",markdownFormatKind:"bold",regEx:/(__)(\s*)([^__]*)(\s*)(__)()/,regExForAutoFormatting:/(__)(\s*)([^__]*)(\s*)(__)(\s)$/},{...A,exportFormat:"italic",exportTag:"_",markdownFormatKind:"italic",regEx:/(_)()([^_]*)()(_)()/,regExForAutoFormatting:/(_)()([^_]*)()(_)(\s)$/},{...A,exportFormat:"underline",exportTag:"<u>",exportTagClose:"</u>",markdownFormatKind:"underline",regEx:/(<u>)(\s*)([^<]*)(\s*)(<\/u>)()/,regExForAutoFormatting:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)(\s)$/},
|
|
13
|
+
{...A,exportFormat:"strikethrough",exportTag:"~~",markdownFormatKind:"strikethrough",regEx:/(~~)(\s*)([^~~]*)(\s*)(~~)()/,regExForAutoFormatting:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~)(\s)$/},{...A,markdownFormatKind:"link",regEx:/(\[)([^\]]*)(\]\()([^)]*)(\)*)()/,regExForAutoFormatting:/(\[)([^\]]*)(\]\()([^)]*)(\)*)(\s)$/}],I=[D,E,aa,ba,ca,{...B,export:ea,markdownFormatKind:"paragraphBlockQuote",regEx:/^(?:> )/,regExForAutoFormatting:/^(?:> )/},{...B,export:H,markdownFormatKind:"paragraphUnorderedList",
|
|
14
|
+
regEx:/^(\s{0,10})(?:- )/,regExForAutoFormatting:/^(\s{0,10})(?:- )/},{...B,export:H,markdownFormatKind:"paragraphUnorderedList",regEx:/^(\s{0,10})(?:\* )/,regExForAutoFormatting:/^(\s{0,10})(?:\* )/},{...B,export:H,markdownFormatKind:"paragraphOrderedList",regEx:/^(\s{0,10})(\d+)\.\s/,regExForAutoFormatting:/^(\s{0,10})(\d+)\.\s/},F,{...B,markdownFormatKind:"horizontalRule",regEx:/^(?:\*\*\*)$/,regExForAutoFormatting:/^(?:\*\*\* )/},{...B,markdownFormatKind:"horizontalRule",regEx:/^(?:---)$/,regExForAutoFormatting:/^(?:--- )/}],
|
|
15
|
+
fa=[...I,...G];function J(a,d,c,b){return{currentElementNode:null,editor:a,isAutoFormatting:d,isWithinCodeBlock:!1,joinedText:null,markdownCriteria:{markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null},patternMatchResults:{regExCaptureGroups:[]},textNodeWithOffset:c,triggerState:b}}
|
|
16
|
+
function K(a){a.joinedText=null;a.markdownCriteria={markdownFormatKind:"noTransformation",regEx:/(?:)/,regExForAutoFormatting:/(?:)/,requiresParagraphStart:null};a.patternMatchResults={regExCaptureGroups:[]};a.triggerState=null;a.textNodeWithOffset=null;return a}
|
|
17
|
+
function L(a,d,c){if(!0===a.requiresParagraphStart)return c=M(d),null===c.node.getPreviousSibling()?(c=c.node.getTextContent(),a=N(c,!0,!1,d.isAutoFormatting?a.regExForAutoFormatting:a.regEx)):a=null,a;null==d.joinedText&&(p.$isElementNode(c)?null==d.joinedText&&(d.joinedText=w.$joinTextNodesInElementNode(c,"\u0004",M(d))):y(52,c.__key));return N(d.joinedText,!1,!0===a.regExForAutoFormatting,d.isAutoFormatting?a.regExForAutoFormatting:a.regEx)}
|
|
18
|
+
function N(a,d,c,b){const e={regExCaptureGroups:[]};b=a.match(b);if(null!==b&&0<b.length&&(!1===d||0===b.index)&&(!1===c||b.index+b[0].length===a.length)){a=b.length;d=b.index;for(c=0;c<a;c++){const f=b[c];e.regExCaptureGroups.push({offsetInParent:d,text:f});0<c&&(d+=f.length)}return e}return null}function M(a){a=a.textNodeWithOffset;null==a&&y(82);return a}
|
|
19
|
+
function ha(a,d,c){var b=null,e=d.getChildren();const f=a.markdownCriteria,g=a.patternMatchResults;if(null!=f.markdownFormatKind)switch(f.markdownFormatKind){case "paragraphH1":b=v.$createHeadingNode("h1");b.append(...e);break;case "paragraphH2":b=v.$createHeadingNode("h2");b.append(...e);break;case "paragraphH3":b=v.$createHeadingNode("h3");b.append(...e);break;case "paragraphH4":b=v.$createHeadingNode("h4");b.append(...e);break;case "paragraphH5":b=v.$createHeadingNode("h5");b.append(...e);break;
|
|
20
|
+
case "paragraphBlockQuote":b=v.$createQuoteNode();b.append(...e);break;case "paragraphUnorderedList":return O(d,e,g,"ul"),{newNode:null,shouldDelete:!1};case "paragraphOrderedList":return b=1<g.regExCaptureGroups.length?g.regExCaptureGroups[g.regExCaptureGroups.length-1].text:"1",a=a.isAutoFormatting?parseInt(b,10):void 0,O(d,e,g,"ol",a),{newNode:null,shouldDelete:!1};case "paragraphCodeBlock":if(!1===a.isAutoFormatting){if(0<a.patternMatchResults.regExCaptureGroups.length)return a.isWithinCodeBlock=
|
|
21
|
+
!0!==a.isWithinCodeBlock,a.currentElementNode=null,{newNode:null,shouldDelete:!0};if(a.isWithinCodeBlock){if(null==a.currentElementNode)return d=h.$createCodeNode(),d.append(...e),a.currentElementNode=d,{newNode:d,shouldDelete:!1};null!=a.currentElementNode&&(d=a.currentElementNode,a=p.$createLineBreakNode(),d.append(a),e.length&&d.append(a),d.append(...e))}return{newNode:null,shouldDelete:!0}}null!=a.triggerState&&a.triggerState.isCodeBlock?b=p.$createParagraphNode():(b=h.$createCodeNode(),d=3<=
|
|
21
22
|
g.regExCaptureGroups.length?g.regExCaptureGroups[2].text:null,null!=d&&0<d.length&&b.setLanguage(d));b.append(...e);break;case "horizontalRule":null!=c&&(e=c(),d.insertBefore(e))}return{newNode:b,shouldDelete:!1}}
|
|
22
|
-
function
|
|
23
|
-
function
|
|
23
|
+
function O(a,d,c,b,e){const f=n.$createListItemNode();c=(c=c.regExCaptureGroups[0].text.match(/^\s*/))?Math.floor(c[0].length/4):0;f.append(...d);d=a.getPreviousSibling();n.$isListNode(d)&&d.getTag()===b?(d.append(f),a.remove()):(b=n.$createListNode(b,e),b.append(f),a.replace(b));c&&f.setIndent(c)}
|
|
24
|
+
function P(a,d,c){if(!0===a.markdownCriteria.requiresParagraphStart){if(null!=a.textNodeWithOffset){var b=M(a);0<a.patternMatchResults.regExCaptureGroups.length&&(b=b.node.spliceText(0,a.patternMatchResults.regExCaptureGroups[0].text.length,"",!0),""===b.getTextContent()&&(b.selectPrevious(),b.remove()))}const {newNode:g,shouldDelete:k}=ha(a,d,c);k?d.remove():null!==g&&d.replace(g)}else if(c=a.markdownCriteria,null!=c.markdownFormatKind)if(b=ia(c.markdownFormatKind),null!=b){if(c=b,7===a.patternMatchResults.regExCaptureGroups.length){Q(5,
|
|
24
25
|
5,a,d);Q(1,1,a,d);b=a.patternMatchResults.regExCaptureGroups;3<b.length||y(65);if(0!==b[3].text.length&&(b=R(3,3,!1,!0,a,d),null!=b&&(p.$setSelection(b),b=p.$getSelection(),p.$isRangeSelection(b))))for(var e=0;e<c.length;e++)b.formatText(c[e]);S(a,d)}}else if("link"===c.markdownFormatKind&&(c=a.patternMatchResults.regExCaptureGroups,7===c.length&&(e=c[2].text,c=c[4].text,0!==e.length&&0!==c.length))){Q(1,5,a,d);b=a.patternMatchResults.regExCaptureGroups;if(!(1>=b.length)){e={offsetInParent:b[1].offsetInParent,
|
|
25
26
|
text:e};var f=R(1,1,!1,!1,a,d);if(null!=f&&(p.$setSelection(f),f=p.$getSelection(),null!=f&&p.$isRangeSelection(f)&&f.isCollapsed())){f.insertText(e.text);b.splice(1,0,e);e=e.text.length;f=b.length;for(let g=2;g<f;g++)b[g].offsetInParent+=e}}b=R(1,1,!1,!0,a,d);null!=b&&(p.$setSelection(b),a.editor.dispatchCommand(u.TOGGLE_LINK_COMMAND,c),S(a,d))}}
|
|
26
|
-
function
|
|
27
|
-
function R(a,d,c,b,e,f){var g=e.patternMatchResults;e=g.regExCaptureGroups;var
|
|
27
|
+
function ia(a){switch(a){case "italic":case "bold":case "underline":case "strikethrough":case "code":return[a];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}
|
|
28
|
+
function R(a,d,c,b,e,f){var g=e.patternMatchResults;e=g.regExCaptureGroups;var k=e.length;if(a>=k||d>=k)return null;k=g.regExCaptureGroups.length;2>k?g=0:(--k,g=g.regExCaptureGroups[k].offsetInParent+g.regExCaptureGroups[k].text.length);a=e[a];d=e[d];b=b?d.offsetInParent+d.text.length:d.offsetInParent;c=w.$findNodeWithOffsetFromJoinedText(c?a.offsetInParent+a.text.length:a.offsetInParent,g,1,f);b=w.$findNodeWithOffsetFromJoinedText(b,g,1,f);if(null==c&&null==b&&0===f.getChildren().length)return c=
|
|
28
29
|
p.$createRangeSelection(),c.anchor.set(f.getKey(),0,"element"),c.focus.set(f.getKey(),0,"element"),c;if(null==c||null==b)return null;f=p.$createRangeSelection();f.anchor.set(c.node.getKey(),c.offset,"text");f.focus.set(b.node.getKey(),b.offset,"text");return f}
|
|
29
30
|
function Q(a,d,c,b){const e=c.patternMatchResults.regExCaptureGroups;c=R(a,d,!1,!0,c,b);if(null!=c&&(p.$setSelection(c),c=p.$getSelection(),null!=c&&p.$isRangeSelection(c)&&!1===c.isCollapsed())){c.removeText();c=0;b=e.length;for(let f=a;f<b;f++){const g=e[f];f>a&&(g.offsetInParent-=c);f<=d&&(c+=g.text.length,g.text="")}}}function S(a,d){var c=a.patternMatchResults.regExCaptureGroups.length;2>c||(--c,a=R(c,c,!0,!0,a,d),null!=a&&p.$setSelection(a))}
|
|
30
|
-
function
|
|
31
|
-
function
|
|
31
|
+
function C(a){return(d,c)=>v.$isHeadingNode(d)&&d.getTag()==="h"+a?"#".repeat(a)+" "+c(d):null}function H(a,d){return n.$isListNode(a)?T(a,d,0):null}function T(a,d,c){const b=[];var e=a.getChildren();let f=0;for(const g of e)if(n.$isListItemNode(g)){if(1===g.getChildrenSize()&&(e=g.getFirstChild(),n.$isListNode(e))){b.push(T(e,d,c+1));continue}e=" ".repeat(4*c);const k="ul"===a.getTag()?"- ":`${a.getStart()+f}. `;b.push(e+k+d(g));f++}return b.join("\n")}
|
|
32
|
+
function ea(a,d){return v.$isQuoteNode(a)?"> "+d(a):null}function da(a){if(!h.$isCodeNode(a))return null;const d=a.getTextContent();return"```"+(a.getLanguage()||"")+(d?"\n"+d:"")+"\n```"}function ja(a,d,c){a.update(()=>{const b=M(d).node.getParentOrThrow();P(d,b,c)},{tag:"history-push"})}
|
|
33
|
+
function ka(a,d){let c=null;a.getEditorState().read(()=>{var b=p.$getSelection();if(p.$isRangeSelection(b)){var e=b.anchor.getNode();b=p.$isTextNode(e)?{node:e,offset:b.anchor.offset}:null}else b=null;if(null!==b){b=J(a,!0,b,d);a:{e=!1===d.isParentAListItemNode?fa:G;const f=b.triggerState,g=e.length;for(let k=0;k<g;k++){const l=e[k];if(null!=f&&!1===f.isCodeBlock||"paragraphCodeBlock"===l.markdownFormatKind){const m=L(l,b,M(b).node.getParentOrThrow());if(null!=m){e={markdownCriteria:l,patternMatchResults:m};
|
|
32
34
|
break a}}}e={markdownCriteria:null,patternMatchResults:null}}null!==e.markdownCriteria&&null!==e.patternMatchResults&&(c=b,c.markdownCriteria=e.markdownCriteria,c.patternMatchResults=e.patternMatchResults)}});return c}
|
|
33
|
-
function
|
|
34
|
-
function
|
|
35
|
-
g.length)break a;if(g!==e){e=g;m=t;f=!1;break}}}}}else for(d=d.getChildren(),b=d.length,e=0;e<b;e++)f=d[e],p.$isElementNode(f)&&
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
exports
|
|
40
|
-
|
|
35
|
+
function la(a){let d=null;a.read(()=>{const c=p.$getSelection();if(p.$isRangeSelection(c)&&c.isCollapsed()){var b=c.anchor.getNode(),e=b.getParent(),f=n.$isListItemNode(e);d={anchorOffset:c.anchor.offset,hasParentNode:null!==e,isCodeBlock:h.$isCodeNode(b),isParentAListItemNode:f,isSelectionCollapsed:!0,isSimpleText:p.$isTextNode(b)&&b.isSimpleText(),nodeKey:b.getKey(),textContent:b.getTextContent()}}});return d}
|
|
36
|
+
function U(a,d,c){var b=d.getFirstChild();if(p.$isTextNode(b))a:{K(a);b=G.length;var e=d.getTextContent(),f=0===e.length;let m=0;for(;!f;){f=!0;for(let t=m;t<b;t++){var g=G[t];if(null==a.textNodeWithOffset){b:{var k=d.getChildren();var l=k.length;for(--l;0<=l;l--)if(p.$isTextNode(k[l])){k=k[l];break b}k=null}if(null==k)break a;a.textNodeWithOffset={node:k,offset:k.getTextContent().length}}k=L(g,a,d);if(null!=k){a.markdownCriteria=g;a.patternMatchResults=k;P(a,d,c);K(a);g=d.getTextContent();if(0===
|
|
37
|
+
g.length)break a;if(g!==e){e=g;m=t;f=!1;break}}}}}else for(d=d.getChildren(),b=d.length,e=0;e<b;e++)f=d[e],p.$isElementNode(f)&&U(a,f,c)}function ma(a){for(const d of I)if(null!=d.export){const c=d.export(a,b=>V(b));if(null!=c)return c}return p.$isElementNode(a)?V(a):null}
|
|
38
|
+
function V(a){const d=[];a=a.getChildren();for(const c of a)if(p.$isLineBreakNode(c))d.push("\n");else if(p.$isTextNode(c))d.push(W(c,c.getTextContent()));else if(u.$isLinkNode(c)){a=`[${c.getTextContent()}](${c.getURL()})`;const b=c.getFirstChild();1===c.getChildrenSize()&&p.$isTextNode(b)?d.push(W(b,a)):d.push(a)}else p.$isElementNode(c)&&d.push(V(c));return d.join("")}
|
|
39
|
+
function W(a,d){const c=new Set;for(const e of G){const {exportFormat:f,exportTag:g,exportTagClose:k=g}=e;if(null!=f&&null!=g&&null!=k&&Y(a,f)&&!c.has(f)){c.add(f);var b=Z(a,!0);Y(b,f)||(d=g+d);b=Z(a,!1);Y(b,f)||(d+=k)}}return d}
|
|
40
|
+
function Z(a,d){let c=d?a.getPreviousSibling():a.getNextSibling();c||(a=a.getParentOrThrow(),a.isInline()&&(c=d?a.getPreviousSibling():a.getNextSibling()));for(;c;){if(p.$isElementNode(c)){if(!c.isInline())break;a=d?c.getLastDescendant():c.getFirstDescendant();if(p.$isTextNode(a))return a;c=d?c.getPreviousSibling():c.getNextSibling()}if(p.$isTextNode(c))return c}return null}function Y(a,d){return p.$isTextNode(a)&&a.hasFormat(d)}
|
|
41
|
+
exports.$convertFromMarkdownString=function(a,d,c){if(a.length){var b=[];a=a.split("\n");var e=a.length;for(var f=0;f<e;f++)0<a[f].length?b.push(p.$createParagraphNode().append(p.$createTextNode(a[f]))):b.push(p.$createParagraphNode());b.length?(a=p.$getRoot(),a.clear(),a.append(...b),b=a):b=null}else b=null;if(null!=b){d=J(d,!1,null,null);b=p.$getRoot();a=!1;for(e=0;!a;){a=!0;var g=b.getChildren(),k=g.length;for(var l=e;l<k;l++){var m=g[l];if(p.$isElementNode(m)){f=d;var t=c;var r=m.getTextContent();
|
|
42
|
+
if(p.$isParagraphNode(m)){var q=m.getFirstChild(),x=p.$isTextNode(q);if(!0===f.isWithinCodeBlock)null!=q&&x&&(f.textNodeWithOffset={node:q,offset:0},q=F,r=N(r,!0,!1,f.isAutoFormatting?q.regExForAutoFormatting:q.regEx),null!=r&&(f.patternMatchResults=r)),f.markdownCriteria=F,P(f,m,t);else if(m.getChildren().length)for(r=I.length,f.joinedText=m.getTextContent(),null!=q&&x||y(80),f.textNodeWithOffset={node:q,offset:0},q=0;q<r;q++){x=I[q];if(!1===x.requiresParagraphStart)break;const X=L(x,f,M(f).node.getParentOrThrow());
|
|
43
|
+
if(null!=X){f.markdownCriteria=x;f.patternMatchResults=X;P(f,m,t);break}}}}K(d);if(b.getChildren().length!==k){e=l;a=!1;break}}}a=!1;for(e=0;!a;)for(a=!0,f=b.getChildren(),g=f.length,k=e;k<g;k++)l=f[k],p.$isElementNode(l)&&U(d,l,c),K(d)}};exports.$convertToMarkdownString=function(){const a=[];var d=p.$getRoot().getChildren();for(const c of d)d=ma(c),null!=d&&a.push(d);return a.join("\n")};
|
|
44
|
+
exports.registerMarkdownShortcuts=function(a,d){let c=null;return a.registerUpdateListener(({tags:b})=>{if(!1===b.has("historic")){b=la(a.getEditorState());if(null==b)var e=null;else a:{e=b;var f=c;if(null==e||null==f)e=null;else{var g=z.length;for(let k=0;k<g;k++){const l=z[k].triggerString,m=l.length,t=e.textContent.length,r=e.anchorOffset-m;if(!1===(!0===e.hasParentNode&&e.isSimpleText&&e.isSelectionCollapsed&&e.anchorOffset!==f.anchorOffset&&0<=r&&r+m<=t&&e.textContent.substr(r,m)===l&&e.textContent!==
|
|
45
|
+
f.textContent)){e=null;break a}}e=ka(a,e)}}null!=e&&ja(a,e,d);c=b}else c=null})};
|
package/package.json
CHANGED
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
"markdown"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.2.
|
|
11
|
+
"version": "0.2.5",
|
|
12
12
|
"main": "LexicalMarkdown.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.2.
|
|
14
|
+
"lexical": "0.2.5"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.2.
|
|
18
|
-
"@lexical/code": "0.2.
|
|
19
|
-
"@lexical/text": "0.2.
|
|
20
|
-
"@lexical/rich-text": "0.2.
|
|
21
|
-
"@lexical/list": "0.2.
|
|
22
|
-
"@lexical/link": "0.2.
|
|
17
|
+
"@lexical/utils": "0.2.5",
|
|
18
|
+
"@lexical/code": "0.2.5",
|
|
19
|
+
"@lexical/text": "0.2.5",
|
|
20
|
+
"@lexical/rich-text": "0.2.5",
|
|
21
|
+
"@lexical/list": "0.2.5",
|
|
22
|
+
"@lexical/link": "0.2.5"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|