@payloadcms/richtext-lexical 3.12.0 → 3.12.1-canary.7fea7e5
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/dist/exports/client/{Field-7XFZE2MU.js → Field-Q6F2LMIQ.js} +2 -2
- package/dist/exports/client/chunk-WCORQHZQ.js +12 -0
- package/dist/exports/client/chunk-WCORQHZQ.js.map +7 -0
- package/dist/exports/client/index.js +11 -11
- package/dist/exports/client/index.js.map +3 -3
- package/dist/features/link/markdownTransformer.d.ts.map +1 -1
- package/dist/features/link/markdownTransformer.js +6 -11
- package/dist/features/link/markdownTransformer.js.map +1 -1
- package/dist/packages/@lexical/markdown/MarkdownExport.js +68 -14
- package/dist/packages/@lexical/markdown/MarkdownExport.js.map +1 -1
- package/dist/packages/@lexical/markdown/MarkdownImport.d.ts +6 -1
- package/dist/packages/@lexical/markdown/MarkdownImport.d.ts.map +1 -1
- package/dist/packages/@lexical/markdown/MarkdownImport.js +3 -119
- package/dist/packages/@lexical/markdown/MarkdownImport.js.map +1 -1
- package/dist/packages/@lexical/markdown/MarkdownTransformers.d.ts +4 -1
- package/dist/packages/@lexical/markdown/MarkdownTransformers.d.ts.map +1 -1
- package/dist/packages/@lexical/markdown/MarkdownTransformers.js.map +1 -1
- package/dist/packages/@lexical/markdown/importTextFormatTransformer.d.ts +22 -0
- package/dist/packages/@lexical/markdown/importTextFormatTransformer.d.ts.map +1 -0
- package/dist/packages/@lexical/markdown/importTextFormatTransformer.js +88 -0
- package/dist/packages/@lexical/markdown/importTextFormatTransformer.js.map +1 -0
- package/dist/packages/@lexical/markdown/importTextMatchTransformer.d.ts +21 -0
- package/dist/packages/@lexical/markdown/importTextMatchTransformer.d.ts.map +1 -0
- package/dist/packages/@lexical/markdown/importTextMatchTransformer.js +54 -0
- package/dist/packages/@lexical/markdown/importTextMatchTransformer.js.map +1 -0
- package/dist/packages/@lexical/markdown/importTextTransformers.d.ts +18 -0
- package/dist/packages/@lexical/markdown/importTextTransformers.d.ts.map +1 -0
- package/dist/packages/@lexical/markdown/importTextTransformers.js +55 -0
- package/dist/packages/@lexical/markdown/importTextTransformers.js.map +1 -0
- package/dist/utilities/jsx/lexicalMarkdownCopy.d.ts.map +1 -1
- package/dist/utilities/jsx/lexicalMarkdownCopy.js +11 -6
- package/dist/utilities/jsx/lexicalMarkdownCopy.js.map +1 -1
- package/package.json +6 -6
- package/dist/exports/client/chunk-XOABLBHB.js +0 -12
- package/dist/exports/client/chunk-XOABLBHB.js.map +0 -7
- /package/dist/exports/client/{Field-7XFZE2MU.js.map → Field-Q6F2LMIQ.js.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownTransformer.d.ts","sourceRoot":"","sources":["../../../src/features/link/markdownTransformer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0DAA0D,CAAA;AAKpG,eAAO,MAAM,uBAAuB,EAAE,
|
|
1
|
+
{"version":3,"file":"markdownTransformer.d.ts","sourceRoot":"","sources":["../../../src/features/link/markdownTransformer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0DAA0D,CAAA;AAKpG,eAAO,MAAM,uBAAuB,EAAE,oBAoCrC,CAAA"}
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
*/ // Order of text transformers matters:
|
|
4
4
|
//
|
|
5
5
|
// - code should go first as it prevents any transformations inside
|
|
6
|
-
import { $createTextNode
|
|
6
|
+
import { $createTextNode } from 'lexical';
|
|
7
7
|
import { $createLinkNode, $isLinkNode, LinkNode } from './nodes/LinkNode.js';
|
|
8
8
|
// - then longer tags match (e.g. ** or __ should go before * or _)
|
|
9
9
|
export const LinkMarkdownTransformer = {
|
|
10
10
|
type: 'text-match',
|
|
11
11
|
dependencies: [LinkNode],
|
|
12
|
-
export: (_node, exportChildren
|
|
12
|
+
export: (_node, exportChildren) => {
|
|
13
13
|
if (!$isLinkNode(_node)) {
|
|
14
14
|
return null;
|
|
15
15
|
}
|
|
@@ -17,15 +17,9 @@ export const LinkMarkdownTransformer = {
|
|
|
17
17
|
const {
|
|
18
18
|
url
|
|
19
19
|
} = node.getFields();
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
// then one we ignore it as markdown does not support nested styles for links
|
|
24
|
-
if (node.getChildrenSize() === 1 && $isTextNode(firstChild)) {
|
|
25
|
-
return exportFormat(firstChild, linkContent);
|
|
26
|
-
} else {
|
|
27
|
-
return linkContent;
|
|
28
|
-
}
|
|
20
|
+
const textContent = exportChildren(node);
|
|
21
|
+
const linkContent = `[${textContent}](${url})`;
|
|
22
|
+
return linkContent;
|
|
29
23
|
},
|
|
30
24
|
importRegExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)/,
|
|
31
25
|
regExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)$/,
|
|
@@ -43,6 +37,7 @@ export const LinkMarkdownTransformer = {
|
|
|
43
37
|
linkTextNode.setFormat(textNode.getFormat());
|
|
44
38
|
linkNode.append(linkTextNode);
|
|
45
39
|
textNode.replace(linkNode);
|
|
40
|
+
return linkTextNode;
|
|
46
41
|
},
|
|
47
42
|
trigger: ')'
|
|
48
43
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownTransformer.js","names":["$createTextNode","$
|
|
1
|
+
{"version":3,"file":"markdownTransformer.js","names":["$createTextNode","$createLinkNode","$isLinkNode","LinkNode","LinkMarkdownTransformer","type","dependencies","export","_node","exportChildren","node","url","getFields","textContent","linkContent","importRegExp","regExp","replace","textNode","match","linkText","linkUrl","linkNode","fields","doc","linkType","newTab","linkTextNode","setFormat","getFormat","append","trigger"],"sources":["../../../src/features/link/markdownTransformer.ts"],"sourcesContent":["/**\n * Code taken from https://github.com/facebook/lexical/blob/main/packages/lexical-markdown/src/MarkdownTransformers.ts#L357\n */\n\n// Order of text transformers matters:\n//\n// - code should go first as it prevents any transformations inside\n\nimport { $createTextNode, $isTextNode } from 'lexical'\n\nimport type { TextMatchTransformer } from '../../packages/@lexical/markdown/MarkdownTransformers.js'\n\nimport { $createLinkNode, $isLinkNode, LinkNode } from './nodes/LinkNode.js'\n\n// - then longer tags match (e.g. ** or __ should go before * or _)\nexport const LinkMarkdownTransformer: TextMatchTransformer = {\n type: 'text-match',\n dependencies: [LinkNode],\n export: (_node, exportChildren) => {\n if (!$isLinkNode(_node)) {\n return null\n }\n const node: LinkNode = _node\n const { url } = node.getFields()\n\n const textContent = exportChildren(node)\n\n const linkContent = `[${textContent}](${url})`\n\n return linkContent\n },\n importRegExp: /\\[([^[]+)\\]\\(([^()\\s]+)(?:\\s\"((?:[^\"]*\\\\\")*[^\"]*)\"\\s*)?\\)/,\n regExp: /\\[([^[]+)\\]\\(([^()\\s]+)(?:\\s\"((?:[^\"]*\\\\\")*[^\"]*)\"\\s*)?\\)$/,\n replace: (textNode, match) => {\n const [, linkText, linkUrl] = match\n const linkNode = $createLinkNode({\n fields: {\n doc: null,\n linkType: 'custom',\n newTab: false,\n url: linkUrl,\n },\n })\n const linkTextNode = $createTextNode(linkText)\n linkTextNode.setFormat(textNode.getFormat())\n linkNode.append(linkTextNode)\n textNode.replace(linkNode)\n\n return linkTextNode\n },\n trigger: ')',\n}\n"],"mappings":"AAAA;;GAAA,CAIA;AACA;AACA;AAEA,SAASA,eAAe,QAAqB;AAI7C,SAASC,eAAe,EAAEC,WAAW,EAAEC,QAAQ,QAAQ;AAEvD;AACA,OAAO,MAAMC,uBAAA,GAAgD;EAC3DC,IAAA,EAAM;EACNC,YAAA,EAAc,CAACH,QAAA,CAAS;EACxBI,MAAA,EAAQA,CAACC,KAAA,EAAOC,cAAA;IACd,IAAI,CAACP,WAAA,CAAYM,KAAA,GAAQ;MACvB,OAAO;IACT;IACA,MAAME,IAAA,GAAiBF,KAAA;IACvB,MAAM;MAAEG;IAAG,CAAE,GAAGD,IAAA,CAAKE,SAAS;IAE9B,MAAMC,WAAA,GAAcJ,cAAA,CAAeC,IAAA;IAEnC,MAAMI,WAAA,GAAc,IAAID,WAAA,KAAgBF,GAAA,GAAM;IAE9C,OAAOG,WAAA;EACT;EACAC,YAAA,EAAc;EACdC,MAAA,EAAQ;EACRC,OAAA,EAASA,CAACC,QAAA,EAAUC,KAAA;IAClB,MAAM,GAAGC,QAAA,EAAUC,OAAA,CAAQ,GAAGF,KAAA;IAC9B,MAAMG,QAAA,GAAWrB,eAAA,CAAgB;MAC/BsB,MAAA,EAAQ;QACNC,GAAA,EAAK;QACLC,QAAA,EAAU;QACVC,MAAA,EAAQ;QACRf,GAAA,EAAKU;MACP;IACF;IACA,MAAMM,YAAA,GAAe3B,eAAA,CAAgBoB,QAAA;IACrCO,YAAA,CAAaC,SAAS,CAACV,QAAA,CAASW,SAAS;IACzCP,QAAA,CAASQ,MAAM,CAACH,YAAA;IAChBT,QAAA,CAASD,OAAO,CAACK,QAAA;IAEjB,OAAOK,YAAA;EACT;EACAI,OAAA,EAAS;AACX","ignoreList":[]}
|
|
@@ -51,15 +51,28 @@ function exportTopLevelElements(node, elementTransformers, textTransformersIndex
|
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
function exportChildren(node, textTransformersIndex, textMatchTransformers) {
|
|
54
|
+
function exportChildren(node, textTransformersIndex, textMatchTransformers, unclosedTags, unclosableTags) {
|
|
55
55
|
const output = [];
|
|
56
56
|
const children = node.getChildren();
|
|
57
|
+
// keep track of unclosed tags from the very beginning
|
|
58
|
+
if (!unclosedTags) {
|
|
59
|
+
unclosedTags = [];
|
|
60
|
+
}
|
|
61
|
+
if (!unclosableTags) {
|
|
62
|
+
unclosableTags = [];
|
|
63
|
+
}
|
|
57
64
|
mainLoop: for (const child of children) {
|
|
58
65
|
for (const transformer of textMatchTransformers) {
|
|
59
66
|
if (!transformer.export) {
|
|
60
67
|
continue;
|
|
61
68
|
}
|
|
62
|
-
const result = transformer.export(child, parentNode => exportChildren(parentNode, textTransformersIndex, textMatchTransformers
|
|
69
|
+
const result = transformer.export(child, parentNode => exportChildren(parentNode, textTransformersIndex, textMatchTransformers, unclosedTags,
|
|
70
|
+
// Add current unclosed tags to the list of unclosable tags - we don't want nested tags from
|
|
71
|
+
// textmatch transformers to close the outer ones, as that may result in invalid markdown.
|
|
72
|
+
// E.g. **text [text**](https://lexical.io)
|
|
73
|
+
// is invalid markdown, as the closing ** is inside the link.
|
|
74
|
+
//
|
|
75
|
+
[...unclosableTags, ...unclosedTags]), (textNode, textContent) => exportTextFormat(textNode, textContent, textTransformersIndex, unclosedTags, unclosableTags));
|
|
63
76
|
if (result != null) {
|
|
64
77
|
output.push(result);
|
|
65
78
|
continue mainLoop;
|
|
@@ -68,44 +81,85 @@ function exportChildren(node, textTransformersIndex, textMatchTransformers) {
|
|
|
68
81
|
if ($isLineBreakNode(child)) {
|
|
69
82
|
output.push('\n');
|
|
70
83
|
} else if ($isTextNode(child)) {
|
|
71
|
-
output.push(exportTextFormat(child, child.getTextContent(), textTransformersIndex));
|
|
84
|
+
output.push(exportTextFormat(child, child.getTextContent(), textTransformersIndex, unclosedTags, unclosableTags));
|
|
72
85
|
} else if ($isElementNode(child)) {
|
|
73
86
|
// empty paragraph returns ""
|
|
74
|
-
output.push(exportChildren(child, textTransformersIndex, textMatchTransformers));
|
|
87
|
+
output.push(exportChildren(child, textTransformersIndex, textMatchTransformers, unclosedTags, unclosableTags));
|
|
75
88
|
} else if ($isDecoratorNode(child)) {
|
|
76
89
|
output.push(child.getTextContent());
|
|
77
90
|
}
|
|
78
91
|
}
|
|
79
92
|
return output.join('');
|
|
80
93
|
}
|
|
81
|
-
function exportTextFormat(node, textContent, textTransformers
|
|
94
|
+
function exportTextFormat(node, textContent, textTransformers,
|
|
95
|
+
// unclosed tags include the markdown tags that haven't been closed yet, and their associated formats
|
|
96
|
+
unclosedTags, unclosableTags) {
|
|
82
97
|
// This function handles the case of a string looking like this: " foo "
|
|
83
98
|
// Where it would be invalid markdown to generate: "** foo **"
|
|
84
99
|
// We instead want to trim the whitespace out, apply formatting, and then
|
|
85
100
|
// bring the whitespace back. So our returned string looks like this: " **foo** "
|
|
86
101
|
const frozenString = textContent.trim();
|
|
87
102
|
let output = frozenString;
|
|
103
|
+
// the opening tags to be added to the result
|
|
104
|
+
let openingTags = '';
|
|
105
|
+
// the closing tags to be added to the result
|
|
106
|
+
let closingTagsBefore = '';
|
|
107
|
+
let closingTagsAfter = '';
|
|
108
|
+
const prevNode = getTextSibling(node, true);
|
|
109
|
+
const nextNode = getTextSibling(node, false);
|
|
88
110
|
const applied = new Set();
|
|
89
111
|
for (const transformer of textTransformers) {
|
|
90
112
|
const format = transformer.format[0];
|
|
91
113
|
const tag = transformer.tag;
|
|
114
|
+
// dedup applied formats
|
|
92
115
|
if (hasFormat(node, format) && !applied.has(format)) {
|
|
93
116
|
// Multiple tags might be used for the same format (*, _)
|
|
94
117
|
applied.add(format);
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
if (!hasFormat(
|
|
98
|
-
|
|
118
|
+
// append the tag to openningTags, if it's not applied to the previous nodes,
|
|
119
|
+
// or the nodes before that (which would result in an unclosed tag)
|
|
120
|
+
if (!hasFormat(prevNode, format) || !unclosedTags.find(element => element.tag === tag)) {
|
|
121
|
+
unclosedTags.push({
|
|
122
|
+
format,
|
|
123
|
+
tag
|
|
124
|
+
});
|
|
125
|
+
openingTags += tag;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// close any tags in the same order they were applied, if necessary
|
|
130
|
+
for (let i = 0; i < unclosedTags.length; i++) {
|
|
131
|
+
const nodeHasFormat = hasFormat(node, unclosedTags[i].format);
|
|
132
|
+
const nextNodeHasFormat = hasFormat(nextNode, unclosedTags[i].format);
|
|
133
|
+
// prevent adding closing tag if next sibling will do it
|
|
134
|
+
if (nodeHasFormat && nextNodeHasFormat) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const unhandledUnclosedTags = [...unclosedTags] // Shallow copy to avoid modifying the original array
|
|
138
|
+
;
|
|
139
|
+
while (unhandledUnclosedTags.length > i) {
|
|
140
|
+
const unclosedTag = unhandledUnclosedTags.pop();
|
|
141
|
+
// If tag is unclosable, don't close it and leave it in the original array,
|
|
142
|
+
// So that it can be closed when it's no longer unclosable
|
|
143
|
+
if (unclosableTags && unclosedTag && unclosableTags.find(element => element.tag === unclosedTag.tag)) {
|
|
144
|
+
continue;
|
|
99
145
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
146
|
+
if (unclosedTag && typeof unclosedTag.tag === 'string') {
|
|
147
|
+
if (!nodeHasFormat) {
|
|
148
|
+
// Handles cases where the tag has not been closed before, e.g. if the previous node
|
|
149
|
+
// was a text match transformer that did not account for closing tags of the next node (e.g. a link)
|
|
150
|
+
closingTagsBefore += unclosedTag.tag;
|
|
151
|
+
} else if (!nextNodeHasFormat) {
|
|
152
|
+
closingTagsAfter += unclosedTag.tag;
|
|
153
|
+
}
|
|
104
154
|
}
|
|
155
|
+
// Mutate the original array to remove the closed tag
|
|
156
|
+
unclosedTags.pop();
|
|
105
157
|
}
|
|
158
|
+
break;
|
|
106
159
|
}
|
|
160
|
+
output = openingTags + output + closingTagsAfter;
|
|
107
161
|
// Replace trimmed version of textContent ensuring surrounding whitespace is not modified
|
|
108
|
-
return textContent.replace(frozenString, () => output);
|
|
162
|
+
return closingTagsBefore + textContent.replace(frozenString, () => output);
|
|
109
163
|
}
|
|
110
164
|
// Get next or previous text sibling a text node, including cases
|
|
111
165
|
// when it's a child of inline element (e.g. link)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownExport.js","names":["$getRoot","$isDecoratorNode","$isElementNode","$isLineBreakNode","$isTextNode","isEmptyParagraph","transformersByType","createMarkdownExport","transformers","shouldPreserveNewLines","byType","elementTransformers","multilineElement","element","isNewlineDelimited","textFormatTransformers","textFormat","filter","transformer","format","length","node","output","children","getChildren","i","child","result","exportTopLevelElements","textMatch","push","concat","join","textTransformersIndex","textMatchTransformers","export","_node","exportChildren","getTextContent","mainLoop","parentNode","textNode","textContent","exportTextFormat","textTransformers","frozenString","trim","applied","Set","tag","hasFormat","has","add","previousNode","getTextSibling","nextNode","replace","backward","sibling","getPreviousSibling","getNextSibling","parent","getParentOrThrow","isInline","descendant","getLastDescendant","getFirstDescendant"],"sources":["../../../../src/packages/@lexical/markdown/MarkdownExport.ts"],"sourcesContent":["/**\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport type { ElementNode, LexicalNode, TextFormatType, TextNode } from 'lexical'\n\nimport { $getRoot, $isDecoratorNode, $isElementNode, $isLineBreakNode, $isTextNode } from 'lexical'\n\nimport type {\n ElementTransformer,\n MultilineElementTransformer,\n TextFormatTransformer,\n TextMatchTransformer,\n Transformer,\n} from './MarkdownTransformers.js'\n\nimport { isEmptyParagraph, transformersByType } from './utils.js'\n\n/**\n * Renders string from markdown. The selection is moved to the start after the operation.\n */\nexport function createMarkdownExport(\n transformers: Array<Transformer>,\n shouldPreserveNewLines: boolean = false,\n): (node?: ElementNode) => string {\n const byType = transformersByType(transformers)\n const elementTransformers = [...byType.multilineElement, ...byType.element]\n const isNewlineDelimited = !shouldPreserveNewLines\n\n // Export only uses text formats that are responsible for single format\n // e.g. it will filter out *** (bold, italic) and instead use separate ** and *\n const textFormatTransformers = byType.textFormat.filter(\n (transformer) => transformer.format.length === 1,\n )\n\n return (node) => {\n const output: string[] = []\n const children = (node || $getRoot()).getChildren()\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n const result = exportTopLevelElements(\n child,\n elementTransformers,\n textFormatTransformers,\n byType.textMatch,\n )\n\n if (result != null) {\n output.push(\n // separate consecutive group of texts with a line break: eg. [\"hello\", \"world\"] -> [\"hello\", \"/nworld\"]\n isNewlineDelimited &&\n i > 0 &&\n !isEmptyParagraph(child) &&\n !isEmptyParagraph(children[i - 1])\n ? '\\n'.concat(result)\n : result,\n )\n }\n }\n // Ensure consecutive groups of texts are at least \\n\\n apart while each empty paragraph render as a newline.\n // Eg. [\"hello\", \"\", \"\", \"hi\", \"\\nworld\"] -> \"hello\\n\\n\\nhi\\n\\nworld\"\n return output.join('\\n')\n }\n}\n\nfunction exportTopLevelElements(\n node: LexicalNode,\n elementTransformers: Array<ElementTransformer | MultilineElementTransformer>,\n textTransformersIndex: Array<TextFormatTransformer>,\n textMatchTransformers: Array<TextMatchTransformer>,\n): null | string {\n for (const transformer of elementTransformers) {\n if (!transformer.export) {\n continue\n }\n const result = transformer.export(node, (_node) =>\n exportChildren(_node, textTransformersIndex, textMatchTransformers),\n )\n\n if (result != null) {\n return result\n }\n }\n\n if ($isElementNode(node)) {\n return exportChildren(node, textTransformersIndex, textMatchTransformers)\n } else if ($isDecoratorNode(node)) {\n return node.getTextContent()\n } else {\n return null\n }\n}\n\nfunction exportChildren(\n node: ElementNode,\n textTransformersIndex: Array<TextFormatTransformer>,\n textMatchTransformers: Array<TextMatchTransformer>,\n): string {\n const output: string[] = []\n const children = node.getChildren()\n\n mainLoop: for (const child of children) {\n for (const transformer of textMatchTransformers) {\n if (!transformer.export) {\n continue\n }\n\n const result = transformer.export(\n child,\n (parentNode) => exportChildren(parentNode, textTransformersIndex, textMatchTransformers),\n (textNode, textContent) => exportTextFormat(textNode, textContent, textTransformersIndex),\n )\n\n if (result != null) {\n output.push(result)\n continue mainLoop\n }\n }\n\n if ($isLineBreakNode(child)) {\n output.push('\\n')\n } else if ($isTextNode(child)) {\n output.push(exportTextFormat(child, child.getTextContent(), textTransformersIndex))\n } else if ($isElementNode(child)) {\n // empty paragraph returns \"\"\n output.push(exportChildren(child, textTransformersIndex, textMatchTransformers))\n } else if ($isDecoratorNode(child)) {\n output.push(child.getTextContent())\n }\n }\n\n return output.join('')\n}\n\nfunction exportTextFormat(\n node: TextNode,\n textContent: string,\n textTransformers: Array<TextFormatTransformer>,\n): string {\n // This function handles the case of a string looking like this: \" foo \"\n // Where it would be invalid markdown to generate: \"** foo **\"\n // We instead want to trim the whitespace out, apply formatting, and then\n // bring the whitespace back. So our returned string looks like this: \" **foo** \"\n const frozenString = textContent.trim()\n let output = frozenString\n\n const applied = new Set()\n\n for (const transformer of textTransformers) {\n const format = transformer.format[0]\n const tag = transformer.tag\n\n if (hasFormat(node, format) && !applied.has(format)) {\n // Multiple tags might be used for the same format (*, _)\n applied.add(format)\n // Prevent adding opening tag is already opened by the previous sibling\n const previousNode = getTextSibling(node, true)\n\n if (!hasFormat(previousNode, format)) {\n output = tag + output\n }\n\n // Prevent adding closing tag if next sibling will do it\n const nextNode = getTextSibling(node, false)\n\n if (!hasFormat(nextNode, format)) {\n output += tag\n }\n }\n }\n\n // Replace trimmed version of textContent ensuring surrounding whitespace is not modified\n return textContent.replace(frozenString, () => output)\n}\n\n// Get next or previous text sibling a text node, including cases\n// when it's a child of inline element (e.g. link)\nfunction getTextSibling(node: TextNode, backward: boolean): null | TextNode {\n let sibling = backward ? node.getPreviousSibling() : node.getNextSibling()\n\n if (!sibling) {\n const parent = node.getParentOrThrow()\n\n if (parent.isInline()) {\n sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling()\n }\n }\n\n while (sibling) {\n if ($isElementNode(sibling)) {\n if (!sibling.isInline()) {\n break\n }\n\n const descendant = backward ? sibling.getLastDescendant() : sibling.getFirstDescendant()\n\n if ($isTextNode(descendant)) {\n return descendant\n } else {\n sibling = backward ? sibling.getPreviousSibling() : sibling.getNextSibling()\n }\n }\n\n if ($isTextNode(sibling)) {\n return sibling\n }\n\n if (!$isElementNode(sibling)) {\n return null\n }\n }\n\n return null\n}\n\nfunction hasFormat(node: LexicalNode | null | undefined, format: TextFormatType): boolean {\n return $isTextNode(node) && node.hasFormat(format)\n}\n"],"mappings":"AAAA;;;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extNode","isEmptyParagraph","transformersByType","createMarkdownExport","transformers","shouldPreserveNewLines","byType","elementTransformers","multilineElement","element","isNewlineDelimited","textFormatTransformers","textFormat","filter","transformer","format","length","node","output","children","getChildren","i","child","result","exportTopLevelElements","textMatch","push","concat","join","textTransformersIndex","textMatchTransformers","export","_node","exportChildren","getTextContent","unclosedTags","unclosableTags","mainLoop","parentNode","textNode","textContent","exportTextFormat","textTransformers","frozenString","trim","openingTags","closingTagsBefore","closingTagsAfter","prevNode","getTextSibling","nextNode","applied","Set","tag","hasFormat","has","add","find","nodeHasFormat","nextNodeHasFormat","unhandledUnclosedTags","unclosedTag","pop","replace","backward","sibling","getPreviousSibling","getNextSibling","parent","getParentOrThrow","isInline","descendant","getLastDescendant","getFirstDescendant"],"sources":["../../../../src/packages/@lexical/markdown/MarkdownExport.ts"],"sourcesContent":["/**\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport type { ElementNode, LexicalNode, TextFormatType, TextNode } from 'lexical'\n\nimport { $getRoot, $isDecoratorNode, $isElementNode, $isLineBreakNode, $isTextNode } from 'lexical'\n\nimport type {\n ElementTransformer,\n MultilineElementTransformer,\n TextFormatTransformer,\n TextMatchTransformer,\n Transformer,\n} from './MarkdownTransformers.js'\n\nimport { isEmptyParagraph, transformersByType } from './utils.js'\n\n/**\n * Renders string from markdown. The selection is moved to the start after the operation.\n */\nexport function createMarkdownExport(\n transformers: Array<Transformer>,\n shouldPreserveNewLines: boolean = false,\n): (node?: ElementNode) => string {\n const byType = transformersByType(transformers)\n const elementTransformers = [...byType.multilineElement, ...byType.element]\n const isNewlineDelimited = !shouldPreserveNewLines\n\n // Export only uses text formats that are responsible for single format\n // e.g. it will filter out *** (bold, italic) and instead use separate ** and *\n const textFormatTransformers = byType.textFormat.filter(\n (transformer) => transformer.format.length === 1,\n )\n\n return (node) => {\n const output = []\n const children = (node || $getRoot()).getChildren()\n\n for (let i = 0; i < children.length; i++) {\n const child = children[i]\n const result = exportTopLevelElements(\n child,\n elementTransformers,\n textFormatTransformers,\n byType.textMatch,\n )\n\n if (result != null) {\n output.push(\n // separate consecutive group of texts with a line break: eg. [\"hello\", \"world\"] -> [\"hello\", \"/nworld\"]\n isNewlineDelimited &&\n i > 0 &&\n !isEmptyParagraph(child) &&\n !isEmptyParagraph(children[i - 1])\n ? '\\n'.concat(result)\n : result,\n )\n }\n }\n // Ensure consecutive groups of texts are at least \\n\\n apart while each empty paragraph render as a newline.\n // Eg. [\"hello\", \"\", \"\", \"hi\", \"\\nworld\"] -> \"hello\\n\\n\\nhi\\n\\nworld\"\n return output.join('\\n')\n }\n}\n\nfunction exportTopLevelElements(\n node: LexicalNode,\n elementTransformers: Array<ElementTransformer | MultilineElementTransformer>,\n textTransformersIndex: Array<TextFormatTransformer>,\n textMatchTransformers: Array<TextMatchTransformer>,\n): null | string {\n for (const transformer of elementTransformers) {\n if (!transformer.export) {\n continue\n }\n const result = transformer.export(node, (_node) =>\n exportChildren(_node, textTransformersIndex, textMatchTransformers),\n )\n\n if (result != null) {\n return result\n }\n }\n\n if ($isElementNode(node)) {\n return exportChildren(node, textTransformersIndex, textMatchTransformers)\n } else if ($isDecoratorNode(node)) {\n return node.getTextContent()\n } else {\n return null\n }\n}\n\nfunction exportChildren(\n node: ElementNode,\n textTransformersIndex: Array<TextFormatTransformer>,\n textMatchTransformers: Array<TextMatchTransformer>,\n unclosedTags?: Array<{ format: TextFormatType; tag: string }>,\n unclosableTags?: Array<{ format: TextFormatType; tag: string }>,\n): string {\n const output = []\n const children = node.getChildren()\n // keep track of unclosed tags from the very beginning\n if (!unclosedTags) {\n unclosedTags = []\n }\n if (!unclosableTags) {\n unclosableTags = []\n }\n\n mainLoop: for (const child of children) {\n for (const transformer of textMatchTransformers) {\n if (!transformer.export) {\n continue\n }\n\n const result = transformer.export(\n child,\n (parentNode) =>\n exportChildren(\n parentNode,\n textTransformersIndex,\n textMatchTransformers,\n unclosedTags,\n // Add current unclosed tags to the list of unclosable tags - we don't want nested tags from\n // textmatch transformers to close the outer ones, as that may result in invalid markdown.\n // E.g. **text [text**](https://lexical.io)\n // is invalid markdown, as the closing ** is inside the link.\n //\n [...unclosableTags, ...unclosedTags],\n ),\n (textNode, textContent) =>\n exportTextFormat(\n textNode,\n textContent,\n textTransformersIndex,\n unclosedTags,\n unclosableTags,\n ),\n )\n\n if (result != null) {\n output.push(result)\n continue mainLoop\n }\n }\n\n if ($isLineBreakNode(child)) {\n output.push('\\n')\n } else if ($isTextNode(child)) {\n output.push(\n exportTextFormat(\n child,\n child.getTextContent(),\n textTransformersIndex,\n unclosedTags,\n unclosableTags,\n ),\n )\n } else if ($isElementNode(child)) {\n // empty paragraph returns \"\"\n output.push(\n exportChildren(\n child,\n textTransformersIndex,\n textMatchTransformers,\n unclosedTags,\n unclosableTags,\n ),\n )\n } else if ($isDecoratorNode(child)) {\n output.push(child.getTextContent())\n }\n }\n\n return output.join('')\n}\n\nfunction exportTextFormat(\n node: TextNode,\n textContent: string,\n textTransformers: Array<TextFormatTransformer>,\n // unclosed tags include the markdown tags that haven't been closed yet, and their associated formats\n unclosedTags: Array<{ format: TextFormatType; tag: string }>,\n unclosableTags?: Array<{ format: TextFormatType; tag: string }>,\n): string {\n // This function handles the case of a string looking like this: \" foo \"\n // Where it would be invalid markdown to generate: \"** foo **\"\n // We instead want to trim the whitespace out, apply formatting, and then\n // bring the whitespace back. So our returned string looks like this: \" **foo** \"\n const frozenString = textContent.trim()\n let output = frozenString\n // the opening tags to be added to the result\n let openingTags = ''\n // the closing tags to be added to the result\n let closingTagsBefore = ''\n let closingTagsAfter = ''\n\n const prevNode = getTextSibling(node, true)\n const nextNode = getTextSibling(node, false)\n\n const applied = new Set()\n\n for (const transformer of textTransformers) {\n const format = transformer.format[0]\n const tag = transformer.tag\n\n // dedup applied formats\n if (hasFormat(node, format) && !applied.has(format)) {\n // Multiple tags might be used for the same format (*, _)\n applied.add(format)\n\n // append the tag to openningTags, if it's not applied to the previous nodes,\n // or the nodes before that (which would result in an unclosed tag)\n if (!hasFormat(prevNode, format) || !unclosedTags.find((element) => element.tag === tag)) {\n unclosedTags.push({ format, tag })\n openingTags += tag\n }\n }\n }\n\n // close any tags in the same order they were applied, if necessary\n for (let i = 0; i < unclosedTags.length; i++) {\n const nodeHasFormat = hasFormat(node, unclosedTags[i].format)\n const nextNodeHasFormat = hasFormat(nextNode, unclosedTags[i].format)\n\n // prevent adding closing tag if next sibling will do it\n if (nodeHasFormat && nextNodeHasFormat) {\n continue\n }\n\n const unhandledUnclosedTags = [...unclosedTags] // Shallow copy to avoid modifying the original array\n\n while (unhandledUnclosedTags.length > i) {\n const unclosedTag = unhandledUnclosedTags.pop()\n\n // If tag is unclosable, don't close it and leave it in the original array,\n // So that it can be closed when it's no longer unclosable\n if (\n unclosableTags &&\n unclosedTag &&\n unclosableTags.find((element) => element.tag === unclosedTag.tag)\n ) {\n continue\n }\n\n if (unclosedTag && typeof unclosedTag.tag === 'string') {\n if (!nodeHasFormat) {\n // Handles cases where the tag has not been closed before, e.g. if the previous node\n // was a text match transformer that did not account for closing tags of the next node (e.g. a link)\n closingTagsBefore += unclosedTag.tag\n } else if (!nextNodeHasFormat) {\n closingTagsAfter += unclosedTag.tag\n }\n }\n // Mutate the original array to remove the closed tag\n unclosedTags.pop()\n }\n break\n }\n\n output = openingTags + output + closingTagsAfter\n // Replace trimmed version of textContent ensuring surrounding whitespace is not modified\n return closingTagsBefore + textContent.replace(frozenString, () => output)\n}\n\n// Get next or previous text sibling a text node, including cases\n// when it's a child of inline element (e.g. link)\nfunction getTextSibling(node: TextNode, backward: boolean): null | TextNode {\n let sibling = backward ? node.getPreviousSibling() : node.getNextSibling()\n\n if (!sibling) {\n const parent = node.getParentOrThrow()\n\n if (parent.isInline()) {\n sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling()\n }\n }\n\n while (sibling) {\n if ($isElementNode(sibling)) {\n if (!sibling.isInline()) {\n break\n }\n\n const descendant = backward ? sibling.getLastDescendant() : sibling.getFirstDescendant()\n\n if ($isTextNode(descendant)) {\n return descendant\n } else {\n sibling = backward ? sibling.getPreviousSibling() : sibling.getNextSibling()\n }\n }\n\n if ($isTextNode(sibling)) {\n return sibling\n }\n\n if (!$isElementNode(sibling)) {\n return null\n }\n }\n\n return null\n}\n\nfunction hasFormat(node: LexicalNode | null | undefined, format: TextFormatType): boolean {\n return $isTextNode(node) && node.hasFormat(format)\n}\n"],"mappings":"AAAA;;;;;;GAUA,SAASA,QAAQ,EAAEC,gBAAgB,EAAEC,cAAc,EAAEC,gBAAgB,EAAEC,WAAW,QAAQ;AAU1F,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ;AAErD;;;AAGA,OAAO,SAASC,qBACdC,YAAgC,EAChCC,sBAAA,GAAkC,KAAK;EAEvC,MAAMC,MAAA,GAASJ,kBAAA,CAAmBE,YAAA;EAClC,MAAMG,mBAAA,GAAsB,C,GAAID,MAAA,CAAOE,gBAAgB,E,GAAKF,MAAA,CAAOG,OAAO,CAAC;EAC3E,MAAMC,kBAAA,GAAqB,CAACL,sBAAA;EAE5B;EACA;EACA,MAAMM,sBAAA,GAAyBL,MAAA,CAAOM,UAAU,CAACC,MAAM,CACpDC,WAAA,IAAgBA,WAAA,CAAYC,MAAM,CAACC,MAAM,KAAK;EAGjD,OAAQC,IAAA;IACN,MAAMC,MAAA,GAAS,EAAE;IACjB,MAAMC,QAAA,GAAW,CAACF,IAAA,IAAQrB,QAAA,EAAS,EAAGwB,WAAW;IAEjD,KAAK,IAAIC,CAAA,GAAI,GAAGA,CAAA,GAAIF,QAAA,CAASH,MAAM,EAAEK,CAAA,IAAK;MACxC,MAAMC,KAAA,GAAQH,QAAQ,CAACE,CAAA,CAAE;MACzB,MAAME,MAAA,GAASC,sBAAA,CACbF,KAAA,EACAf,mBAAA,EACAI,sBAAA,EACAL,MAAA,CAAOmB,SAAS;MAGlB,IAAIF,MAAA,IAAU,MAAM;QAClBL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AQO,iBAAiB,KAAKP,OAAA,CAAQQ,kBAAkB;MAEtF,IAAIzE,WAAA,CAAYuE,UAAA,GAAa;QAC3B,OAAOA,UAAA;MACT,OAAO;QACLN,OAAA,GAAUD,QAAA,GAAWC,OAAA,CAAQC,kBAAkB,KAAKD,OAAA,CAAQE,cAAc;MAC5E;IACF;IAEA,IAAInE,WAAA,CAAYiE,OAAA,GAAU;MACxB,OAAOA,OAAA;IACT;IAEA,IAAI,CAACnE,cAAA,CAAemE,OAAA,GAAU;MAC5B,OAAO;IACT;EACF;EAEA,OAAO;AACT;AAEA,SAASX,UAAUrC,IAAoC,EAAEF,MAAsB;EAC7E,OAAOf,WAAA,CAAYiB,IAAA,KAASA,IAAA,CAAKqC,SAAS,CAACvC,MAAA;AAC7C","ignoreList":[]}
|
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { ElementNode } from 'lexical';
|
|
9
|
-
import type { Transformer } from './MarkdownTransformers.js';
|
|
9
|
+
import type { TextFormatTransformer, Transformer } from './MarkdownTransformers.js';
|
|
10
|
+
export type TextFormatTransformersIndex = Readonly<{
|
|
11
|
+
fullMatchRegExpByTag: Readonly<Record<string, RegExp>>;
|
|
12
|
+
openTagsRegExp: RegExp;
|
|
13
|
+
transformersByTag: Readonly<Record<string, TextFormatTransformer>>;
|
|
14
|
+
}>;
|
|
10
15
|
/**
|
|
11
16
|
* Renders markdown from a string. The selection is moved to the start after the operation.
|
|
12
17
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownImport.d.ts","sourceRoot":"","sources":["../../../../src/packages/@lexical/markdown/MarkdownImport.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"MarkdownImport.d.ts","sourceRoot":"","sources":["../../../../src/packages/@lexical/markdown/MarkdownImport.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAc1C,OAAO,KAAK,EAGV,qBAAqB,EAErB,WAAW,EACZ,MAAM,2BAA2B,CAAA;AAMlC,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC;IACjD,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACtD,cAAc,EAAE,MAAM,CAAA;IACtB,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC,CAAA;CACnE,CAAC,CAAA;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,YAAY,EAAE,KAAK,CAAC,WAAW,CAAC,EAChC,sBAAsB,UAAQ,GAC7B,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,IAAI,CAyCtD"}
|
|
@@ -9,7 +9,8 @@ import { $isQuoteNode } from '@lexical/rich-text';
|
|
|
9
9
|
import { $findMatchingParent } from '@lexical/utils';
|
|
10
10
|
import { $createLineBreakNode, $createParagraphNode, $createTextNode, $getRoot, $getSelection, $isParagraphNode } from 'lexical';
|
|
11
11
|
import { IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '../../../lexical/utils/environment.js';
|
|
12
|
-
import {
|
|
12
|
+
import { importTextTransformers } from './importTextTransformers.js';
|
|
13
|
+
import { isEmptyParagraph, transformersByType } from './utils.js';
|
|
13
14
|
/**
|
|
14
15
|
* Renders markdown from a string. The selection is moved to the start after the operation.
|
|
15
16
|
*/
|
|
@@ -146,7 +147,7 @@ function $importBlocks(lineText, rootNode, elementTransformers, textFormatTransf
|
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
|
-
|
|
150
|
+
importTextTransformers(textNode, textFormatTransformersIndex, textMatchTransformers);
|
|
150
151
|
// If no transformer found and we left with original paragraph node
|
|
151
152
|
// can check if its content can be appended to the previous node
|
|
152
153
|
// if it's a paragraph, quote or list
|
|
@@ -169,123 +170,6 @@ function $importBlocks(lineText, rootNode, elementTransformers, textFormatTransf
|
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
|
-
// Processing text content and replaces text format tags.
|
|
173
|
-
// It takes outermost tag match and its content, creates text node with
|
|
174
|
-
// format based on tag and then recursively executed over node's content
|
|
175
|
-
//
|
|
176
|
-
// E.g. for "*Hello **world**!*" string it will create text node with
|
|
177
|
-
// "Hello **world**!" content and italic format and run recursively over
|
|
178
|
-
// its content to transform "**world**" part
|
|
179
|
-
function importTextFormatTransformers(textNode, textFormatTransformersIndex, textMatchTransformers) {
|
|
180
|
-
const textContent = textNode.getTextContent();
|
|
181
|
-
const match = findOutermostMatch(textContent, textFormatTransformersIndex);
|
|
182
|
-
if (!match) {
|
|
183
|
-
// Once text format processing is done run text match transformers, as it
|
|
184
|
-
// only can span within single text node (unline formats that can cover multiple nodes)
|
|
185
|
-
importTextMatchTransformers(textNode, textMatchTransformers);
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
let currentNode, leadingNode, remainderNode;
|
|
189
|
-
// If matching full content there's no need to run splitText and can reuse existing textNode
|
|
190
|
-
// to update its content and apply format. E.g. for **_Hello_** string after applying bold
|
|
191
|
-
// format (**) it will reuse the same text node to apply italic (_)
|
|
192
|
-
if (match[0] === textContent) {
|
|
193
|
-
currentNode = textNode;
|
|
194
|
-
} else {
|
|
195
|
-
const startIndex = match.index || 0;
|
|
196
|
-
const endIndex = startIndex + match[0].length;
|
|
197
|
-
if (startIndex === 0) {
|
|
198
|
-
[currentNode, remainderNode] = textNode.splitText(endIndex);
|
|
199
|
-
} else {
|
|
200
|
-
[leadingNode, currentNode, remainderNode] = textNode.splitText(startIndex, endIndex);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
currentNode.setTextContent(match[2]);
|
|
204
|
-
const transformer = textFormatTransformersIndex.transformersByTag[match[1]];
|
|
205
|
-
if (transformer) {
|
|
206
|
-
for (const format of transformer.format) {
|
|
207
|
-
if (!currentNode.hasFormat(format)) {
|
|
208
|
-
currentNode.toggleFormat(format);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
// Recursively run over inner text if it's not inline code
|
|
213
|
-
if (!currentNode.hasFormat('code')) {
|
|
214
|
-
importTextFormatTransformers(currentNode, textFormatTransformersIndex, textMatchTransformers);
|
|
215
|
-
}
|
|
216
|
-
// Run over leading/remaining text if any
|
|
217
|
-
if (leadingNode) {
|
|
218
|
-
importTextFormatTransformers(leadingNode, textFormatTransformersIndex, textMatchTransformers);
|
|
219
|
-
}
|
|
220
|
-
if (remainderNode) {
|
|
221
|
-
importTextFormatTransformers(remainderNode, textFormatTransformersIndex, textMatchTransformers);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
function importTextMatchTransformers(textNode_, textMatchTransformers) {
|
|
225
|
-
let textNode = textNode_;
|
|
226
|
-
mainLoop: while (textNode) {
|
|
227
|
-
for (const transformer of textMatchTransformers) {
|
|
228
|
-
if (!transformer.replace || !transformer.importRegExp) {
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const match = textNode.getTextContent().match(transformer.importRegExp);
|
|
232
|
-
if (!match) {
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
const startIndex = match.index || 0;
|
|
236
|
-
const endIndex = transformer.getEndIndex ? transformer.getEndIndex(textNode, match) : startIndex + match[0].length;
|
|
237
|
-
if (endIndex === false) {
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
let newTextNode, replaceNode;
|
|
241
|
-
if (startIndex === 0) {
|
|
242
|
-
[replaceNode, textNode] = textNode.splitText(endIndex);
|
|
243
|
-
} else {
|
|
244
|
-
[, replaceNode, newTextNode] = textNode.splitText(startIndex, endIndex);
|
|
245
|
-
}
|
|
246
|
-
if (newTextNode) {
|
|
247
|
-
importTextMatchTransformers(newTextNode, textMatchTransformers);
|
|
248
|
-
}
|
|
249
|
-
transformer.replace(replaceNode, match);
|
|
250
|
-
continue mainLoop;
|
|
251
|
-
}
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
// Finds first "<tag>content<tag>" match that is not nested into another tag
|
|
256
|
-
function findOutermostMatch(textContent, textTransformersIndex) {
|
|
257
|
-
const openTagsMatch = textContent.match(textTransformersIndex.openTagsRegExp);
|
|
258
|
-
if (openTagsMatch == null) {
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
for (const match of openTagsMatch) {
|
|
262
|
-
// Open tags reg exp might capture leading space so removing it
|
|
263
|
-
// before using match to find transformer
|
|
264
|
-
const tag = match.replace(/^\s/, '');
|
|
265
|
-
const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[tag];
|
|
266
|
-
if (fullMatchRegExp == null) {
|
|
267
|
-
continue;
|
|
268
|
-
}
|
|
269
|
-
const fullMatch = textContent.match(fullMatchRegExp);
|
|
270
|
-
const transformer = textTransformersIndex.transformersByTag[tag];
|
|
271
|
-
if (fullMatch != null && transformer != null) {
|
|
272
|
-
if (transformer.intraword !== false) {
|
|
273
|
-
return fullMatch;
|
|
274
|
-
}
|
|
275
|
-
// For non-intraword transformers checking if it's within a word
|
|
276
|
-
// or surrounded with space/punctuation/newline
|
|
277
|
-
const {
|
|
278
|
-
index = 0
|
|
279
|
-
} = fullMatch;
|
|
280
|
-
const beforeChar = textContent[index - 1];
|
|
281
|
-
const afterChar = textContent[index + fullMatch[0].length];
|
|
282
|
-
if ((!beforeChar || PUNCTUATION_OR_SPACE.test(beforeChar)) && (!afterChar || PUNCTUATION_OR_SPACE.test(afterChar))) {
|
|
283
|
-
return fullMatch;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return null;
|
|
288
|
-
}
|
|
289
173
|
function createTextFormatTransformersIndex(textTransformers) {
|
|
290
174
|
const transformersByTag = {};
|
|
291
175
|
const fullMatchRegExpByTag = {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownImport.js","names":["$isListItemNode","$isListNode","$isQuoteNode","$findMatchingParent","$createLineBreakNode","$createParagraphNode","$createTextNode","$getRoot","$getSelection","$isParagraphNode","IS_APPLE_WEBKIT","IS_IOS","IS_SAFARI","isEmptyParagraph","PUNCTUATION_OR_SPACE","transformersByType","createMarkdownImport","transformers","shouldPreserveNewLines","byType","textFormatTransformersIndex","createTextFormatTransformersIndex","textFormat","markdownString","node","lines","split","linesLength","length","root","clear","i","lineText","imported","shiftedIndex","$importMultiline","multilineElement","$importBlocks","element","textMatch","children","getChildren","child","getChildrenSize","remove","selectStart","startLineIndex","multilineElementTransformers","rootNode","transformer","handleImportAfterStartMatch","regExpEnd","regExpStart","replace","startMatch","match","result","regexpEndRegex","regExp","isEndOptional","optional","endLineIndex","endMatch","index","linesInBetween","push","slice","text","elementTransformers","textMatchTransformers","textNode","elementNode","append","setTextContent","importTextFormatTransformers","isAttached","previousNode","getPreviousSibling","targetNode","lastDescendant","getLastDescendant","getTextContentSize","splice","textContent","getTextContent","findOutermostMatch","importTextMatchTransformers","currentNode","leadingNode","remainderNode","startIndex","endIndex","splitText","transformersByTag","format","hasFormat","toggleFormat","textNode_","mainLoop","importRegExp","getEndIndex","newTextNode","replaceNode","textTransformersIndex","openTagsMatch","openTagsRegExp","tag","fullMatchRegExp","fullMatchRegExpByTag","fullMatch","intraword","beforeChar","afterChar","test","textTransformers","escapeRegExp","tagRegExp","RegExp","join"],"sources":["../../../../src/packages/@lexical/markdown/MarkdownImport.ts"],"sourcesContent":["/**\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport type { ListItemNode } from '@lexical/list'\nimport type { ElementNode, TextNode } from 'lexical'\n\nimport { $isListItemNode, $isListNode } from '@lexical/list'\nimport { $isQuoteNode } from '@lexical/rich-text'\nimport { $findMatchingParent } from '@lexical/utils'\nimport {\n $createLineBreakNode,\n $createParagraphNode,\n $createTextNode,\n $getRoot,\n $getSelection,\n $isParagraphNode,\n} from 'lexical'\n\nimport type {\n ElementTransformer,\n MultilineElementTransformer,\n TextFormatTransformer,\n TextMatchTransformer,\n Transformer,\n} from './MarkdownTransformers.js'\n\nimport { IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '../../../lexical/utils/environment.js'\nimport { isEmptyParagraph, PUNCTUATION_OR_SPACE, transformersByType } from './utils.js'\n\ntype TextFormatTransformersIndex = Readonly<{\n fullMatchRegExpByTag: Readonly<Record<string, RegExp>>\n openTagsRegExp: RegExp\n transformersByTag: Readonly<Record<string, TextFormatTransformer>>\n}>\n\n/**\n * Renders markdown from a string. The selection is moved to the start after the operation.\n */\nexport function createMarkdownImport(\n transformers: Array<Transformer>,\n shouldPreserveNewLines = false,\n): (markdownString: string, node?: ElementNode) => void {\n const byType = transformersByType(transformers)\n const textFormatTransformersIndex = createTextFormatTransformersIndex(byType.textFormat)\n\n return (markdownString, node) => {\n const lines = markdownString.split('\\n')\n const linesLength = lines.length\n const root = node || $getRoot()\n root.clear()\n\n for (let i = 0; i < linesLength; i++) {\n const lineText = lines[i]\n\n const [imported, shiftedIndex] = $importMultiline(lines, i, byType.multilineElement, root)\n\n if (imported) {\n // If a multiline markdown element was imported, we don't want to process the lines that were part of it anymore.\n // There could be other sub-markdown elements (both multiline and normal ones) matching within this matched multiline element's children.\n // However, it would be the responsibility of the matched multiline transformer to decide how it wants to handle them.\n // 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.\n i = shiftedIndex // Next loop will start from the line after the last line of the multiline element\n continue\n }\n\n $importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch)\n }\n\n // By default, removing empty paragraphs as md does not really\n // allow empty lines and uses them as delimiter.\n // If you need empty lines set shouldPreserveNewLines = true.\n const children = root.getChildren()\n for (const child of children) {\n if (!shouldPreserveNewLines && isEmptyParagraph(child) && root.getChildrenSize() > 1) {\n child.remove()\n }\n }\n\n if ($getSelection() !== null) {\n root.selectStart()\n }\n }\n}\n\n/**\n *\n * @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.\n */\nfunction $importMultiline(\n lines: Array<string>,\n startLineIndex: number,\n multilineElementTransformers: Array<MultilineElementTransformer>,\n rootNode: ElementNode,\n): [boolean, number] {\n for (const transformer of multilineElementTransformers) {\n const { handleImportAfterStartMatch, regExpEnd, regExpStart, replace } = transformer\n\n const startMatch = lines[startLineIndex].match(regExpStart)\n if (!startMatch) {\n continue // Try next transformer\n }\n\n if (handleImportAfterStartMatch) {\n const result = handleImportAfterStartMatch({\n lines,\n rootNode,\n startLineIndex,\n startMatch,\n transformer,\n })\n if (result === null) {\n continue\n } else if (result) {\n return result\n }\n }\n\n const regexpEndRegex: RegExp | undefined =\n typeof regExpEnd === 'object' && 'regExp' in regExpEnd ? regExpEnd.regExp : regExpEnd\n\n const isEndOptional =\n regExpEnd && typeof regExpEnd === 'object' && 'optional' in regExpEnd\n ? regExpEnd.optional\n : !regExpEnd\n\n let endLineIndex = startLineIndex\n const linesLength = lines.length\n\n // check every single line for the closing match. It could also be on the same line as the opening match.\n while (endLineIndex < linesLength) {\n const endMatch = regexpEndRegex ? lines[endLineIndex].match(regexpEndRegex) : null\n if (!endMatch) {\n if (\n !isEndOptional ||\n (isEndOptional && endLineIndex < linesLength - 1) // Optional end, but didn't reach the end of the document yet => continue searching for potential closing match\n ) {\n endLineIndex++\n continue // Search next line for closing match\n }\n }\n\n // Now, check if the closing match matched is the same as the opening match.\n // If it is, we need to continue searching for the actual closing match.\n if (endMatch && startLineIndex === endLineIndex && endMatch.index === startMatch.index) {\n endLineIndex++\n continue // Search next line for closing match\n }\n\n // At this point, we have found the closing match. Next: calculate the lines in between open and closing match\n // This should not include the matches themselves, and be split up by lines\n const linesInBetween: string[] = []\n\n if (endMatch && startLineIndex === endLineIndex) {\n linesInBetween.push(lines[startLineIndex].slice(startMatch[0].length, -endMatch[0].length))\n } else {\n for (let i = startLineIndex; i <= endLineIndex; i++) {\n if (i === startLineIndex) {\n const text = lines[i].slice(startMatch[0].length)\n linesInBetween.push(text) // Also include empty text\n } else if (i === endLineIndex && endMatch) {\n const text = lines[i].slice(0, -endMatch[0].length)\n linesInBetween.push(text) // Also include empty text\n } else {\n linesInBetween.push(lines[i])\n }\n }\n }\n\n if (replace(rootNode, null, startMatch, endMatch, linesInBetween, true) !== false) {\n // Return here. This $importMultiline function is run line by line and should only process a single multiline element at a time.\n return [true, endLineIndex]\n }\n\n // The replace function returned false, despite finding the matching open and close tags => this transformer does not want to handle it.\n // Thus, we continue letting the remaining transformers handle the passed lines of text from the beginning\n break\n }\n }\n\n // No multiline transformer handled this line successfully\n return [false, startLineIndex]\n}\n\nfunction $importBlocks(\n lineText: string,\n rootNode: ElementNode,\n elementTransformers: Array<ElementTransformer>,\n textFormatTransformersIndex: TextFormatTransformersIndex,\n textMatchTransformers: Array<TextMatchTransformer>,\n) {\n const textNode = $createTextNode(lineText)\n const elementNode = $createParagraphNode()\n elementNode.append(textNode)\n rootNode.append(elementNode)\n\n for (const { regExp, replace } of elementTransformers) {\n const match = lineText.match(regExp)\n\n if (match) {\n textNode.setTextContent(lineText.slice(match[0].length))\n if (replace(elementNode, [textNode], match, true) !== false) {\n break\n }\n }\n }\n\n importTextFormatTransformers(textNode, textFormatTransformersIndex, textMatchTransformers)\n\n // If no transformer found and we left with original paragraph node\n // can check if its content can be appended to the previous node\n // if it's a paragraph, quote or list\n if (elementNode.isAttached() && lineText.length > 0) {\n const previousNode = elementNode.getPreviousSibling()\n if ($isParagraphNode(previousNode) || $isQuoteNode(previousNode) || $isListNode(previousNode)) {\n let targetNode: ListItemNode | null | typeof previousNode = previousNode\n\n if ($isListNode(previousNode)) {\n const lastDescendant = previousNode.getLastDescendant()\n if (lastDescendant == null) {\n targetNode = null\n } else {\n targetNode = $findMatchingParent(lastDescendant, $isListItemNode)\n }\n }\n\n if (targetNode != null && targetNode.getTextContentSize() > 0) {\n targetNode.splice(targetNode.getChildrenSize(), 0, [\n $createLineBreakNode(),\n ...elementNode.getChildren(),\n ])\n elementNode.remove()\n }\n }\n }\n}\n\n// Processing text content and replaces text format tags.\n// It takes outermost tag match and its content, creates text node with\n// format based on tag and then recursively executed over node's content\n//\n// E.g. for \"*Hello **world**!*\" string it will create text node with\n// \"Hello **world**!\" content and italic format and run recursively over\n// its content to transform \"**world**\" part\nfunction importTextFormatTransformers(\n textNode: TextNode,\n textFormatTransformersIndex: TextFormatTransformersIndex,\n textMatchTransformers: Array<TextMatchTransformer>,\n) {\n const textContent = textNode.getTextContent()\n const match = findOutermostMatch(textContent, textFormatTransformersIndex)\n\n if (!match) {\n // Once text format processing is done run text match transformers, as it\n // only can span within single text node (unline formats that can cover multiple nodes)\n importTextMatchTransformers(textNode, textMatchTransformers)\n return\n }\n\n let currentNode, leadingNode, remainderNode\n\n // If matching full content there's no need to run splitText and can reuse existing textNode\n // to update its content and apply format. E.g. for **_Hello_** string after applying bold\n // format (**) it will reuse the same text node to apply italic (_)\n if (match[0] === textContent) {\n currentNode = textNode\n } else {\n const startIndex = match.index || 0\n const endIndex = startIndex + match[0].length\n\n if (startIndex === 0) {\n ;[currentNode, remainderNode] = textNode.splitText(endIndex)\n } else {\n ;[leadingNode, currentNode, remainderNode] = textNode.splitText(startIndex, endIndex)\n }\n }\n\n currentNode.setTextContent(match[2])\n const transformer = textFormatTransformersIndex.transformersByTag[match[1]]\n\n if (transformer) {\n for (const format of transformer.format) {\n if (!currentNode.hasFormat(format)) {\n currentNode.toggleFormat(format)\n }\n }\n }\n\n // Recursively run over inner text if it's not inline code\n if (!currentNode.hasFormat('code')) {\n importTextFormatTransformers(currentNode, textFormatTransformersIndex, textMatchTransformers)\n }\n\n // Run over leading/remaining text if any\n if (leadingNode) {\n importTextFormatTransformers(leadingNode, textFormatTransformersIndex, textMatchTransformers)\n }\n\n if (remainderNode) {\n importTextFormatTransformers(remainderNode, textFormatTransformersIndex, textMatchTransformers)\n }\n}\n\nfunction importTextMatchTransformers(\n textNode_: TextNode,\n textMatchTransformers: Array<TextMatchTransformer>,\n) {\n let textNode = textNode_\n\n mainLoop: while (textNode) {\n for (const transformer of textMatchTransformers) {\n if (!transformer.replace || !transformer.importRegExp) {\n continue\n }\n const match = textNode.getTextContent().match(transformer.importRegExp)\n\n if (!match) {\n continue\n }\n\n const startIndex = match.index || 0\n const endIndex = transformer.getEndIndex\n ? transformer.getEndIndex(textNode, match)\n : startIndex + match[0].length\n\n if (endIndex === false) {\n continue\n }\n\n let newTextNode, replaceNode\n\n if (startIndex === 0) {\n ;[replaceNode, textNode] = textNode.splitText(endIndex)\n } else {\n ;[, replaceNode, newTextNode] = textNode.splitText(startIndex, endIndex)\n }\n\n if (newTextNode) {\n importTextMatchTransformers(newTextNode, textMatchTransformers)\n }\n transformer.replace(replaceNode, match)\n continue mainLoop\n }\n\n break\n }\n}\n\n// Finds first \"<tag>content<tag>\" match that is not nested into another tag\nfunction findOutermostMatch(\n textContent: string,\n textTransformersIndex: TextFormatTransformersIndex,\n): null | RegExpMatchArray {\n const openTagsMatch = textContent.match(textTransformersIndex.openTagsRegExp)\n\n if (openTagsMatch == null) {\n return null\n }\n\n for (const match of openTagsMatch) {\n // Open tags reg exp might capture leading space so removing it\n // before using match to find transformer\n const tag = match.replace(/^\\s/, '')\n const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[tag]\n if (fullMatchRegExp == null) {\n continue\n }\n\n const fullMatch = textContent.match(fullMatchRegExp)\n const transformer = textTransformersIndex.transformersByTag[tag]\n if (fullMatch != null && transformer != null) {\n if (transformer.intraword !== false) {\n return fullMatch\n }\n\n // For non-intraword transformers checking if it's within a word\n // or surrounded with space/punctuation/newline\n const { index = 0 } = fullMatch\n const beforeChar = textContent[index - 1]\n const afterChar = textContent[index + fullMatch[0].length]\n\n if (\n (!beforeChar || PUNCTUATION_OR_SPACE.test(beforeChar)) &&\n (!afterChar || PUNCTUATION_OR_SPACE.test(afterChar))\n ) {\n return fullMatch\n }\n }\n }\n\n return null\n}\n\nfunction createTextFormatTransformersIndex(\n textTransformers: Array<TextFormatTransformer>,\n): TextFormatTransformersIndex {\n const transformersByTag: Record<string, TextFormatTransformer> = {}\n const fullMatchRegExpByTag: Record<string, RegExp> = {}\n const openTagsRegExp: string[] = []\n const escapeRegExp = `(?<![\\\\\\\\])`\n\n for (const transformer of textTransformers) {\n const { tag } = transformer\n transformersByTag[tag] = transformer\n const tagRegExp = tag.replace(/([*^+])/g, '\\\\$1')\n openTagsRegExp.push(tagRegExp)\n\n if (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT) {\n fullMatchRegExpByTag[tag] = new RegExp(\n `(${tagRegExp})(?![${tagRegExp}\\\\s])(.*?[^${tagRegExp}\\\\s])${tagRegExp}(?!${tagRegExp})`,\n )\n } else {\n fullMatchRegExpByTag[tag] = new RegExp(\n `(?<![\\\\\\\\${tagRegExp}])(${tagRegExp})((\\\\\\\\${tagRegExp})?.*?[^${tagRegExp}\\\\s](\\\\\\\\${tagRegExp})?)((?<!\\\\\\\\)|(?<=\\\\\\\\\\\\\\\\))(${tagRegExp})(?![\\\\\\\\${tagRegExp}])`,\n )\n }\n }\n\n return {\n // Reg exp to find open tag + content + close tag\n fullMatchRegExpByTag,\n // Reg exp to find opening tags\n openTagsRegExp: new RegExp(\n (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT ? '' : `${escapeRegExp}`) +\n '(' +\n openTagsRegExp.join('|') +\n ')',\n 'g',\n ),\n transformersByTag,\n }\n}\n"],"mappings":"AAAA;;;;;;GAWA,SAASA,eAAe,EAAEC,WAAW,QAAQ;AAC7C,SAASC,YAAY,QAAQ;AAC7B,SAASC,mBAAmB,QAAQ;AACpC,SACEC,oBAAoB,EACpBC,oBAAoB,EACpBC,eAAe,EACfC,QAAQ,EACRC,aAAa,EACbC,gBAAgB,QACX;AAUP,SAASC,eAAe,EAAEC,MAAM,EAAEC,SAA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AI,CAACD,SAAA,CAAS,GAClD;QACA,OAAOH,SAAA;MACT;IACF;EACF;EAEA,OAAO;AACT;AAEA,SAASxF,kCACP6F,gBAA8C;EAE9C,MAAMrB,iBAAA,GAA2D,CAAC;EAClE,MAAMe,oBAAA,GAA+C,CAAC;EACtD,MAAMH,cAAA,GAA2B,EAAE;EACnC,MAAMU,YAAA,GAAe,aAAa;EAElC,KAAK,MAAMlE,WAAA,IAAeiE,gBAAA,EAAkB;IAC1C,MAAM;MAAER;IAAG,CAAE,GAAGzD,WAAA;IAChB4C,iBAAiB,CAACa,GAAA,CAAI,GAAGzD,WAAA;IACzB,MAAMmE,SAAA,GAAYV,GAAA,CAAIrD,OAAO,CAAC,YAAY;IAC1CoD,cAAA,CAAexC,IAAI,CAACmD,SAAA;IAEpB,IAAIxG,SAAA,IAAaD,MAAA,IAAUD,eAAA,EAAiB;MAC1CkG,oBAAoB,CAACF,GAAA,CAAI,GAAG,IAAIW,MAAA,CAC9B,IAAID,SAAA,QAAiBA,SAAA,cAAuBA,SAAA,QAAiBA,SAAA,MAAeA,SAAA,GAAY;IAE5F,OAAO;MACLR,oBAAoB,CAACF,GAAA,CAAI,GAAG,IAAIW,MAAA,CAC9B,YAAYD,SAAA,MAAeA,SAAA,UAAmBA,SAAA,UAAmBA,SAAA,YAAqBA,SAAA,gCAAyCA,SAAA,YAAqBA,SAAA,IAAa;IAErK;EACF;EAEA,OAAO;IACL;IACAR,oBAAA;IACA;IACAH,cAAA,EAAgB,IAAIY,MAAA,CAClB,CAACzG,SAAA,IAAaD,MAAA,IAAUD,eAAA,GAAkB,KAAK,GAAGyG,YAAA,EAAc,IAC9D,MACAV,cAAA,CAAea,IAAI,CAAC,OACpB,KACF;IAEFzB;EACF;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"MarkdownImport.js","names":["$isListItemNode","$isListNode","$isQuoteNode","$findMatchingParent","$createLineBreakNode","$createParagraphNode","$createTextNode","$getRoot","$getSelection","$isParagraphNode","IS_APPLE_WEBKIT","IS_IOS","IS_SAFARI","importTextTransformers","isEmptyParagraph","transformersByType","createMarkdownImport","transformers","shouldPreserveNewLines","byType","textFormatTransformersIndex","createTextFormatTransformersIndex","textFormat","markdownString","node","lines","split","linesLength","length","root","clear","i","lineText","imported","shiftedIndex","$importMultiline","multilineElement","$importBlocks","element","textMatch","children","getChildren","child","getChildrenSize","remove","selectStart","startLineIndex","multilineElementTransformers","rootNode","transformer","handleImportAfterStartMatch","regExpEnd","regExpStart","replace","startMatch","match","result","regexpEndRegex","regExp","isEndOptional","optional","endLineIndex","endMatch","index","linesInBetween","push","slice","text","elementTransformers","textMatchTransformers","textNode","elementNode","append","setTextContent","isAttached","previousNode","getPreviousSibling","targetNode","lastDescendant","getLastDescendant","getTextContentSize","splice","textTransformers","transformersByTag","fullMatchRegExpByTag","openTagsRegExp","escapeRegExp","tag","tagRegExp","RegExp","join"],"sources":["../../../../src/packages/@lexical/markdown/MarkdownImport.ts"],"sourcesContent":["/**\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport type { ListItemNode } from '@lexical/list'\nimport type { ElementNode } from 'lexical'\n\nimport { $isListItemNode, $isListNode } from '@lexical/list'\nimport { $isQuoteNode } from '@lexical/rich-text'\nimport { $findMatchingParent } from '@lexical/utils'\nimport {\n $createLineBreakNode,\n $createParagraphNode,\n $createTextNode,\n $getRoot,\n $getSelection,\n $isParagraphNode,\n} from 'lexical'\n\nimport type {\n ElementTransformer,\n MultilineElementTransformer,\n TextFormatTransformer,\n TextMatchTransformer,\n Transformer,\n} from './MarkdownTransformers.js'\n\nimport { IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '../../../lexical/utils/environment.js'\nimport { importTextTransformers } from './importTextTransformers.js'\nimport { isEmptyParagraph, transformersByType } from './utils.js'\n\nexport type TextFormatTransformersIndex = Readonly<{\n fullMatchRegExpByTag: Readonly<Record<string, RegExp>>\n openTagsRegExp: RegExp\n transformersByTag: Readonly<Record<string, TextFormatTransformer>>\n}>\n\n/**\n * Renders markdown from a string. The selection is moved to the start after the operation.\n */\nexport function createMarkdownImport(\n transformers: Array<Transformer>,\n shouldPreserveNewLines = false,\n): (markdownString: string, node?: ElementNode) => void {\n const byType = transformersByType(transformers)\n const textFormatTransformersIndex = createTextFormatTransformersIndex(byType.textFormat)\n\n return (markdownString, node) => {\n const lines = markdownString.split('\\n')\n const linesLength = lines.length\n const root = node || $getRoot()\n root.clear()\n\n for (let i = 0; i < linesLength; i++) {\n const lineText = lines[i]\n\n const [imported, shiftedIndex] = $importMultiline(lines, i, byType.multilineElement, root)\n\n if (imported) {\n // If a multiline markdown element was imported, we don't want to process the lines that were part of it anymore.\n // There could be other sub-markdown elements (both multiline and normal ones) matching within this matched multiline element's children.\n // However, it would be the responsibility of the matched multiline transformer to decide how it wants to handle them.\n // 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.\n i = shiftedIndex // Next loop will start from the line after the last line of the multiline element\n continue\n }\n\n $importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch)\n }\n\n // By default, removing empty paragraphs as md does not really\n // allow empty lines and uses them as delimiter.\n // If you need empty lines set shouldPreserveNewLines = true.\n const children = root.getChildren()\n for (const child of children) {\n if (!shouldPreserveNewLines && isEmptyParagraph(child) && root.getChildrenSize() > 1) {\n child.remove()\n }\n }\n\n if ($getSelection() !== null) {\n root.selectStart()\n }\n }\n}\n\n/**\n *\n * @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.\n */\nfunction $importMultiline(\n lines: Array<string>,\n startLineIndex: number,\n multilineElementTransformers: Array<MultilineElementTransformer>,\n rootNode: ElementNode,\n): [boolean, number] {\n for (const transformer of multilineElementTransformers) {\n const { handleImportAfterStartMatch, regExpEnd, regExpStart, replace } = transformer\n\n const startMatch = lines[startLineIndex].match(regExpStart)\n if (!startMatch) {\n continue // Try next transformer\n }\n\n if (handleImportAfterStartMatch) {\n const result = handleImportAfterStartMatch({\n lines,\n rootNode,\n startLineIndex,\n startMatch,\n transformer,\n })\n if (result === null) {\n continue\n } else if (result) {\n return result\n }\n }\n\n const regexpEndRegex: RegExp | undefined =\n typeof regExpEnd === 'object' && 'regExp' in regExpEnd ? regExpEnd.regExp : regExpEnd\n\n const isEndOptional =\n regExpEnd && typeof regExpEnd === 'object' && 'optional' in regExpEnd\n ? regExpEnd.optional\n : !regExpEnd\n\n let endLineIndex = startLineIndex\n const linesLength = lines.length\n\n // check every single line for the closing match. It could also be on the same line as the opening match.\n while (endLineIndex < linesLength) {\n const endMatch = regexpEndRegex ? lines[endLineIndex].match(regexpEndRegex) : null\n if (!endMatch) {\n if (\n !isEndOptional ||\n (isEndOptional && endLineIndex < linesLength - 1) // Optional end, but didn't reach the end of the document yet => continue searching for potential closing match\n ) {\n endLineIndex++\n continue // Search next line for closing match\n }\n }\n\n // Now, check if the closing match matched is the same as the opening match.\n // If it is, we need to continue searching for the actual closing match.\n if (endMatch && startLineIndex === endLineIndex && endMatch.index === startMatch.index) {\n endLineIndex++\n continue // Search next line for closing match\n }\n\n // At this point, we have found the closing match. Next: calculate the lines in between open and closing match\n // This should not include the matches themselves, and be split up by lines\n const linesInBetween: string[] = []\n\n if (endMatch && startLineIndex === endLineIndex) {\n linesInBetween.push(lines[startLineIndex].slice(startMatch[0].length, -endMatch[0].length))\n } else {\n for (let i = startLineIndex; i <= endLineIndex; i++) {\n if (i === startLineIndex) {\n const text = lines[i].slice(startMatch[0].length)\n linesInBetween.push(text) // Also include empty text\n } else if (i === endLineIndex && endMatch) {\n const text = lines[i].slice(0, -endMatch[0].length)\n linesInBetween.push(text) // Also include empty text\n } else {\n linesInBetween.push(lines[i])\n }\n }\n }\n\n if (replace(rootNode, null, startMatch, endMatch, linesInBetween, true) !== false) {\n // Return here. This $importMultiline function is run line by line and should only process a single multiline element at a time.\n return [true, endLineIndex]\n }\n\n // The replace function returned false, despite finding the matching open and close tags => this transformer does not want to handle it.\n // Thus, we continue letting the remaining transformers handle the passed lines of text from the beginning\n break\n }\n }\n\n // No multiline transformer handled this line successfully\n return [false, startLineIndex]\n}\n\nfunction $importBlocks(\n lineText: string,\n rootNode: ElementNode,\n elementTransformers: Array<ElementTransformer>,\n textFormatTransformersIndex: TextFormatTransformersIndex,\n textMatchTransformers: Array<TextMatchTransformer>,\n) {\n const textNode = $createTextNode(lineText)\n const elementNode = $createParagraphNode()\n elementNode.append(textNode)\n rootNode.append(elementNode)\n\n for (const { regExp, replace } of elementTransformers) {\n const match = lineText.match(regExp)\n\n if (match) {\n textNode.setTextContent(lineText.slice(match[0].length))\n if (replace(elementNode, [textNode], match, true) !== false) {\n break\n }\n }\n }\n\n importTextTransformers(textNode, textFormatTransformersIndex, textMatchTransformers)\n\n // If no transformer found and we left with original paragraph node\n // can check if its content can be appended to the previous node\n // if it's a paragraph, quote or list\n if (elementNode.isAttached() && lineText.length > 0) {\n const previousNode = elementNode.getPreviousSibling()\n if ($isParagraphNode(previousNode) || $isQuoteNode(previousNode) || $isListNode(previousNode)) {\n let targetNode: ListItemNode | null | typeof previousNode = previousNode\n\n if ($isListNode(previousNode)) {\n const lastDescendant = previousNode.getLastDescendant()\n if (lastDescendant == null) {\n targetNode = null\n } else {\n targetNode = $findMatchingParent(lastDescendant, $isListItemNode)\n }\n }\n\n if (targetNode != null && targetNode.getTextContentSize() > 0) {\n targetNode.splice(targetNode.getChildrenSize(), 0, [\n $createLineBreakNode(),\n ...elementNode.getChildren(),\n ])\n elementNode.remove()\n }\n }\n }\n}\n\nfunction createTextFormatTransformersIndex(\n textTransformers: Array<TextFormatTransformer>,\n): TextFormatTransformersIndex {\n const transformersByTag: Record<string, TextFormatTransformer> = {}\n const fullMatchRegExpByTag: Record<string, RegExp> = {}\n const openTagsRegExp: string[] = []\n const escapeRegExp = `(?<![\\\\\\\\])`\n\n for (const transformer of textTransformers) {\n const { tag } = transformer\n transformersByTag[tag] = transformer\n const tagRegExp = tag.replace(/([*^+])/g, '\\\\$1')\n openTagsRegExp.push(tagRegExp)\n\n if (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT) {\n fullMatchRegExpByTag[tag] = new RegExp(\n `(${tagRegExp})(?![${tagRegExp}\\\\s])(.*?[^${tagRegExp}\\\\s])${tagRegExp}(?!${tagRegExp})`,\n )\n } else {\n fullMatchRegExpByTag[tag] = new RegExp(\n `(?<![\\\\\\\\${tagRegExp}])(${tagRegExp})((\\\\\\\\${tagRegExp})?.*?[^${tagRegExp}\\\\s](\\\\\\\\${tagRegExp})?)((?<!\\\\\\\\)|(?<=\\\\\\\\\\\\\\\\))(${tagRegExp})(?![\\\\\\\\${tagRegExp}])`,\n )\n }\n }\n\n return {\n // Reg exp to find open tag + content + close tag\n fullMatchRegExpByTag,\n // Reg exp to find opening tags\n openTagsRegExp: new RegExp(\n (IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT ? '' : `${escapeRegExp}`) +\n '(' +\n openTagsRegExp.join('|') +\n ')',\n 'g',\n ),\n transformersByTag,\n }\n}\n"],"mappings":"AAAA;;;;;;GAWA,SAASA,eAAe,EAAEC,WAAW,QAAQ;AAC7C,SAASC,YAAY,QAAQ;AAC7B,SAASC,mBAAmB,QAAQ;AACpC,SACEC,oBAAoB,EACpBC,oBAAoB,EACpBC,eAAe,EACfC,QAAQ,EACRC,aAAa,EACbC,gBAAgB,QACX;AAUP,SAASC,eAAe,EAAEC,MAAM,EAAEC,SAAS,QAAQ;AACnD,SAASC,sBAAsB,QAAQ;AACvC,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ;AAQrD;;;AAGA,OAAO,SAASC,qBACdC,YAAgC,EAChCC,sBAAA,GAAyB,KAAK;EAE9B,MAAMC,MAAA,GAASJ,kBAAA,CAAmBE,YAAA;EAClC,MAAMG,2BAAA,GAA8BC,iCAAA,CAAkCF,MAAA,CAAOG,UAAU;EAEvF,OAAO,CAACC,cAAA,EAAgBC,IAAA;IACtB,MAA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AElC,KAAK,MAAMrC,WAAA,IAAeiC,gBAAA,EAAkB;IAC1C,MAAM;MAAEK;IAAG,CAAE,GAAGtC,WAAA;IAChBkC,iBAAiB,CAACI,GAAA,CAAI,GAAGtC,WAAA;IACzB,MAAMuC,SAAA,GAAYD,GAAA,CAAIlC,OAAO,CAAC,YAAY;IAC1CgC,cAAA,CAAepB,IAAI,CAACuB,SAAA;IAEpB,IAAI5E,SAAA,IAAaD,MAAA,IAAUD,eAAA,EAAiB;MAC1C0E,oBAAoB,CAACG,GAAA,CAAI,GAAG,IAAIE,MAAA,CAC9B,IAAID,SAAA,QAAiBA,SAAA,cAAuBA,SAAA,QAAiBA,SAAA,MAAeA,SAAA,GAAY;IAE5F,OAAO;MACLJ,oBAAoB,CAACG,GAAA,CAAI,GAAG,IAAIE,MAAA,CAC9B,YAAYD,SAAA,MAAeA,SAAA,UAAmBA,SAAA,UAAmBA,SAAA,YAAqBA,SAAA,gCAAyCA,SAAA,YAAqBA,SAAA,IAAa;IAErK;EACF;EAEA,OAAO;IACL;IACAJ,oBAAA;IACA;IACAC,cAAA,EAAgB,IAAII,MAAA,CAClB,CAAC7E,SAAA,IAAaD,MAAA,IAAUD,eAAA,GAAkB,KAAK,GAAG4E,YAAA,EAAc,IAC9D,MACAD,cAAA,CAAeK,IAAI,CAAC,OACpB,KACF;IAEFP;EACF;AACF","ignoreList":[]}
|
|
@@ -117,8 +117,11 @@ export type TextMatchTransformer = Readonly<{
|
|
|
117
117
|
regExp: RegExp;
|
|
118
118
|
/**
|
|
119
119
|
* Determines how the matched markdown text should be transformed into a node during the markdown import process
|
|
120
|
+
*
|
|
121
|
+
* @returns nothing, or a TextNode that may be a child of the new node that is created.
|
|
122
|
+
* If a TextNode is returned, text format matching will be applied to it (e.g. bold, italic, etc.)
|
|
120
123
|
*/
|
|
121
|
-
replace?: (node: TextNode, match: RegExpMatchArray) => void;
|
|
124
|
+
replace?: (node: TextNode, match: RegExpMatchArray) => TextNode | void;
|
|
122
125
|
/**
|
|
123
126
|
* Single character that allows the transformer to trigger when typed in the editor. This does not affect markdown imports outside of the markdown shortcut plugin.
|
|
124
127
|
* If the trigger is matched, the `regExp` will be used to match the text in the second step.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownTransformers.d.ts","sourceRoot":"","sources":["../../../../src/packages/@lexical/markdown/MarkdownTransformers.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAoBxF,MAAM,MAAM,WAAW,GACnB,kBAAkB,GAClB,2BAA2B,GAC3B,qBAAqB,GACrB,oBAAoB,CAAA;AAExB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAA;IACvC;;;;OAIG;IACH,MAAM,EAAE,CACN,IAAI,EAAE,WAAW,EAEjB,gBAAgB,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,KAC5C,IAAI,GAAG,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;IACd;;;;OAIG;IACH,OAAO,EAAE,CACP,UAAU,EAAE,WAAW,EACvB,QAAQ,EAAE,KAAK,CAAC,WAAW,CAAC,EAC5B,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC;IACpB;;OAEG;IACH,QAAQ,EAAE,OAAO,KACd,OAAO,GAAG,IAAI,CAAA;IACnB,IAAI,EAAE,SAAS,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,2BAA2B,GAAG;IACxC,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAA;IACvC;;;;OAIG;IACH,MAAM,CAAC,EAAE,CACP,IAAI,EAAE,WAAW,EAEjB,gBAAgB,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,KAC5C,IAAI,GAAG,MAAM,CA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AM,CAAC,EAAE,CACP,IAAI,EAAE,WAAW,EAEjB,cAAc,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,EAE7C,YAAY,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,KAAK,MAAM,KAC1D,IAAI,GAAG,MAAM,CAAA;IAClB;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,gBAAgB,KAAK,KAAK,GAAG,MAAM,CAAA;IACzE;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd
|
|
1
|
+
{"version":3,"file":"MarkdownTransformers.d.ts","sourceRoot":"","sources":["../../../../src/packages/@lexical/markdown/MarkdownTransformers.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAoBxF,MAAM,MAAM,WAAW,GACnB,kBAAkB,GAClB,2BAA2B,GAC3B,qBAAqB,GACrB,oBAAoB,CAAA;AAExB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAA;IACvC;;;;OAIG;IACH,MAAM,EAAE,CACN,IAAI,EAAE,WAAW,EAEjB,gBAAgB,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,MAAM,KAC5C,IAAI,GAAG,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;IACd;;;;OAIG;IACH,OAAO,EAAE,CACP,UAAU,EAAE,WAAW,EACvB,QAAQ,EAAE,KAAK,CAAC,WAAW,CAAC,EAC5B,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC;IACpB;;OAEG;IACH,QAAQ,EAAE,OAAO,KACd,OAAO,GAAG,IAAI,CAAA;IACnB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