Skip to content

[lexical-markdown] Bug Fix: Fix nested mixed-type list structure during markdown import - #8283

Merged
etrepum merged 42 commits into
meta:mainfrom
achaljhawar:fix/nested-mixed-type-list-markdown-import
Sep 2, 2026
Merged

etrepum merged 42 commits into
meta:mainfrom
achaljhawar:fix/nested-mixed-type-list-markdown-import

Conversation

@achaljhawar

Copy link
Copy Markdown
Contributor

Description

Importing markdown with nested lists of different types (e.g. bullet containing ordered) flattened them into separate top-level lists. listReplace only merged items into an adjacent list when types matched, so indented items of a different type couldn't merge. Fixed by allowing the merge when indented regardless of type, then correcting the nested list type after setIndent().

Closes #7938

Before

Lexical.Playground.12.mp4

After

Lexical.Playground.13.mp4

@vercel

vercel Bot commented Apr 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 2, 2026 5:51pm UTC
lexical-playground Ready Ready Preview Sep 2, 2026 5:51pm UTC

Request Review

@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 Apr 5, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 5, 2026
@etrepum
etrepum marked this pull request as draft April 22, 2026 04:20
claude and others added 2 commits September 1, 2026 15:17
…line

## Description

The levels a list leaves open were closed by any block that was not a list,
and a blank line is a block: it arrives as an empty paragraph and only gets
dropped once the import is over. So a blank line decided which of two indent
rules the line after it was read by. `1. First\n   - detail` took the sublist
and `1. First\n\n   - detail` did not, though a blank line before a sublist
only makes the list loose.

Reading one form by columns and the other by the fixed step also left an
indent that does not survive its own export: `- a\n\n        - b` was read as
two levels by `getIndent`, exported as `- a\n        - b` with the blank line
dropped, and read back as one level by the columns.

A blank line no longer closes the levels. They stay open while the nearest
block that is not one is still the list itself, so a paragraph or a code fence
closes them whether or not blank lines surround it.

## Test plan

`pnpm exec vitest run packages/lexical-markdown packages/lexical-list`

### Before

```
 FAIL  Markdown > can import "- a\n\n  - b"
 FAIL  Markdown > can import "1. First\n\n   - detail"
 FAIL  Loose list indents round trip > re-imports its own export of "- a\n\n        - b" unchanged

 Test Files  1 failed | 10 passed (11)
      Tests  3 failed | 618 passed (621)
```

The round trip reads:

```
- Expected  '- a\n        - b'
+ Received  '- a\n    - b'
```

### After

```
 Test Files  11 passed (11)
      Tests  621 passed (621)
```

Tight and loose now agree, and each is its own fixed point:

```
'- a\n  - b'            -> '- a\n    - b'
'- a\n\n  - b'          -> '- a\n    - b'
'- a\n        - b'      -> '- a\n    - b'
'- a\n\n        - b'    -> '- a\n    - b'
'1. First\n   - detail'   -> '1. First\n    - detail'
'1. First\n\n   - detail' -> '1. First\n\n    - detail'
```

while a block of another kind still closes them, blank lines or not:

```
'- a\n\ntext\n\n  - b'                  -> '- a\n\ntext\n\n- b'
'- a\n\n```\ncode\n```\n\n  - b'        -> '- a\n\n```\ncode\n```\n\n- b'
```

`pnpm run test-unit`

```
 Test Files  227 passed (227)
      Tests  5003 passed | 1 skipped (5004)
```

`pnpm run ci-check`

```
No errors!
```

Playwright, against the playground dev server:

```
pnpm exec playwright test --project=chromium \
  packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs \
  packages/lexical-playground/__tests__/e2e/List.spec.mjs \
  packages/lexical-playground/__tests__/e2e/CheckList.spec.mjs
  89 passed (6.7m)
```
…1n2lz' into fix/nested-mixed-type-list-markdown-import
claude and others added 5 commits September 1, 2026 16:04
…asures against

## Description

Review follow-ups, all reproduced before fixing.

The blank-line fix taught the indent measurement to look across blank lines,
but not the placement: the line was measured against the list above the blanks
and then placed relative to its immediate sibling, the blank paragraph. A
loose same-type sublist only looked right because the `ListNode` transform
later merged the two lists it left behind — which `$generateNodesFromMarkdown-
String` never runs, so it returned a split list — and a loose type-changing
sublist (`- a\n\n  1. b`) had no such rescue: it became a detached list nested
inside itself. The line is now placed into the same block it is measured
against, so both land inside the item above them, joined. The two placement
branches this touches had become identical and are merged. An import that
preserves new lines keeps blank paragraphs as content, so there a blank line
still closes the list like any block.

The typed number of an indented ordered item was written onto the list it was
inserted into before `setIndent` moved it out: typing `    1. z` above an
existing `1. y` list renumbered that list to start at 1 while `z` went into a
sublist. The start is only taken when the item stays where it landed.

`[X]` imported as unchecked: `CHECK_LIST_REGEX` matches case-insensitively,
but the checked test compared against lowercase `x` only.

A tab after a list marker was measured as one column, though it advances to
the next tab stop: `-\ta` opens its content at column four, so a two-space
`- b` under it is a sibling, not a sublist. The content column is now read
from where the matched marker ends with tabs expanded, which also removes the
special case for numbered markers.

