@@ -2,6 +2,7 @@ import type { TDescendant, TElement, TText } from '@udecode/plate-common';
2
2
3
3
import type { MdastNode , RemarkElementRules } from './types' ;
4
4
5
+ import { MarkdownPlugin } from '../MarkdownPlugin' ;
5
6
import { remarkTransformElementChildren } from './remarkTransformElementChildren' ;
6
7
import { remarkTransformNode } from './remarkTransformNode' ;
7
8
@@ -79,7 +80,16 @@ export const remarkDefaultElementRules: RemarkElementRules = {
79
80
indent = 1
80
81
) => {
81
82
_node . children ?. forEach ( ( listItem ) => {
82
- const [ paragraph , ...subLists ] = listItem . children ! ;
83
+ if ( ! listItem . children ) {
84
+ listItems . push ( {
85
+ children : remarkTransformElementChildren ( listItem , options ) ,
86
+ type : options . editor . getType ( { key : 'p' } ) ,
87
+ } ) ;
88
+
89
+ return listItems ;
90
+ }
91
+
92
+ const [ paragraph , ...subLists ] = listItem . children ;
83
93
84
94
listItems . push ( {
85
95
children : remarkTransformElementChildren (
@@ -139,6 +149,9 @@ export const remarkDefaultElementRules: RemarkElementRules = {
139
149
} ,
140
150
paragraph : {
141
151
transform : ( node , options ) => {
152
+ const isKeepLineBreak =
153
+ options . editor . getOptions ( MarkdownPlugin ) . splitLineBreaks ;
154
+
142
155
const children = remarkTransformElementChildren ( node , options ) ;
143
156
144
157
const paragraphType = options . editor . getType ( { key : 'p' } ) ;
@@ -164,6 +177,42 @@ export const remarkDefaultElementRules: RemarkElementRules = {
164
177
if ( type && splitBlockTypes . has ( type as string ) ) {
165
178
flushInlineNodes ( ) ;
166
179
elements . push ( child as TElement ) ;
180
+ } else if (
181
+ isKeepLineBreak &&
182
+ 'text' in child &&
183
+ typeof child . text === 'string'
184
+ ) {
185
+ // Handle line break generated by <br>
186
+ const isSingleLineBreak =
187
+ child . text === '\n' && inlineNodes . length === 0 ;
188
+
189
+ if ( isSingleLineBreak ) {
190
+ inlineNodes . push ( { ...child , text : '' } ) ;
191
+ flushInlineNodes ( ) ;
192
+
193
+ return ;
194
+ }
195
+
196
+ // Handle text containing line breaks
197
+ const textParts = child . text . split ( '\n' ) ;
198
+
199
+ textParts . forEach ( ( part , index , array ) => {
200
+ const isNotFirstPart = index > 0 ;
201
+ const isNotLastPart = index < array . length - 1 ;
202
+
203
+ // Create new paragraph for non-first parts
204
+ if ( isNotFirstPart ) {
205
+ flushInlineNodes ( ) ;
206
+ }
207
+ // Only add non-empty text
208
+ if ( part ) {
209
+ inlineNodes . push ( { ...child , text : part } ) ;
210
+ }
211
+ // Create paragraph break for non-last parts
212
+ if ( isNotLastPart ) {
213
+ flushInlineNodes ( ) ;
214
+ }
215
+ } ) ;
167
216
} else {
168
217
inlineNodes . push ( child ) ;
169
218
}
0 commit comments