@@ -14,18 +14,28 @@ function deltaToMarkdown(delta) {
14
14
const options = {
15
15
bullet : "*" ,
16
16
listItemIndent : "one" ,
17
+ handlers : {
18
+ // Custom handlers can be added here if needed
19
+ } ,
20
+ unknownHandler : ( node ) => {
21
+ console . warn ( `Unknown node type encountered: ${ node . type } ` , node ) ;
22
+ // Return false to fall back to the default handler
23
+ return false ;
24
+ }
17
25
} ;
18
26
const markdown = mdastUtilToMarkdown ( mdastTree , options ) ; // Convert MDAST to Markdown
19
27
console . log ( "markdown" , markdown ) ;
20
28
return markdown ;
21
29
} catch ( error ) {
22
30
console . error ( "Error during Delta to Markdown conversion:" , error ) ;
31
+ console . warn ( "Falling back to basic text extraction" ) ;
23
32
try {
24
33
return delta . ops
25
34
. map ( ( op ) => ( typeof op . insert === "string" ? op . insert : "" ) )
26
35
. join ( "" )
27
36
. trim ( ) ;
28
37
} catch ( e ) {
38
+ console . error ( "Fallback extraction also failed:" , e ) ;
29
39
return "" ;
30
40
}
31
41
}
@@ -173,14 +183,11 @@ function deltaToMdast(delta) {
173
183
} ;
174
184
175
185
let currentParagraph = null ;
186
+ let currentList = null ;
176
187
let textBuffer = "" ;
177
188
178
189
for ( const op of delta . ops ) {
179
- if ( op . delete ) {
180
- continue ;
181
- }
182
-
183
- if ( op . retain ) {
190
+ if ( op . delete || op . retain ) {
184
191
continue ;
185
192
}
186
193
@@ -220,15 +227,88 @@ function deltaToMdast(delta) {
220
227
}
221
228
currentParagraph = null ;
222
229
textBuffer = "" ;
230
+ } else if ( attributes [ "code-block" ] ) {
231
+ const codeBlock = {
232
+ type : "code" ,
233
+ value : textBuffer ,
234
+ lang : attributes [ "code-block" ] === "plain" ? null : attributes [ "code-block" ] ,
235
+ } ;
236
+ mdast . children . push ( codeBlock ) ;
237
+ currentParagraph = null ;
238
+ textBuffer = "" ;
239
+ } else if ( attributes . list ) {
240
+ if ( ! currentList || currentList . ordered !== ( attributes . list === "ordered" ) ) {
241
+ currentList = {
242
+ type : "list" ,
243
+ ordered : attributes . list === "ordered" ,
244
+ spread : false ,
245
+ children : [ ] ,
246
+ } ;
247
+ mdast . children . push ( currentList ) ;
248
+ }
249
+
250
+ const listItem = {
251
+ type : "listItem" ,
252
+ spread : false ,
253
+ children : [ currentParagraph ] ,
254
+ } ;
255
+
256
+ currentList . children . push ( listItem ) ;
257
+ currentParagraph = null ;
258
+ textBuffer = "" ;
259
+ } else if ( attributes . blockquote ) {
260
+ const blockquote = {
261
+ type : "blockquote" ,
262
+ children : [ currentParagraph ] ,
263
+ } ;
264
+ mdast . children . push ( blockquote ) ;
265
+ currentParagraph = null ;
266
+ textBuffer = "" ;
223
267
} else {
224
268
mdast . children . push ( currentParagraph ) ;
225
269
currentParagraph = null ;
270
+ textBuffer = "" ;
226
271
}
272
+ } else if ( attributes [ "code-block" ] ) {
273
+ const codeBlock = {
274
+ type : "code" ,
275
+ value : "" ,
276
+ lang : attributes [ "code-block" ] === "plain" ? null : attributes [ "code-block" ] ,
277
+ } ;
278
+ mdast . children . push ( codeBlock ) ;
279
+ } else if ( attributes . list ) {
280
+ if ( ! currentList || currentList . ordered !== ( attributes . list === "ordered" ) ) {
281
+ currentList = {
282
+ type : "list" ,
283
+ ordered : attributes . list === "ordered" ,
284
+ spread : false ,
285
+ children : [ ] ,
286
+ } ;
287
+ mdast . children . push ( currentList ) ;
288
+ }
289
+
290
+ const listItem = {
291
+ type : "listItem" ,
292
+ spread : false ,
293
+ children : [ { type : "paragraph" , children : [ ] } ] ,
294
+ } ;
295
+
296
+ currentList . children . push ( listItem ) ;
297
+ } else if ( attributes . blockquote ) {
298
+ const blockquote = {
299
+ type : "blockquote" ,
300
+ children : [ { type : "paragraph" , children : [ ] } ] ,
301
+ } ;
302
+ mdast . children . push ( blockquote ) ;
303
+ }
304
+
305
+ if ( ! attributes . list && ! attributes . blockquote && ! attributes [ "code-block" ] && ! attributes . header ) {
306
+ currentList = null ;
227
307
}
308
+
228
309
continue ;
229
310
}
230
311
231
- textBuffer += text ;
232
312
let node = {
233
313
type : "text" ,
234
314
value : text ,
@@ -263,6 +343,7 @@ function deltaToMdast(delta) {
263
343
} ;
264
344
}
265
345
346
+ textBuffer += text ;
266
347
currentParagraph . children . push ( node ) ;
267
348
} else if ( typeof op . insert === "object" ) {
268
349
if ( op . insert . image ) {
@@ -281,7 +362,6 @@ function deltaToMdast(delta) {
281
362
}
282
363
283
364
currentParagraph . children . push ( imageNode ) ;
284
- textBuffer = "" ;
285
365
}
286
366
}
287
367
}
@@ -295,3 +375,5 @@ function deltaToMdast(delta) {
295
375
296
376
// --- Main Script Execution ---
297
377
document . addEventListener ( "DOMContentLoaded" , initializeEditors ) ;
378
+
379
+ export { deltaToMdast } ;
0 commit comments