payload icon indicating copy to clipboard operation
payload copied to clipboard

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

Open anatolykoptev opened this issue 2 weeks ago • 0 comments

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)

anatolykoptev avatar Nov 16 '26 05:11 anatolykoptev