forked from facebook/lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownExport.ts
338 lines (298 loc) · 10.3 KB
/
MarkdownExport.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* 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 {ElementNode, LexicalNode, TextFormatType, TextNode} from 'lexical';
import {
$getRoot,
$isDecoratorNode,
$isElementNode,
$isLineBreakNode,
$isTextNode,
} from 'lexical';
import {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers';
import {isEmptyParagraph, transformersByType} from './utils';
/**
* Renders string from markdown. The selection is moved to the start after the operation.
*/
export function createMarkdownExport(
transformers: Array<Transformer>,
shouldPreserveNewLines: boolean = false,
): (node?: ElementNode) => string {
const byType = transformersByType(transformers);
const elementTransformers = [...byType.multilineElement, ...byType.element];
const isNewlineDelimited = !shouldPreserveNewLines;
// Export only uses text formats that are responsible for single format
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
const textFormatTransformers = byType.textFormat
.filter((transformer) => transformer.format.length === 1)
// Make sure all text transformers that contain 'code' in their format are at the end of the array. Otherwise, formatted code like
// <strong><code>code</code></strong> will be exported as `**Bold Code**`, as the code format will be applied first, and the bold format
// will be applied second and thus skipped entirely, as the code format will prevent any further formatting.
.sort((a, b) => {
return (
Number(a.format.includes('code')) - Number(b.format.includes('code'))
);
});
return (node) => {
const output = [];
const children = (node || $getRoot()).getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
const result = exportTopLevelElements(
child,
elementTransformers,
textFormatTransformers,
byType.textMatch,
);
if (result != null) {
output.push(
// separate consecutive group of texts with a line break: eg. ["hello", "world"] -> ["hello", "/nworld"]
isNewlineDelimited &&
i > 0 &&
!isEmptyParagraph(child) &&
!isEmptyParagraph(children[i - 1])
? '\n'.concat(result)
: result,
);
}
}
// Ensure consecutive groups of texts are at least \n\n apart while each empty paragraph render as a newline.
// Eg. ["hello", "", "", "hi", "\nworld"] -> "hello\n\n\nhi\n\nworld"
return output.join('\n');
};
}
function exportTopLevelElements(
node: LexicalNode,
elementTransformers: Array<ElementTransformer | MultilineElementTransformer>,
textTransformersIndex: Array<TextFormatTransformer>,
textMatchTransformers: Array<TextMatchTransformer>,
): string | null {
for (const transformer of elementTransformers) {
if (!transformer.export) {
continue;
}
const result = transformer.export(node, (_node) =>
exportChildren(_node, textTransformersIndex, textMatchTransformers),
);
if (result != null) {
return result;
}
}
if ($isElementNode(node)) {
return exportChildren(node, textTransformersIndex, textMatchTransformers);
} else if ($isDecoratorNode(node)) {
return node.getTextContent();
} else {
return null;
}
}
function exportChildren(
node: ElementNode,
textTransformersIndex: Array<TextFormatTransformer>,
textMatchTransformers: Array<TextMatchTransformer>,
unclosedTags?: Array<{format: TextFormatType; tag: string}>,
unclosableTags?: Array<{format: TextFormatType; tag: string}>,
): string {
const output = [];
const children = node.getChildren();
// keep track of unclosed tags from the very beginning
if (!unclosedTags) {
unclosedTags = [];
}
if (!unclosableTags) {
unclosableTags = [];
}
mainLoop: for (const child of children) {
for (const transformer of textMatchTransformers) {
if (!transformer.export) {
continue;
}
const result = transformer.export(
child,
(parentNode) =>
exportChildren(
parentNode,
textTransformersIndex,
textMatchTransformers,
unclosedTags,
// Add current unclosed tags to the list of unclosable tags - we don't want nested tags from
// textmatch transformers to close the outer ones, as that may result in invalid markdown.
// E.g. **text [text**](https://lexical.io)
// is invalid markdown, as the closing ** is inside the link.
//
[...unclosableTags, ...unclosedTags],
),
(textNode, textContent) =>
exportTextFormat(
textNode,
textContent,
textTransformersIndex,
unclosedTags,
unclosableTags,
),
);
if (result != null) {
output.push(result);
continue mainLoop;
}
}
if ($isLineBreakNode(child)) {
output.push('\n');
} else if ($isTextNode(child)) {
output.push(
exportTextFormat(
child,
child.getTextContent(),
textTransformersIndex,
unclosedTags,
unclosableTags,
),
);
} else if ($isElementNode(child)) {
// empty paragraph returns ""
output.push(
exportChildren(
child,
textTransformersIndex,
textMatchTransformers,
unclosedTags,
unclosableTags,
),
);
} else if ($isDecoratorNode(child)) {
output.push(child.getTextContent());
}
}
return output.join('');
}
function exportTextFormat(
node: TextNode,
textContent: string,
textTransformers: Array<TextFormatTransformer>,
// unclosed tags include the markdown tags that haven't been closed yet, and their associated formats
unclosedTags: Array<{format: TextFormatType; tag: string}>,
unclosableTags?: Array<{format: TextFormatType; tag: string}>,
): string {
// This function handles the case of a string looking like this: " foo "
// Where it would be invalid markdown to generate: "** foo **"
// We instead want to trim the whitespace out, apply formatting, and then
// bring the whitespace back. So our returned string looks like this: " **foo** "
const frozenString = textContent.trim();
let output = frozenString;
// the opening tags to be added to the result
let openingTags = '';
// the closing tags to be added to the result
let closingTagsBefore = '';
let closingTagsAfter = '';
const prevNode = getTextSibling(node, true);
const nextNode = getTextSibling(node, false);
const applied = new Set();
for (const transformer of textTransformers) {
const format = transformer.format[0];
const tag = transformer.tag;
// dedup applied formats
if (hasFormat(node, format) && !applied.has(format)) {
// Multiple tags might be used for the same format (*, _)
applied.add(format);
// append the tag to openningTags, if it's not applied to the previous nodes,
// or the nodes before that (which would result in an unclosed tag)
if (
!hasFormat(prevNode, format) ||
!unclosedTags.find((element) => element.tag === tag)
) {
unclosedTags.push({format, tag});
openingTags += tag;
}
}
}
// close any tags in the same order they were applied, if necessary
for (let i = 0; i < unclosedTags.length; i++) {
const nodeHasFormat = hasFormat(node, unclosedTags[i].format);
const nextNodeHasFormat = hasFormat(nextNode, unclosedTags[i].format);
// prevent adding closing tag if next sibling will do it
if (nodeHasFormat && nextNodeHasFormat) {
continue;
}
const unhandledUnclosedTags = [...unclosedTags]; // Shallow copy to avoid modifying the original array
while (unhandledUnclosedTags.length > i) {
const unclosedTag = unhandledUnclosedTags.pop();
// If tag is unclosable, don't close it and leave it in the original array,
// So that it can be closed when it's no longer unclosable
if (
unclosableTags &&
unclosedTag &&
unclosableTags.find((element) => element.tag === unclosedTag.tag)
) {
continue;
}
if (unclosedTag && typeof unclosedTag.tag === 'string') {
if (!nodeHasFormat) {
// Handles cases where the tag has not been closed before, e.g. if the previous node
// was a text match transformer that did not account for closing tags of the next node (e.g. a link)
closingTagsBefore += unclosedTag.tag;
} else if (!nextNodeHasFormat) {
closingTagsAfter += unclosedTag.tag;
}
}
// Mutate the original array to remove the closed tag
unclosedTags.pop();
}
break;
}
output = openingTags + output + closingTagsAfter;
// Replace trimmed version of textContent ensuring surrounding whitespace is not modified
return closingTagsBefore + textContent.replace(frozenString, () => output);
}
// Get next or previous text sibling a text node, including cases
// when it's a child of inline element (e.g. link)
function getTextSibling(node: TextNode, backward: boolean): TextNode | null {
let sibling = backward ? node.getPreviousSibling() : node.getNextSibling();
if (!sibling) {
const parent = node.getParentOrThrow();
if (parent.isInline()) {
sibling = backward
? parent.getPreviousSibling()
: parent.getNextSibling();
}
}
while (sibling) {
if ($isElementNode(sibling)) {
if (!sibling.isInline()) {
break;
}
const descendant = backward
? sibling.getLastDescendant()
: sibling.getFirstDescendant();
if ($isTextNode(descendant)) {
return descendant;
} else {
sibling = backward
? sibling.getPreviousSibling()
: sibling.getNextSibling();
}
}
if ($isTextNode(sibling)) {
return sibling;
}
if (!$isElementNode(sibling)) {
return null;
}
}
return null;
}
function hasFormat(
node: LexicalNode | null | undefined,
format: TextFormatType,
): boolean {
return $isTextNode(node) && node.hasFormat(format);
}