Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-markdown] Feature: add ability to hook into the import process for multiline element transformers #6682

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,30 @@ function $importMultiline(
multilineElementTransformers: Array<MultilineElementTransformer>,
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
Expand Down
13 changes: 13 additions & 0 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks safe since this is optional, wdyt about adding a test case to demo a simple usecase of this fn?

lines: Array<string>;
rootNode: ElementNode;
startLineIndex: number;
startMatch: RegExpMatchArray;
transformer: MultilineElementTransformer;
}) => [boolean, number] | null | undefined;
dependencies: Array<Klass<LexicalNode>>;
/**
* `export` is called when the `$convertToMarkdownString` is called to convert the editor state into markdown.
Expand Down
Loading