Skip to content

Commit c203af6

Browse files
committed
refactor deltaToMarkdown and createTextNode functions for improved text handling in rich text editor
1 parent 379e5a3 commit c203af6

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

examples/rich-text-editor/rich_text_editor.js

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,17 @@ import { toMarkdown as mdastUtilToMarkdown } from "https://esm.sh/mdast-util-to-
3232
* @returns {string} - Markdown representation.
3333
*/
3434
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);
6946
}
7047

7148
/**
@@ -302,14 +279,14 @@ function deltaToMdast(delta) {
302279
}
303280

304281
// Process regular text
305-
const node = createTextNode(text, attributes);
282+
const nodes = createTextNodes(text, attributes);
306283

307284
if (!currentParagraph) {
308285
currentParagraph = createParagraphNode();
309286
}
310287

311288
textBuffer += text;
312-
currentParagraph.children.push(node);
289+
currentParagraph.children.push(...nodes);
313290
}
314291

315292
if (currentParagraph) {
@@ -368,43 +345,42 @@ function createImageNode(op) {
368345
* Creates a text MDAST node with optional formatting.
369346
* @param {string} text - The text content.
370347
* @param {Object} attributes - The formatting attributes.
371-
* @returns {MdastNode} - The formatted text node.
348+
* @returns {MdastNode[]} - The formatted text nodes.
372349
*/
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+
]);
378358

379359
if (attributes.bold) {
380-
node = wrapNodeWith(node, "strong");
360+
nodes = [wrapNodesWith(nodes, "strong")];
381361
}
382362

383363
if (attributes.italic) {
384-
node = wrapNodeWith(node, "emphasis");
364+
nodes = [wrapNodesWith(nodes, "emphasis")];
385365
}
386366

387367
if (attributes.link) {
388-
node = {
389-
type: "link",
390-
url: attributes.link,
391-
children: [node],
392-
};
368+
nodes = [{ ...wrapNodesWith(nodes, "link"), url: attributes.link }];
393369
}
394370

395-
return node;
371+
return nodes;
396372
}
397373

398374
/**
399375
* Wraps a node with a formatting container.
400-
* @param {MdastNode} node - The node to wrap.
376+
* @param {MdastNode[]} children - The node to wrap.
401377
* @param {string} type - The type of container.
402378
* @returns {MdastNode} - The wrapped node.
403379
*/
404-
function wrapNodeWith(node, type) {
380+
function wrapNodesWith(children, type) {
405381
return {
406382
type: type,
407-
children: [node],
383+
children,
408384
};
409385
}
410386

0 commit comments

Comments
 (0)