Skip to content

Commit

Permalink
[lexical-markdown]: Refactor: allows omitting certain properties from…
Browse files Browse the repository at this point in the history
… TextMatchTransformers, adds jsdocs (#6651)

Co-authored-by: Bob Ippolito <[email protected]>
  • Loading branch information
AlessioGr and etrepum authored Sep 20, 2024
1 parent cf43676 commit 1469aa7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
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

0 comments on commit 1469aa7

Please sign in to comment.