@@ -32,40 +32,17 @@ import { toMarkdown as mdastUtilToMarkdown } from "https://esm.sh/mdast-util-to-
32
32
* @returns {string } - Markdown representation.
33
33
*/
34
34
function deltaToMarkdown ( delta ) {
35
- try {
36
- const mdastTree = deltaToMdast ( delta ) ;
37
- const options = {
38
- bullet : "*" ,
39
- listItemIndent : "one" ,
40
- handlers : { } ,
41
- unknownHandler : ( node ) => {
42
- console . warn ( `Unknown node type encountered: ${ node . type } ` , node ) ;
43
- return false ;
44
- } ,
45
- } ;
46
- return mdastUtilToMarkdown ( mdastTree , options ) ;
47
- } catch ( error ) {
48
- console . error ( "Error during Delta to Markdown conversion:" , error ) ;
49
- console . warn ( "Falling back to basic text extraction" ) ;
50
- return extractPlainTextFromDelta ( delta ) ;
51
- }
52
- }
53
-
54
- /**
55
- * Extracts plain text from a Quill Delta object.
56
- * @param {QuillDelta } delta - Quill Delta object.
57
- * @returns {string } - Plain text extracted from the delta.
58
- */
59
- function extractPlainTextFromDelta ( delta ) {
60
- try {
61
- return delta . ops
62
- . map ( ( op ) => ( typeof op . insert === "string" ? op . insert : "" ) )
63
- . join ( "" )
64
- . trim ( ) ;
65
- } catch ( e ) {
66
- console . error ( "Fallback extraction also failed:" , e ) ;
67
- return "" ;
68
- }
35
+ const mdastTree = deltaToMdast ( delta ) ;
36
+ const options = {
37
+ bullet : "*" ,
38
+ listItemIndent : "one" ,
39
+ handlers : { } ,
40
+ unknownHandler : ( node ) => {
41
+ console . warn ( `Unknown node type encountered: ${ node . type } ` , node ) ;
42
+ return false ;
43
+ } ,
44
+ } ;
45
+ return mdastUtilToMarkdown ( mdastTree , options ) ;
69
46
}
70
47
71
48
/**
@@ -302,14 +279,14 @@ function deltaToMdast(delta) {
302
279
}
303
280
304
281
// Process regular text
305
- const node = createTextNode ( text , attributes ) ;
282
+ const nodes = createTextNodes ( text , attributes ) ;
306
283
307
284
if ( ! currentParagraph ) {
308
285
currentParagraph = createParagraphNode ( ) ;
309
286
}
310
287
311
288
textBuffer += text ;
312
- currentParagraph . children . push ( node ) ;
289
+ currentParagraph . children . push ( ... nodes ) ;
313
290
}
314
291
315
292
if ( currentParagraph ) {
@@ -368,43 +345,42 @@ function createImageNode(op) {
368
345
* Creates a text MDAST node with optional formatting.
369
346
* @param {string } text - The text content.
370
347
* @param {Object } attributes - The formatting attributes.
371
- * @returns {MdastNode } - The formatted text node .
348
+ * @returns {MdastNode[] } - The formatted text nodes .
372
349
*/
373
- function createTextNode ( text , attributes ) {
374
- let node = {
375
- type : "text" ,
376
- value : text ,
377
- } ;
350
+ function createTextNodes ( text , attributes ) {
351
+ let nodes = text . split ( "\n" ) . flatMap ( ( value , i ) => [
352
+ ...( i > 0 ? [ { type : "break" } ] : [ ] ) ,
353
+ {
354
+ type : "text" ,
355
+ value,
356
+ } ,
357
+ ] ) ;
378
358
379
359
if ( attributes . bold ) {
380
- node = wrapNodeWith ( node , "strong" ) ;
360
+ nodes = [ wrapNodesWith ( nodes , "strong" ) ] ;
381
361
}
382
362
383
363
if ( attributes . italic ) {
384
- node = wrapNodeWith ( node , "emphasis" ) ;
364
+ nodes = [ wrapNodesWith ( nodes , "emphasis" ) ] ;
385
365
}
386
366
387
367
if ( attributes . link ) {
388
- node = {
389
- type : "link" ,
390
- url : attributes . link ,
391
- children : [ node ] ,
392
- } ;
368
+ nodes = [ { ...wrapNodesWith ( nodes , "link" ) , url : attributes . link } ] ;
393
369
}
394
370
395
- return node ;
371
+ return nodes ;
396
372
}
397
373
398
374
/**
399
375
* Wraps a node with a formatting container.
400
- * @param {MdastNode } node - The node to wrap.
376
+ * @param {MdastNode[] } children - The node to wrap.
401
377
* @param {string } type - The type of container.
402
378
* @returns {MdastNode } - The wrapped node.
403
379
*/
404
- function wrapNodeWith ( node , type ) {
380
+ function wrapNodesWith ( children , type ) {
405
381
return {
406
382
type : type ,
407
- children : [ node ] ,
383
+ children,
408
384
} ;
409
385
}
410
386
0 commit comments