Skip to content

[lexical-markdown] Bug Fix: an escaped backslash is not a hard line break - #9018

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-escaped-backslash-break
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:md-escaped-backslash-break

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

parseMarkdownHardLineBreak treats any line ending in a backslash as a hard
line break:

if (line.endsWith('\\')) {
  return [line.slice(0, -1), '\\'];
}

CommonMark only gives a trailing backslash that meaning when the backslash is
not itself escaped. foo\\ is a backslash escape — a literal \ followed by
an ordinary (soft) line ending — so only an odd-length run of trailing
backslashes is a hard line break.
https://spec.commonmark.org/0.31.2/#backslash-escapes

Because the parser cannot tell foo\ from foo\\, the final backslash is
consumed as a line-break marker and removed from the text. With
shouldMergeAdjacentLines, normalizeMarkdown also refuses to merge the two
lines, since it asks the same function whether the previous line ended in a
hard line break:

input:    foo\\
          bar

expected: one paragraph, text `foo\ bar`
actual:   two lines, text `foo` + hard break + `bar`  (the `\` is gone)

This counts the trailing backslash run and only reports a hard line break for
an odd length. The lone-backslash and two-space forms are unchanged.

Note this fixes the merging path, where normalizeMarkdown joins the lines
before any line-break node is built. Without shouldMergeAdjacentLines the
marker is extracted from a TextNode whose escapes have already been resolved
by importTextTransformers, so foo\\ and foo\ are genuinely
indistinguishable at that point; disambiguating those would mean moving
hard-line-break detection ahead of unescaping, which is a larger change and is
left alone here.

Test plan

New unit test packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts,
covering the parser directly, normalizeMarkdown, and the imported text. The
lone-backslash and two-space cases are included as controls — they pass before
and after.

Before

$ npx vitest run packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts

     × does not treat an escaped backslash as a hard line break 3ms
     × merges a line ending in an escaped backslash with the next line 2ms
     × keeps the literal backslash in the imported text 5ms

AssertionError: expected [ 'foo\', '\' ] to be null // Object.is equality
AssertionError: expected 'foo\\\nbar' to be 'foo\\ bar' // Object.is equality
AssertionError: expected 'foo\nbar' to be 'foo\ bar' // Object.is equality

      Tests  3 failed | 4 passed (7)

After

$ npx vitest run packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts

      Tests  7 passed (7)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core

 Test Files  8 passed (8)
      Tests  474 passed (474)

…reak

## Description

`parseMarkdownHardLineBreak` treats any line ending in a backslash as a hard
line break:

```js
if (line.endsWith('\\')) {
  return [line.slice(0, -1), '\\'];
}
```

CommonMark only gives a trailing backslash that meaning when the backslash is
not itself escaped. `foo\\` is a backslash escape — a literal `\` followed by
an ordinary (soft) line ending — so only an **odd**-length run of trailing
backslashes is a hard line break.
https://spec.commonmark.org/0.31.2/#backslash-escapes

Because the parser cannot tell `foo\` from `foo\\`, the final backslash is
consumed as a line-break marker and removed from the text. With
`shouldMergeAdjacentLines`, `normalizeMarkdown` also refuses to merge the two
lines, since it asks the same function whether the previous line ended in a
hard line break:

```
input:    foo\\
          bar

expected: one paragraph, text `foo\ bar`
actual:   two lines, text `foo` + hard break + `bar`  (the `\` is gone)
```

This counts the trailing backslash run and only reports a hard line break for
an odd length. The lone-backslash and two-space forms are unchanged.

Note this fixes the merging path, where `normalizeMarkdown` joins the lines
before any line-break node is built. Without `shouldMergeAdjacentLines` the
marker is extracted from a TextNode whose escapes have already been resolved
by `importTextTransformers`, so `foo\\` and `foo\` are genuinely
indistinguishable at that point; disambiguating those would mean moving
hard-line-break detection ahead of unescaping, which is a larger change and is
left alone here.

## Test plan

New unit test `packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts`,
covering the parser directly, `normalizeMarkdown`, and the imported text. The
lone-backslash and two-space cases are included as controls — they pass before
and after.

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts

     × does not treat an escaped backslash as a hard line break 3ms
     × merges a line ending in an escaped backslash with the next line 2ms
     × keeps the literal backslash in the imported text 5ms

AssertionError: expected [ 'foo\', '\' ] to be null // Object.is equality
AssertionError: expected 'foo\\\nbar' to be 'foo\\ bar' // Object.is equality
AssertionError: expected 'foo\nbar' to be 'foo\ bar' // Object.is equality

      Tests  3 failed | 4 passed (7)
```

### After

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts

      Tests  7 passed (7)

$ npx vitest run packages/lexical-markdown packages/lexical-code-core

 Test Files  8 passed (8)
      Tests  474 passed (474)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9055 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant