Skip to content

[lexical-markdown] Bug fix: Prevent transform from removing nodes if the replace function returns false #7564

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

Merged
merged 3 commits into from
May 25, 2025
Merged
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: 2 additions & 2 deletions packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ function runElementTransformers(
) {
const nextSiblings = anchorNode.getNextSiblings();
const [leadingNode, remainderNode] = anchorNode.splitText(anchorOffset);
leadingNode.remove();
const siblings = remainderNode
? [remainderNode, ...nextSiblings]
: nextSiblings;
if (replace(parentNode, siblings, match, false) !== false) {
leadingNode.remove();
return true;
}
}
Expand Down Expand Up @@ -127,12 +127,12 @@ function runMultilineElementTransformers(
) {
const nextSiblings = anchorNode.getNextSiblings();
const [leadingNode, remainderNode] = anchorNode.splitText(anchorOffset);
leadingNode.remove();
const siblings = remainderNode
? [remainderNode, ...nextSiblings]
: nextSiblings;

if (replace(parentNode, siblings, match, null, null, false) !== false) {
leadingNode.remove();
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html';
import {$createLinkNode, LinkNode} from '@lexical/link';
import {ListItemNode, ListNode} from '@lexical/list';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {$createTextNode, $getRoot, $insertNodes} from 'lexical';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$insertNodes,
$isRangeSelection,
} from 'lexical';

import {
$convertFromMarkdownString,
$convertToMarkdownString,
LINK,
registerMarkdownShortcuts,
TextMatchTransformer,
Transformer,
TRANSFORMERS,
} from '../..';
import {
CODE,
ElementTransformer,
HEADING,
MultilineElementTransformer,
normalizeMarkdown,
} from '../../MarkdownTransformers';
Expand Down Expand Up @@ -213,6 +223,18 @@ const CODE_TAG_COUNTER_EXAMPLE: MultilineElementTransformer = {
type: 'multiline-element',
};

export const CANCELED_HEADING_REPLACE_EXAMPLE: ElementTransformer = {
dependencies: [HeadingNode],
export: () => {
return null;
},
regExp: /^(#{1,6})\s/,
replace: () => {
return false;
},
type: 'element',
};

describe('Markdown', () => {
type Input = Array<{
html: string;
Expand Down Expand Up @@ -734,6 +756,99 @@ describe('Markdown', () => {
).toBe(mdAfterExport ?? md);
});
}
it('should not remove leading node and transform if replace returns false', () => {
const editor = createHeadlessEditor({
nodes: [
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
LinkNode,
],
});

registerMarkdownShortcuts(editor, [CANCELED_HEADING_REPLACE_EXAMPLE]);

editor.update(
() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
paragraph.selectEnd();
const selection = $getSelection();
if ($isRangeSelection(selection)) {
selection.insertText('#');
}
},
{
discrete: true,
},
);

editor.update(
() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
selection.insertText(' ');
}
},
{
discrete: true,
},
);

expect(editor.read(() => $generateHtmlFromNodes(editor))).toBe(
'<p><span style="white-space: pre-wrap;"># </span></p>',
);
});

it('should remove leading node and execute transform if replace does not return false', () => {
const editor = createHeadlessEditor({
nodes: [
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
LinkNode,
],
});

registerMarkdownShortcuts(editor, [HEADING]);

editor.update(
() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
paragraph.selectEnd();
const selection = $getSelection();
if ($isRangeSelection(selection)) {
selection.insertText('#');
}
},
{
discrete: true,
},
);

editor.update(
() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
selection.insertText(' ');
}
},
{
discrete: true,
},
);

expect(editor.read(() => $generateHtmlFromNodes(editor))).toBe(
'<h1><br></h1>',
);
});
});

describe('normalizeMarkdown - shouldMergeAdjacentLines = true', () => {
Expand Down