Skip to content

Commit d0b60e1

Browse files
committed
enhance deltaToMdast function to support code blocks, blockquotes, and lists in rich text editor
1 parent 772970d commit d0b60e1

File tree

1 file changed

+89
-7
lines changed

1 file changed

+89
-7
lines changed

examples/rich-text-editor/rich_text_editor.js

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,28 @@ function deltaToMarkdown(delta) {
1414
const options = {
1515
bullet: "*",
1616
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+
}
1725
};
1826
const markdown = mdastUtilToMarkdown(mdastTree, options); // Convert MDAST to Markdown
1927
console.log("markdown", markdown);
2028
return markdown;
2129
} catch (error) {
2230
console.error("Error during Delta to Markdown conversion:", error);
31+
console.warn("Falling back to basic text extraction");
2332
try {
2433
return delta.ops
2534
.map((op) => (typeof op.insert === "string" ? op.insert : ""))
2635
.join("")
2736
.trim();
2837
} catch (e) {
38+
console.error("Fallback extraction also failed:", e);
2939
return "";
3040
}
3141
}
@@ -173,14 +183,11 @@ function deltaToMdast(delta) {
173183
};
174184

175185
let currentParagraph = null;
186+
let currentList = null;
176187
let textBuffer = "";
177188

178189
for (const op of delta.ops) {
179-
if (op.delete) {
180-
continue;
181-
}
182-
183-
if (op.retain) {
190+
if (op.delete || op.retain) {
184191
continue;
185192
}
186193

@@ -220,15 +227,88 @@ function deltaToMdast(delta) {
220227
}
221228
currentParagraph = null;
222229
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 = "";
223267
} else {
224268
mdast.children.push(currentParagraph);
225269
currentParagraph = null;
270+
textBuffer = "";
226271
}
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;
227307
}
308+
228309
continue;
229310
}
230311

231-
textBuffer += text;
232312
let node = {
233313
type: "text",
234314
value: text,
@@ -263,6 +343,7 @@ function deltaToMdast(delta) {
263343
};
264344
}
265345

346+
textBuffer += text;
266347
currentParagraph.children.push(node);
267348
} else if (typeof op.insert === "object") {
268349
if (op.insert.image) {
@@ -281,7 +362,6 @@ function deltaToMdast(delta) {
281362
}
282363

283364
currentParagraph.children.push(imageNode);
284-
textBuffer = "";
285365
}
286366
}
287367
}
@@ -295,3 +375,5 @@ function deltaToMdast(delta) {
295375

296376
// --- Main Script Execution ---
297377
document.addEventListener("DOMContentLoaded", initializeEditors);
378+
379+
export { deltaToMdast };

0 commit comments

Comments
 (0)