forked from facebook/lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownImport.ts
321 lines (285 loc) · 10.2 KB
/
MarkdownImport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers';
import {$isListItemNode, $isListNode, ListItemNode} from '@lexical/list';
import {$isQuoteNode} from '@lexical/rich-text';
import {$findMatchingParent} from '@lexical/utils';
import {
$createLineBreakNode,
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isParagraphNode,
ElementNode,
} from 'lexical';
import {IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI} from 'shared/environment';
import {importTextTransformers} from './importTextTransformers';
import {isEmptyParagraph, transformersByType} from './utils';
export type TextFormatTransformersIndex = Readonly<{
fullMatchRegExpByTag: Readonly<Record<string, RegExp>>;
openTagsRegExp: RegExp;
transformersByTag: Readonly<Record<string, TextFormatTransformer>>;
}>;
/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*/
export function createMarkdownImport(
transformers: Array<Transformer>,
shouldPreserveNewLines = false,
): (markdownString: string, node?: ElementNode) => void {
const byType = transformersByType(transformers);
const textFormatTransformersIndex = createTextFormatTransformersIndex(
byType.textFormat,
);
return (markdownString, node) => {
const lines = markdownString.split('\n');
const linesLength = lines.length;
const root = node || $getRoot();
root.clear();
for (let i = 0; i < linesLength; i++) {
const lineText = lines[i];
const [imported, shiftedIndex] = $importMultiline(
lines,
i,
byType.multilineElement,
root,
);
if (imported) {
// If a multiline markdown element was imported, we don't want to process the lines that were part of it anymore.
// There could be other sub-markdown elements (both multiline and normal ones) matching within this matched multiline element's children.
// However, it would be the responsibility of the matched multiline transformer to decide how it wants to handle them.
// We cannot handle those, as there is no way for us to know how to maintain the correct order of generated lexical nodes for possible children.
i = shiftedIndex; // Next loop will start from the line after the last line of the multiline element
continue;
}
$importBlocks(
lineText,
root,
byType.element,
textFormatTransformersIndex,
byType.textMatch,
);
}
// By default, removing empty paragraphs as md does not really
// allow empty lines and uses them as delimiter.
// If you need empty lines set shouldPreserveNewLines = true.
const children = root.getChildren();
for (const child of children) {
if (
!shouldPreserveNewLines &&
isEmptyParagraph(child) &&
root.getChildrenSize() > 1
) {
child.remove();
}
}
if ($getSelection() !== null) {
root.selectStart();
}
};
}
/**
*
* @returns first element of the returned tuple is a boolean indicating if a multiline element was imported. The second element is the index of the last line that was processed.
*/
function $importMultiline(
lines: Array<string>,
startLineIndex: number,
multilineElementTransformers: Array<MultilineElementTransformer>,
rootNode: ElementNode,
): [boolean, number] {
for (const transformer of multilineElementTransformers) {
const {handleImportAfterStartMatch, regExpEnd, regExpStart, replace} =
transformer;
const startMatch = lines[startLineIndex].match(regExpStart);
if (!startMatch) {
continue; // Try next transformer
}
if (handleImportAfterStartMatch) {
const result = handleImportAfterStartMatch({
lines,
rootNode,
startLineIndex,
startMatch,
transformer,
});
if (result === null) {
continue;
} else if (result) {
return result;
}
}
const regexpEndRegex: RegExp | undefined =
typeof regExpEnd === 'object' && 'regExp' in regExpEnd
? regExpEnd.regExp
: regExpEnd;
const isEndOptional =
regExpEnd && typeof regExpEnd === 'object' && 'optional' in regExpEnd
? regExpEnd.optional
: !regExpEnd;
let endLineIndex = startLineIndex;
const linesLength = lines.length;
// check every single line for the closing match. It could also be on the same line as the opening match.
while (endLineIndex < linesLength) {
const endMatch = regexpEndRegex
? lines[endLineIndex].match(regexpEndRegex)
: null;
if (!endMatch) {
if (
!isEndOptional ||
(isEndOptional && endLineIndex < linesLength - 1) // Optional end, but didn't reach the end of the document yet => continue searching for potential closing match
) {
endLineIndex++;
continue; // Search next line for closing match
}
}
// Now, check if the closing match matched is the same as the opening match.
// If it is, we need to continue searching for the actual closing match.
if (
endMatch &&
startLineIndex === endLineIndex &&
endMatch.index === startMatch.index
) {
endLineIndex++;
continue; // Search next line for closing match
}
// At this point, we have found the closing match. Next: calculate the lines in between open and closing match
// This should not include the matches themselves, and be split up by lines
const linesInBetween = [];
if (endMatch && startLineIndex === endLineIndex) {
linesInBetween.push(
lines[startLineIndex].slice(
startMatch[0].length,
-endMatch[0].length,
),
);
} else {
for (let i = startLineIndex; i <= endLineIndex; i++) {
if (i === startLineIndex) {
const text = lines[i].slice(startMatch[0].length);
linesInBetween.push(text); // Also include empty text
} else if (i === endLineIndex && endMatch) {
const text = lines[i].slice(0, -endMatch[0].length);
linesInBetween.push(text); // Also include empty text
} else {
linesInBetween.push(lines[i]);
}
}
}
if (
replace(rootNode, null, startMatch, endMatch, linesInBetween, true) !==
false
) {
// Return here. This $importMultiline function is run line by line and should only process a single multiline element at a time.
return [true, endLineIndex];
}
// The replace function returned false, despite finding the matching open and close tags => this transformer does not want to handle it.
// Thus, we continue letting the remaining transformers handle the passed lines of text from the beginning
break;
}
}
// No multiline transformer handled this line successfully
return [false, startLineIndex];
}
function $importBlocks(
lineText: string,
rootNode: ElementNode,
elementTransformers: Array<ElementTransformer>,
textFormatTransformersIndex: TextFormatTransformersIndex,
textMatchTransformers: Array<TextMatchTransformer>,
) {
const textNode = $createTextNode(lineText);
const elementNode = $createParagraphNode();
elementNode.append(textNode);
rootNode.append(elementNode);
for (const {regExp, replace} of elementTransformers) {
const match = lineText.match(regExp);
if (match) {
textNode.setTextContent(lineText.slice(match[0].length));
if (replace(elementNode, [textNode], match, true) !== false) {
break;
}
}
}
importTextTransformers(
textNode,
textFormatTransformersIndex,
textMatchTransformers,
);
// If no transformer found and we left with original paragraph node
// can check if its content can be appended to the previous node
// if it's a paragraph, quote or list
if (elementNode.isAttached() && lineText.length > 0) {
const previousNode = elementNode.getPreviousSibling();
if (
$isParagraphNode(previousNode) ||
$isQuoteNode(previousNode) ||
$isListNode(previousNode)
) {
let targetNode: typeof previousNode | ListItemNode | null = previousNode;
if ($isListNode(previousNode)) {
const lastDescendant = previousNode.getLastDescendant();
if (lastDescendant == null) {
targetNode = null;
} else {
targetNode = $findMatchingParent(lastDescendant, $isListItemNode);
}
}
if (targetNode != null && targetNode.getTextContentSize() > 0) {
targetNode.splice(targetNode.getChildrenSize(), 0, [
$createLineBreakNode(),
...elementNode.getChildren(),
]);
elementNode.remove();
}
}
}
}
function createTextFormatTransformersIndex(
textTransformers: Array<TextFormatTransformer>,
): TextFormatTransformersIndex {
const transformersByTag: Record<string, TextFormatTransformer> = {};
const fullMatchRegExpByTag: Record<string, RegExp> = {};
const openTagsRegExp = [];
const escapeRegExp = `(?<![\\\\])`;
for (const transformer of textTransformers) {
const {tag} = transformer;
transformersByTag[tag] = transformer;
const tagRegExp = tag.replace(/(\*|\^|\+)/g, '\\$1');
openTagsRegExp.push(tagRegExp);
if (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT) {
fullMatchRegExpByTag[tag] = new RegExp(
`(${tagRegExp})(?![${tagRegExp}\\s])(.*?[^${tagRegExp}\\s])${tagRegExp}(?!${tagRegExp})`,
);
} else {
fullMatchRegExpByTag[tag] = new RegExp(
`(?<![\\\\${tagRegExp}])(${tagRegExp})((\\\\${tagRegExp})?.*?[^${tagRegExp}\\s](\\\\${tagRegExp})?)((?<!\\\\)|(?<=\\\\\\\\))(${tagRegExp})(?![\\\\${tagRegExp}])`,
);
}
}
return {
// Reg exp to find open tag + content + close tag
fullMatchRegExpByTag,
// Reg exp to find opening tags
openTagsRegExp: new RegExp(
(IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT ? '' : `${escapeRegExp}`) +
'(' +
openTagsRegExp.join('|') +
')',
'g',
),
transformersByTag,
};
}