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]: Refactor: allows omitting certain properties from TextMatchTransformers, adds jsdocs #6651

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions packages/lexical-markdown/src/MarkdownExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function exportChildren(

mainLoop: for (const child of children) {
for (const transformer of textMatchTransformers) {
if (!transformer.export) {
continue;
}

const result = transformer.export(
child,
(parentNode) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ function importTextMatchTransformers(

mainLoop: while (textNode) {
for (const transformer of textMatchTransformers) {
if (!transformer.replace || !transformer.importRegExp) {
continue;
}
const match = textNode.getTextContent().match(transformer.importRegExp);

if (!match) {
Expand Down
11 changes: 7 additions & 4 deletions packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function runTextMatchTransformers(
}

for (const transformer of transformers) {
if (!transformer.replace || !transformer.regExp) {
continue;
}
const match = textContent.match(transformer.regExp);

if (match === null) {
Expand Down Expand Up @@ -389,11 +392,11 @@ export function registerMarkdownShortcuts(
transformers: Array<Transformer> = TRANSFORMERS,
): () => void {
const byType = transformersByType(transformers);
const textFormatTransformersIndex = indexBy(
const textFormatTransformersByTrigger = indexBy(
byType.textFormat,
({tag}) => tag[tag.length - 1],
);
const textMatchTransformersIndex = indexBy(
const textMatchTransformersByTrigger = indexBy(
byType.textMatch,
({trigger}) => trigger,
);
Expand Down Expand Up @@ -449,7 +452,7 @@ export function registerMarkdownShortcuts(
runTextMatchTransformers(
anchorNode,
anchorOffset,
textMatchTransformersIndex,
textMatchTransformersByTrigger,
)
) {
return;
Expand All @@ -458,7 +461,7 @@ export function registerMarkdownShortcuts(
$runTextFormatTransformers(
anchorNode,
anchorOffset,
textFormatTransformersIndex,
textFormatTransformersByTrigger,
);
};

Expand Down
24 changes: 20 additions & 4 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,33 @@ export type TextFormatTransformer = Readonly<{

export type TextMatchTransformer = Readonly<{
dependencies: Array<Klass<LexicalNode>>;
export: (
/**
* Determines how a node should be exported to markdown
*/
export?: (
node: LexicalNode,
// eslint-disable-next-line no-shadow
exportChildren: (node: ElementNode) => string,
// eslint-disable-next-line no-shadow
exportFormat: (node: TextNode, textContent: string) => string,
) => string | null;
importRegExp: RegExp;
/**
* This regex determines what text is matched during markdown imports
*/
importRegExp?: RegExp;
/**
* This regex determines what text is matched for markdown shortcuts while typing in the editor
*/
regExp: RegExp;
replace: (node: TextNode, match: RegExpMatchArray) => void;
trigger: string;
/**
* Determines how the matched markdown text should be transformed into a node during the markdown import process
*/
replace?: (node: TextNode, match: RegExpMatchArray) => void;
/**
* 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.
* If the trigger is matched, the `regExp` will be used to match the text in the second step.
*/
trigger?: string;
type: 'text-match';
}>;

Expand Down
6 changes: 5 additions & 1 deletion packages/lexical-markdown/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,17 @@ function codeBlockExport(node: LexicalNode) {

export function indexBy<T>(
list: Array<T>,
callback: (arg0: T) => string,
callback: (arg0: T) => string | undefined,
): Readonly<Record<string, Array<T>>> {
const index: Record<string, Array<T>> = {};

for (const item of list) {
const key = callback(item);

if (!key) {
continue;
}

if (index[key]) {
index[key].push(item);
} else {
Expand Down
Loading