lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: New lines in tables are not properly transformed to markdown

Open MoritzWeber0 opened this issue 2 months ago • 4 comments

A table with new lines in a table cell results in \n\n in the generated markdown. This is not valid markdown. Instead, it should use <br> for the line breaks.

Lexical version: v0.52.0+dev.esm

Steps To Reproduce

  1. Create a table in the Lexical playground: Image

  2. Toggle markdown mode: Image

  3. Copy the resulting markdown to a markdown renderer, for example https://markdownlivepreview.com/ It will not render the new lines: Image

Link to code example: https://playground.lexical.dev/#doc=H4sIAAAAAAAAE-VVUWsCMQz-K5LnIncHm9jXwZ4H97AH8SFeM68stkcu6kT876PnnLoxcG4MZU9tkiZf8rUfXQM5r1FKRSWwa5AYNa1V7dkJBbCj8wxHip7BZgaeosxQu-0sOgILIXkYDLS64uQAA0ovChZ6Sq32kr1qUqRzG1iQtD4GsPlmbMB5oUo7O8yZ9xCpkA-OwhYuJd_v0ZNZHiBuERoUnAo29SeYCVbPU4nz4O4iR9mBVZHLBtMhAzWhox19uQGJy_fYyV2-TYoTpoqYj_swX7P8z5j4zrgHhSQuT2b0SujN_vihnS_npOYLEPPH2a58nEt5Mr-lyHHX-KN3WrdgR8PCDIsza_-0we4DPKqxMcDYaokLcmDzwTDPs0FRDIrbzEAb51KlvAfG1fY-DrMh698U_Qw2r6UQ_xRpBwAA

The current behavior

Currently, \n\n is used in the markdown to indicate new lines.

The expected behavior

It should use <br> for line breaks in the renderer.

Impact of fix

The bug breaks all tables rendered with markdown renderers.

MoritzWeber0 avatar Oct 04 '26 09:10 MoritzWeber0

The @lexical/mdast implementation probably handles this more correctly.

etrepum avatar Oct 04 '26 13:10 etrepum

I checked @lexical/mdast, but new lines are omitted completely (just a blank space) with the @lexical/mdast package as it seems.

MoritzWeber0 avatar Oct 04 '26 16:10 MoritzWeber0

Looks like this is already in progress, so just a few things I ran into that might be useful.

The one that took me longest to pin down: a hard line break carries a marker. hardLineBreakState holds a backslash or two spaces and $exportLineBreak puts it in front of the newline, so encoding that newline gives you a\<br>b, and CommonMark reads \< as an escaped <. marked and markdown-it both show the tag as text. Returning <br> from a text match transformer for the LineBreakNode sidesteps it, since $exportChildren runs those before it reaches $isLineBreakNode. And it's reachable by just pasting, because resetOnCopyNode only applies to $copyNode while the clipboard goes through exportJSON/importJSON and keeps the state.

Reading the old literal \n back also bites a cell holding C:\new: the exporter escapes that backslash to \\, so the decode makes it C:\ + newline + ew. Invisible right now because re-encoding puts \\n back.

Two unrelated things in the same transformer while I was in there. A single-column header table exports no | --- |, because the header check is === ROW but the factory sets ROW | COLUMN. And | --- | on a line by itself gets eaten and leaves an empty paragraph.

mayrang avatar Oct 04 '26 17:10 mayrang

Still reproduces on playground.lexical.dev (v0.52.0+dev.esm, 2026-10-07, Chromium 151, 1280×720):

  1. Open the playground with a table containing a two-paragraph cell (the issue permalink works directly).
  2. Click the Convert to Markdown button in the actions toolbar (bottom right, [M↓]).
  3. The table serializes as:
| test | |
| --- | --- |
| | test\n\ntest |

The hard line break inside the cell produces literal \n\n — two consecutive backslash-n characters — instead of a <br> tag. This breaks standard Markdown table syntax: a renderer treats the row as continuing with a blank line, which exits the table entirely.

Single-line cells in the same table serialize correctly.

Recording with timestamps: https://scenerecap.com/s/pipbsjjmh0 (markdown view at ~23 s)

scenerecap avatar Oct 17 '26 11:10 scenerecap