## Test plan

`pnpm exec vitest run packages/lexical-markdown packages/lexical-list`

### Before

```
 FAIL  Markdown > can import "1. First\n\n   - detail"
 FAIL  List marker details > reads an uppercase [X] as checked, as the /i on its regex intends
 FAIL  List marker details > expands a tab after the marker when measuring the content column
 FAIL  List marker details > keeps a typed start number out of a list the item only passes through
 FAIL  Loose sublists > nests a loose type-changing sublist into the item above it
 FAIL  Loose sublists > returns one joined list from $generateNodesFromMarkdownString

 Test Files  1 failed | 10 passed (11)
      Tests  6 failed | 620 passed (626)
```

The loose type-changing case read, before:

```
'- a\n\n  1. b' -> <ul><li>a</li></ul><ol><li><ol><li>b</li></ol></li></ol>
```

and now:

```
'- a\n\n  1. b' -> <ul><li>a<ol><li>b</li></ol></li></ul>
```

### After

```
 Test Files  11 passed (11)
      Tests  626 passed (626)
```

`pnpm run test-unit`

```
 Test Files  227 passed (227)
      Tests  5008 passed | 1 skipped (5009)
```

`pnpm run ci-check`

```
No errors!
```

Playwright, against the playground dev server:

```
pnpm exec playwright test --project=chromium \
  packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs \
  packages/lexical-playground/__tests__/e2e/List.spec.mjs \
  packages/lexical-playground/__tests__/e2e/CheckList.spec.mjs
  89 passed (6.6m)
```
## Description

An error thrown inside `editor.update` or `editor.read` is routed to the
editor's `onError` handler rather than rethrown, and `createHeadlessEditor`
defaults that handler to `console.error`. So a failed assertion inside an
update logged and passed — which is how a regression test on this branch
managed to pass against the code it was written to catch, until it was
restructured to assert outside the update.

Every editor in this file now goes through one `createTestEditor` helper that
throws from `onError`, the way `buildEditorFromExtensions` does by default,
and defaults to the node set the file uses throughout — which also replaces
the five identical per-describe copies of that helper. All 495 tests still
pass with errors propagating, so none of the existing in-update assertions
were hiding a failure.

## Test plan

`pnpm exec vitest run packages/lexical-markdown packages/lexical-list`

```
 Test Files  11 passed (11)
      Tests  626 passed (626)
```

`pnpm run test-unit`

```
 Test Files  227 passed (227)
      Tests  5008 passed | 1 skipped (5009)
```

`tsc --noEmit`, `eslint`, `prettier --check` on the file: clean. No source
files changed.
…1n2lz' into fix/nested-mixed-type-list-markdown-import
etrepum
etrepum previously approved these changes Sep 2, 2026
etrepum and others added 7 commits September 2, 2026 09:55
…rkdown import

## Description

"link destination whitespace does not backtrack > reads a run of tabs" has
been failing with a stack overflow the whole time it has existed — the crash
was routed to the editor's default `onError` and logged, so the test passed
vacuously until the error handlers in this file were made to throw.

Two overflows, one pattern: an argument or element per node in a document
whose size nothing bounds.

`$normalizeMarkdownTextNode` spread one offset per tab boundary into
`splitText(...)`, so a pasted line with two hundred thousand tabs overflowed
the call stack before a single node was made. It now builds the replacement
text and tab nodes directly — which also skips the detour of splitting into
single-character nodes only to replace half of them — and returns early for a
`TabNode`, which is already a tab and would only be destroyed and recreated.

`ElementNode.getAllTextNodes` then collected the two hundred thousand nodes
the import now produces with `push(...children)`, which is the same overflow
one call later. It appends in a loop.

Every remaining `splitText` caller in the repository passes one or two fixed
offsets, so no array-taking variant of it is needed for this.

## Test plan

The previously-vacuous test is the regression test; it now runs the code it
was written to time and passes within its 5s limit.

`pnpm run test-unit`

```
 Test Files  323 passed (323)
      Tests  6029 passed | 1 skipped (6030)
```

`pnpm run ci-check`

```
No errors!
```
…1n2lz' into fix/nested-mixed-type-list-markdown-import
…ration

## Description

`$normalizeMarkdownTextNode` walked its replacement nodes in with one
`insertAfter` per node and a trailing `remove()`. `splice` is the primitive
those are built on, so the walker and its moving cursor are replaced with one
call that swaps the text node for the whole replacement list — and it takes
the list as an array, so nothing scales with the node count but the list
itself.

## Test plan

`pnpm exec vitest run packages/lexical-markdown`

```
 Test Files  7 passed (7)
      Tests  631 passed (631)
```

including "reads a run of tabs" (200,000 tabs) within its 5s limit.

`pnpm run test-unit`

```
 Test Files  323 passed (323)
      Tests  6029 passed | 1 skipped (6030)
```

This branch was successfully deployed

2 active deployments
Preview – lexical — 30e2bd47 Deployed Sep 2, 2026 by vercel[bot]
Preview – lexical-playground — 30e2bd47 Deployed Sep 2, 2026 by vercel[bot]
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Importing from Markdown breaks nested list structure in the editor

4 participants