Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/markdown-hard-break-from-markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@lexical/markdown': patch
---

[fix] Read a hard line break at the end of an imported line from its Markdown, so an escaped backslash and inline code ending in `\` or two spaces no longer gain a hard break, and `a\\\` keeps its hard break

Verify: import `a\\` followed by `b` and `` `a\` `` followed by `b`, which keep their backslash and stay soft line breaks, then export them and import the result again. `a\\\` followed by `b` keeps its hard break.
11 changes: 11 additions & 0 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import {$importTextTransformers} from './importTextTransformers';
import {
$createMarkdownLineBreakNode,
$setLineEndHardLineBreak,
type ElementTransformer,
type MultilineElementTransformer,
type TextFormatTransformer,
Expand Down Expand Up @@ -245,12 +246,22 @@ function $importBlocks(
}
}

// Taken after the element transformers ran, so it is the block the line's
// content went into: a list item, a heading, or a quote it was joined to.
const lineParent = textNode.getParent();

$importTextTransformers(
textNode,
textFormatTransformersIndex,
textMatchTransformers,
);

// Whether the line ends in a hard break is a question about its Markdown,
// which the imported text no longer answers once escapes and code are read.
if (lineParent !== null) {
$setLineEndHardLineBreak(lineParent, lineText);
}

// If no transformer found and we left with original paragraph node
// can check if its content can be appended to the previous node
// if it's a paragraph, quote or list
Expand Down
46 changes: 46 additions & 0 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,37 @@ function hasNonWhitespaceContentOnLine(
return false;
}

/**
* The hard line break, if any, at the end of each imported line, read from the
* line's Markdown and keyed by the last node the line produced. The text of
* the nodes can no longer tell: its escapes have been read, so `a\\` ends with
* a backslash just like `a\`, and inline code ends with its literal content.
*/
const lineEndHardLineBreaks = new WeakMap<
LexicalNode,
MarkdownHardLineBreak | null
>();

/**
* Record how the Markdown `line`, whose inline content has just been imported
* into `lineParent`, ends, for when the next line is appended to it.
*
* @internal
*/
export function $setLineEndHardLineBreak(
lineParent: ElementNode,
line: string,
): void {
const lastChild = lineParent.getLastChild();
if (lastChild !== null) {
const hardLineBreak = parseMarkdownHardLineBreak(line);
lineEndHardLineBreaks.set(
lastChild,
hardLineBreak !== null ? hardLineBreak[1] : null,
);
}
}

function $extractMarkdownHardLineBreakMarker(
previousNode: ElementNode,
): MarkdownHardLineBreak | null {
Expand All @@ -375,6 +406,21 @@ function $extractMarkdownHardLineBreakMarker(
}

const lastText = lastChild.getTextContent();

if (lineEndHardLineBreaks.has(lastChild)) {
const marker = lineEndHardLineBreaks.get(lastChild);
if (marker == null) {
return null;
}
// A backslash before the line ending escapes nothing, so it is still the
// last character of the text, unless a text match transformer took it.
if (marker === '\\' && lastText.endsWith('\\')) {
lastChild.setTextContent(lastText.slice(0, -1));
return marker;
}
// Trailing spaces stay in the text as written, so they are read below.
}

const hardLineBreak = parseMarkdownHardLineBreak(lastText);

if (hardLineBreak !== null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

// @vitest-environment node

import {buildEditorFromExtensions} from '@lexical/extension';
import {
$convertFromMarkdownString,
$convertToMarkdownString,
type Transformer,
TRANSFORMERS,
} from '@lexical/markdown';
import {
$getRoot,
$getState,
$isElementNode,
$isLineBreakNode,
$isTextNode,
type LexicalNode,
} from 'lexical';
import {describe, expect, it} from 'vitest';

import {
hardLineBreakState,
type TextMatchTransformer,
} from '../../MarkdownTransformers';
import {MarkdownTestExtension} from '../utils';

// Describes the inline content of the first block: text as it is, code as
// `code(...)`, and each line break as `<soft>` or `<hard \>` / `<hard >`.
function $describeNode(node: LexicalNode): string {
if ($isLineBreakNode(node)) {
const marker = $getState(node, hardLineBreakState);
return marker ? `<hard ${marker}>` : '<soft>';
}
if ($isTextNode(node)) {
const text = node.getTextContent();
return node.hasFormat('code') ? `code(${text})` : text;
}
if ($isElementNode(node)) {
return node.getChildren().map($describeNode).join('');
}
return node.getTextContent();
}

function importFirstBlock(
markdown: string,
shouldMergeAdjacentLines = false,
transformers: Transformer[] = TRANSFORMERS,
): string {
using editor = buildEditorFromExtensions([MarkdownTestExtension]);
let result = '';
editor.update(
() => {
$convertFromMarkdownString(
markdown,
transformers,
undefined,
false,
shouldMergeAdjacentLines,
);
const first = $getRoot().getFirstChild();
result = first ? $describeNode(first) : '';
},
{discrete: true},
);
return result;
}

function roundTrip(markdown: string): string {
using editor = buildEditorFromExtensions([MarkdownTestExtension]);
editor.update(
() => {
$convertFromMarkdownString(markdown, TRANSFORMERS);
},
{discrete: true},
);
return editor.read(() => $convertToMarkdownString(TRANSFORMERS));
}

// In single-quoted TS strings, '\\' is one backslash.
// https://spec.commonmark.org/0.31.2/#hard-line-breaks
describe('hard line breaks are read from the Markdown (#9408)', () => {
it('reads an escaped backslash at the end of a line as a literal backslash', () => {
expect(importFirstBlock('a\\\\\nb')).toBe('a\\<soft>b');
expect(importFirstBlock('_a\\\\_\nb')).toBe('a\\<soft>b');
expect(importFirstBlock('**a\\\\**\nb')).toBe('a\\<soft>b');
expect(importFirstBlock('> a\\\\\n> b')).toBe('a\\<soft>b');
// The continuation line keeps its indent, as it already did.
expect(importFirstBlock('- a\\\\\n b')).toBe('a\\<soft> b');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, non-blocking: each escaped-backslash case here is two lines, so the line whose end is recorded is never moved before its record is read. In a three-line paragraph, line 2's record has to survive the splice in $importBlocks() first; it does today, and one assertion would keep it that way:

Suggested change
expect(importFirstBlock('- a\\\\\n b')).toBe('a\\<soft> b');
expect(importFirstBlock('- a\\\\\n b')).toBe('a\\<soft> b');
// A joined line keeps its record until the next line reads it.
expect(importFirstBlock('a\nb\\\\\nc')).toBe('a<soft>b\\<soft>c');

});

// A character reference for a backslash is a literal backslash, not an
// escape or a hard break.
// https://spec.commonmark.org/0.31.2/#entity-and-numeric-character-references
it('reads a character reference for a backslash as a literal backslash', () => {
expect(importFirstBlock('a&#92;\nb')).toBe('a\\<soft>b');
});

it('reads an escaped backslash followed by a hard break', () => {
expect(importFirstBlock('a\\\\\\\nb')).toBe('a\\<hard \\>b');
});

it('keeps backslashes and spaces at the end of inline code', () => {
expect(importFirstBlock('`a\\`\nb')).toBe('code(a\\)<soft>b');
expect(importFirstBlock('`a `\nb')).toBe('code(a )<soft>b');
});

it('round trips these lines unchanged', () => {
for (const markdown of [
'a\\\\\nb',
'a\\\\\\\nb',
'`a\\`\nb',
'`a `\nb',
'**a\\\\**\nb',
'> a\\\\\n> b',
'- a\\\\\n b',
]) {
expect(roundTrip(markdown)).toBe(markdown);
}
// Italic is exported with `*`.
expect(roundTrip('_a\\\\_\nb')).toBe('*a\\\\*\nb');
});

it('still reads real hard breaks', () => {
expect(importFirstBlock('a\\\nb')).toBe('a<hard \\>b');
expect(importFirstBlock('a \nb')).toBe('a<hard >b');
expect(importFirstBlock('a\\\\\\\\\nb')).toBe('a\\\\<soft>b');
expect(importFirstBlock('**a**\\\nb')).toBe('a<hard \\>b');
expect(importFirstBlock('> a\\\n> b')).toBe('a<hard \\>b');
expect(importFirstBlock('a\\\nb\\\nc')).toBe('a<hard \\>b<hard \\>c');
expect(importFirstBlock('[a](https://lexical.dev)\\\nc')).toBe(
'a<hard \\>c',
);
for (const markdown of ['a\\\nb', 'a \nb', 'a\\\\\\\\\nb', '**a**\\\nb']) {
expect(roundTrip(markdown)).toBe(markdown);
}
});

it('keeps backslashes and spaces at the end of inline code in a quote or list', () => {
expect(importFirstBlock('> `a\\`\n> b')).toBe('code(a\\)<soft>b');
expect(importFirstBlock('- `a `\n b')).toBe('code(a )<soft> b');
expect(roundTrip('> `a\\`\n> b')).toBe('> `a\\`\n> b');
expect(roundTrip('- `a `\n b')).toBe('- `a `\n b');
});

it('reads the hard break the same way when adjacent lines are merged', () => {
expect(importFirstBlock('a\\\\\\\nb', true)).toBe('a\\<hard \\>b');
expect(importFirstBlock('a\\\\\nb', true)).toBe('a\\ b');
});

it('keeps the text of a node that a text match transformer took the backslash into', () => {
// The trailing `\` is part of the match, so the text of the line's last
// node no longer ends with it. Stripping a character anyway would turn
// `#foo` into `#fo`, so the text-based check decides, as it did before.
const HASHTAG_BACKSLASH: TextMatchTransformer = {
dependencies: [],
importRegExp: /#(\w+)\\$/,
regExp: /#(\w+)\\$/,
replace: (textNode, match) => {
textNode.setTextContent('#' + match[1]);
},
type: 'text-match',
};
expect(
importFirstBlock('x #foo\\\nb', false, [
HASHTAG_BACKSLASH,
...TRANSFORMERS,
]),
).toBe('x #foo<soft>b');
});

it('keeps a backslash at the end of a block as text', () => {
expect(importFirstBlock('a\\')).toBe('a\\');
expect(importFirstBlock('# h\\')).toBe('h\\');
});
});