richtext-lexical: CodeBlock markdown import prepends "undefined" and truncates info strings

Open
#19,613 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@anatolykoptev is already working on this.

Since Nov 16, 2026.

  • #19614 by @anatolykoptev — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
38/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, typescript
Domain
backend, content

Research direction

Read packages/richtext-lexical/src/features/blocks/premade/CodeBlock/converter.ts and the reproducing spec at packages/richtext-lexical/src/features/blocks/premade/CodeBlock/converter.spec.ts. Run the unit command from the issue to see the failing import and export cases. Done means all 20 cases pass, including empty language and info strings such as c++; an open linked pull request already addresses this issue.

Written by the indexing model from the issue text.

Description

Bug Payload OSS plugin: richtext-lexical
Describe the Bug

The premade CodeBlock markdown converter (packages/richtext-lexical/src/features/blocks/premade/CodeBlock/converter.ts) corrupts code in two ways, through both convertMarkdownToLexical and convertLexicalToMarkdown.

  1. A one-line fence whose code starts with a non-word character imports with undefined in front of the code. ```$ ls``` gives code undefined$ ls. Blocks with an empty language export to this one-line form, so they are corrupted on a round trip as well: { language: '', code: '$ npm i -g x' } comes back as undefined$ npm i -g x, and an empty block comes back as undefined.
  2. An info string with characters outside \w is cut at the first such character, and the rest ends up in the code. ```c++ gives language c and code ++\n…. The same happens for c#, objective-c and shell-session.

The file is identical on main and 3.x.

Link to the code that reproduces this issue

https://github.com/anatolykoptev/payload/commit/7e8d837e04 (a unit spec only, with no fix, on top of main 56033d9561)

Reproduction Steps
  1. Check out the linked commit and run pnpm install.
  2. Run pnpm vitest run --project unit packages/richtext-lexical/src/features/blocks/premade/CodeBlock/converter.spec.ts.
  3. 11 of 20 cases fail. Examples: expected 'undefined$ ls' to be '$ ls', expected 'undefined' to be '', and for c++ the code is '++\nint x;' instead of 'int x;'. The controls pass (a multi-line fence with a language, ```foo```, a bare multi-line fence).
Which area(s) are affected?

plugin: richtext-lexical

Environment Info
@payloadcms/richtext-lexical 4.0.0-beta.9 (main at 56033d9561); same code in 3.93.0 / 3.x
Node v22.22.3 (where the spec was run)
Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.