Skip to content

[lexical-markdown] Bug Fix: text left outside a markdown link keeps its format - #9131

Merged
potatowagon merged 1 commit into
facebook:mainfrom
Om-singhaI:fix/markdown-link-leftover-text-format
Sep 8, 2026
Merged

potatowagon merged 1 commit into
facebook:mainfrom
Om-singhaI:fix/markdown-link-leftover-text-format

Conversation

@Om-singhaI

Copy link
Copy Markdown
Contributor

Description

Importing *[h[ello](https://lexical.dev)* loses the italic on [h.

importRegExp matches here with a linkText of h[ello, which has more opening than closing brackets, so the LINK transformer splits it: ello goes inside the link and [h goes back as a sibling TextNode ahead of it. The link's own text node is given the format of the TextNode being replaced, but the sibling is a bare $createTextNode, so the format on that half is dropped. One italic run comes back as a plain [h next to an italic ello.

Both new nodes stand in for the same TextNode, so it's inconsistent for only one of them to keep its format. The rest of the import path already preserves it: importTextMatchTransformer hands replace a node that came out of splitText, and splitText copies format, style and detail onto every piece it makes. $createAutoLinkNode_ in @lexical/link gets it right for the same reason. The markdown LINK transformer is the one place that builds its leftover node by hand.

The fix reads the format once before the replace detaches the node, then applies it to both nodes.

I kept this to the format alone. The link's text node doesn't copy style or detail today either, so putting those on just the sibling would swap one mismatch for another.

Test plan

Added the italic case to the IMPORT_AND_EXPORT table right beside the existing [h[ello](...) split case, so it runs through import, export and the selection check. It needs an mdAfterExport because the exporter writes the italic as two runs, *[h*[*ello*](https://lexical.dev).

node_modules/.bin/vitest --project unit --no-watch run packages/lexical-markdown

Before

New test in place, MarkdownTransformers.ts still at main:

 × can import "*[h[ello](https://lexical.dev)*"

AssertionError: expected '<p><span style="white-space: pre-wrap…' to be '<p><i><em style="white-space: pre-wra…'

Expected: "<p><i><em style="white-space: pre-wrap;">[h</em></i><a href="https://lexical.dev"><i><em style="white-space: pre-wrap;">ello</em></i></a></p>"
Received: "<p><span style="white-space: pre-wrap;">[h</span><a href="https://lexical.dev"><i><em style="white-space: pre-wrap;">ello</em></i></a></p>"

 Test Files  1 failed | 6 passed (7)
      Tests  1 failed | 633 passed (634)

After

 Test Files  7 passed (7)
      Tests  634 passed (634)

packages/lexical-link stays green too, 10 files and 187 tests.

…ts format

When the LINK transformer sees more opening than closing brackets it splits the
match, keeps part of the text inside the link and puts the rest back as a
sibling TextNode before it. The link's own text node gets the replaced node's
format, but the sibling was a bare $createTextNode, so any bold, italic or
strikethrough on that leftover text was dropped.

Importing "*[h[ello](https://lexical.dev)*" produced a plain "[h" next to an
italic "ello". Both nodes stand in for the same TextNode, so both now carry its
format.
@vercel

vercel Bot commented Sep 8, 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 8, 2026 12:47am UTC
lexical-playground Ready Ready Preview Sep 8, 2026 12:47am 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 Sep 8, 2026
@potatowagon

Copy link
Copy Markdown
Contributor

manually tested, lgtm

before:
Screenshot 2026-09-08 at 6 24 47 PM

after:

Screenshot 2026-09-08 at 6 24 52 PM

correct one:

Screenshot 2026-09-08 at 6 24 59 PM

@potatowagon
potatowagon added this pull request to the merge queue Sep 8, 2026
Merged via the queue into facebook:main with commit 07767b6 Sep 8, 2026
46 checks passed
@etrepum etrepum mentioned this pull request Sep 17, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical 50d49741 Deployed Sep 8, 2026 by vercel[bot]
Preview – lexical-playground 50d49741 Deployed Sep 8, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants