diff --git a/packages/lexical-markdown/src/MarkdownImport.ts b/packages/lexical-markdown/src/MarkdownImport.ts index 99b7900b144..2f7dc27324b 100644 --- a/packages/lexical-markdown/src/MarkdownImport.ts +++ b/packages/lexical-markdown/src/MarkdownImport.ts @@ -117,16 +117,30 @@ function $importMultiline( multilineElementTransformers: Array, rootNode: ElementNode, ): [boolean, number] { - for (const { - regExpStart, - regExpEnd, - replace, - } of multilineElementTransformers) { + 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 diff --git a/packages/lexical-markdown/src/MarkdownTransformers.ts b/packages/lexical-markdown/src/MarkdownTransformers.ts index b724b3ba0ca..2a335156213 100644 --- a/packages/lexical-markdown/src/MarkdownTransformers.ts +++ b/packages/lexical-markdown/src/MarkdownTransformers.ts @@ -75,6 +75,19 @@ export type ElementTransformer = { }; export type MultilineElementTransformer = { + /** + * Use this function to manually handle the import process, once the `regExpStart` has matched successfully. + * Without providing this function, the default behavior is to match until `regExpEnd` is found, or until the end of the document if `regExpEnd.optional` is true. + * + * @returns a tuple or null. The 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. If null is returned, the next multilineElementTransformer will be tried. If undefined is returned, the default behavior will be used. + */ + handleImportAfterStartMatch?: (args: { + lines: Array; + rootNode: ElementNode; + startLineIndex: number; + startMatch: RegExpMatchArray; + transformer: MultilineElementTransformer; + }) => [boolean, number] | null | undefined; dependencies: Array>; /** * `export` is called when the `$convertToMarkdownString` is called to convert the editor state into markdown.