Skip to content

Commit

Permalink
perf: shared canContainTransformableMarkdown function, avoid editor.u…
Browse files Browse the repository at this point in the history
…pdate call if node can not contain transformable markdown
  • Loading branch information
AlessioGr committed Jan 21, 2025
1 parent 4fd26bb commit 7987e04
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
8 changes: 2 additions & 6 deletions packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import invariant from 'shared/invariant';

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

function runElementTransformers(
Expand Down Expand Up @@ -497,19 +498,14 @@ export function registerMarkdownShortcuts(
const anchorNode = editorState._nodeMap.get(anchorKey);

if (
!$isTextNode(anchorNode) ||
!canContainTransformableMarkdown(anchorNode) ||
!dirtyLeaves.has(anchorKey) ||
(anchorOffset !== 1 && anchorOffset > prevSelection.anchor.offset + 1)
) {
return;
}

editor.update(() => {
// Markdown is not available inside code
if (anchorNode.hasFormat('code')) {
return;
}

const parentNode = anchorNode.getParent();

if (parentNode === null || $isCodeNode(parentNode)) {
Expand Down
50 changes: 19 additions & 31 deletions packages/lexical-markdown/src/importTextTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type {TextFormatTransformersIndex} from './MarkdownImport';
import type {TextMatchTransformer} from './MarkdownTransformers';

import {$isTextNode, type TextNode} from 'lexical';
import {$isTextNode, type LexicalNode, type TextNode} from 'lexical';

import {
findOutermostTextFormatTransformer,
Expand All @@ -19,6 +19,18 @@ import {
importFoundTextMatchTransformer,
} from './importTextMatchTransformer';

/**
* Returns true if the node can contain transformable markdown.
* Code nodes cannot contain transformable markdown.
* For example, `code **bold**` should not be transformed to
* <code>code <strong>bold</strong></code>.
*/
export function canContainTransformableMarkdown(
node: LexicalNode | undefined,
): node is TextNode {
return $isTextNode(node) && !node.hasFormat('code');
}

/**
* Handles applying both text format and text match transformers.
* It finds the outermost text format or text match and applies it,
Expand Down Expand Up @@ -63,33 +75,21 @@ export function importTextTransformers(
foundTextFormat.match,
);

if (
result.nodeAfter &&
$isTextNode(result.nodeAfter) &&
!result.nodeAfter.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.nodeAfter)) {
importTextTransformers(
result.nodeAfter,
textFormatTransformersIndex,
textMatchTransformers,
);
}
if (
result.nodeBefore &&
$isTextNode(result.nodeBefore) &&
!result.nodeBefore.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.nodeBefore)) {
importTextTransformers(
result.nodeBefore,
textFormatTransformersIndex,
textMatchTransformers,
);
}
if (
result.transformedNode &&
$isTextNode(result.transformedNode) &&
!result.transformedNode.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.transformedNode)) {
importTextTransformers(
result.transformedNode,
textFormatTransformersIndex,
Expand All @@ -109,33 +109,21 @@ export function importTextTransformers(
return;
}

if (
result.nodeAfter &&
$isTextNode(result.nodeAfter) &&
!result.nodeAfter.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.nodeAfter)) {
importTextTransformers(
result.nodeAfter,
textFormatTransformersIndex,
textMatchTransformers,
);
}
if (
result.nodeBefore &&
$isTextNode(result.nodeBefore) &&
!result.nodeBefore.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.nodeBefore)) {
importTextTransformers(
result.nodeBefore,
textFormatTransformersIndex,
textMatchTransformers,
);
}
if (
result.transformedNode &&
$isTextNode(result.transformedNode) &&
!result.transformedNode.hasFormat('code')
) {
if (canContainTransformableMarkdown(result.transformedNode)) {
importTextTransformers(
result.transformedNode,
textFormatTransformersIndex,
Expand Down

0 comments on commit 7987e04

Please sign in to comment.