Skip to content

Commit ee3c2cc

Browse files
authored
fix(richtext-lexical): empty lines were incorrectly stripped from mdx blocks if doNotTrimChildren was set to true (#10287)
This caused empty lines to disappear from code blocks
1 parent b6de432 commit ee3c2cc

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

packages/richtext-lexical/src/features/blocks/server/markdownTransformer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ function getMarkdownTransformerForBlock(
403403

404404
let childrenString = ''
405405
if (block?.jsx?.doNotTrimChildren) {
406-
// Do not trim, but remove empty lines
407-
childrenString = linesInBetween.filter((line) => line.trim().length > 0).join('\n')
406+
childrenString = linesInBetween.join('\n')
408407
} else {
409408
childrenString = linesInBetween.join('\n').trim()
410409
}

test/lexical-mdx/int.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { restExamplesTest2 } from './tests/restExamples2.test.js'
1919

2020
import { defaultTests } from './tests/default.test.js'
2121
import { writeFileSync } from 'fs'
22+
import { codeTest1 } from './tests/code1.test.js'
2223

2324
let config: SanitizedConfig
2425
let editorConfig: SanitizedServerEditorConfig
@@ -61,7 +62,12 @@ describe('Lexical MDX', () => {
6162
editorConfig = (richTextField.editor as LexicalRichTextAdapter).editorConfig
6263
})
6364

64-
const INPUT_AND_OUTPUTBase: Tests = [...defaultTests, restExamplesTest1, restExamplesTest2]
65+
const INPUT_AND_OUTPUTBase: Tests = [
66+
...defaultTests,
67+
restExamplesTest1,
68+
restExamplesTest2,
69+
codeTest1,
70+
]
6571

6672
const INPUT_AND_OUTPUT: Tests = INPUT_AND_OUTPUTBase.find((test) => test.debugFlag)
6773
? [INPUT_AND_OUTPUTBase.find((test) => test.debugFlag)]

test/lexical-mdx/mdx/jsxBlocks/code/converter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ export const codeConverter: BlockJSX = {
1010
import: ({ openMatch, children, closeMatch }) => {
1111
const language = openMatch[1]
1212

13+
// Removed first and last \n from children if present
14+
if (children.startsWith('\n')) {
15+
children = children.slice(1)
16+
}
17+
if (children.endsWith('\n')) {
18+
children = children.slice(0, -1)
19+
}
20+
1321
const isSingleLineAndComplete =
1422
!!closeMatch && !children.includes('\n') && openMatch.input?.trim() !== '```' + language
1523

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
```ts
2+
import { buildConfig } from 'payload'
3+
import { lexicalEditor } from '@payloadcms/richtext-lexical'
4+
5+
export default buildConfig({
6+
collections: [
7+
// your collections here
8+
],
9+
// Pass the Lexical editor to the root config
10+
editor: lexicalEditor({}),
11+
})
12+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"editorState": {
3+
"root": {
4+
"children": [
5+
{
6+
"format": "",
7+
"type": "block",
8+
"version": 2,
9+
"fields": {
10+
"blockType": "Code",
11+
"language": "ts",
12+
"code": "import { buildConfig } from 'payload'\nimport { lexicalEditor } from '@payloadcms/richtext-lexical'\n\nexport default buildConfig({\n collections: [\n // your collections here\n ],\n // Pass the Lexical editor to the root config\n editor: lexicalEditor({}),\n})",
13+
"id": "6774559ba09e004452672523"
14+
}
15+
}
16+
],
17+
"direction": null,
18+
"format": "",
19+
"indent": 0,
20+
"type": "root",
21+
"version": 1
22+
}
23+
},
24+
"frontMatter": []
25+
}

test/lexical-mdx/tests/code1.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { readFileSync } from 'fs'
2+
import path from 'path'
3+
import { fileURLToPath } from 'url'
4+
5+
import type { Test } from '../int.spec.js'
6+
7+
const filename = fileURLToPath(import.meta.url)
8+
const dirname = path.dirname(filename)
9+
10+
export const codeTest1: Test = {
11+
input: readFileSync(path.resolve(dirname, 'code1.input.mdx'), 'utf-8'),
12+
rootChildren: JSON.parse(readFileSync(path.resolve(dirname, 'code1.output.json'), 'utf-8'))
13+
.editorState.root.children,
14+
}

0 commit comments

Comments
 (0)