Skip to content

Commit

Permalink
Move Transformer Groups from index to MarkdownTransformers
Browse files Browse the repository at this point in the history
Move transformer groups created in index.ts to MarkdownTransformers.ts, co-locating them with the member transformers and avoiding a cyclical dependency involving index.ts.
  • Loading branch information
jkjk822 committed Jul 29, 2024
1 parent f92696d commit 16efbb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from 'lexical';
import invariant from 'shared/invariant';

import {TRANSFORMERS} from '.';
import {TRANSFORMERS} from './MarkdownTransformers';
import {indexBy, PUNCTUATION_OR_SPACE, transformersByType} from './utils';

function runElementTransformers(
Expand Down
32 changes: 32 additions & 0 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ export const ITALIC_UNDERSCORE: TextFormatTransformer = {
type: 'text-format',
};

export const ELEMENT_TRANSFORMERS: Array<ElementTransformer> = [
HEADING,
QUOTE,
CODE,
UNORDERED_LIST,
ORDERED_LIST,
];

// Order of text transformers matters:
//
// - code should go first as it prevents any transformations inside
Expand Down Expand Up @@ -388,3 +396,27 @@ export const LINK: TextMatchTransformer = {
trigger: ')',
type: 'text-match',
};

// Order of text format transformers matters:
//
// - code should go first as it prevents any transformations inside
// - then longer tags match (e.g. ** or __ should go before * or _)
export const TEXT_FORMAT_TRANSFORMERS: Array<TextFormatTransformer> = [
INLINE_CODE,
BOLD_ITALIC_STAR,
BOLD_ITALIC_UNDERSCORE,
BOLD_STAR,
BOLD_UNDERSCORE,
HIGHLIGHT,
ITALIC_STAR,
ITALIC_UNDERSCORE,
STRIKETHROUGH,
];

export const TEXT_MATCH_TRANSFORMERS: Array<TextMatchTransformer> = [LINK];

export const TRANSFORMERS: Array<Transformer> = [
...ELEMENT_TRANSFORMERS,
...TEXT_FORMAT_TRANSFORMERS,
...TEXT_MATCH_TRANSFORMERS,
];
36 changes: 4 additions & 32 deletions packages/lexical-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CODE,
HEADING,
HIGHLIGHT,
ELEMENT_TRANSFORMERS
INLINE_CODE,
ITALIC_STAR,
ITALIC_UNDERSCORE,
Expand All @@ -34,40 +35,11 @@ import {
QUOTE,
STRIKETHROUGH,
UNORDERED_LIST,
TEXT_FORMAT_TRANSFORMERS,
TEXT_MATCH_TRANSFORMERS,
TRANSFORMERS,
} from './MarkdownTransformers';

const ELEMENT_TRANSFORMERS: Array<ElementTransformer> = [
HEADING,
QUOTE,
CODE,
UNORDERED_LIST,
ORDERED_LIST,
];

// Order of text format transformers matters:
//
// - code should go first as it prevents any transformations inside
// - then longer tags match (e.g. ** or __ should go before * or _)
const TEXT_FORMAT_TRANSFORMERS: Array<TextFormatTransformer> = [
INLINE_CODE,
BOLD_ITALIC_STAR,
BOLD_ITALIC_UNDERSCORE,
BOLD_STAR,
BOLD_UNDERSCORE,
HIGHLIGHT,
ITALIC_STAR,
ITALIC_UNDERSCORE,
STRIKETHROUGH,
];

const TEXT_MATCH_TRANSFORMERS: Array<TextMatchTransformer> = [LINK];

const TRANSFORMERS: Array<Transformer> = [
...ELEMENT_TRANSFORMERS,
...TEXT_FORMAT_TRANSFORMERS,
...TEXT_MATCH_TRANSFORMERS,
];

/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*/
Expand Down

0 comments on commit 16efbb9

Please sign in to comment.