Skip to content

Commit 379e5a3

Browse files
committed
update SQL link generation and refactor deltaToMdast function for improved text processing in rich text editor
1 parent 482c8e6 commit 379e5a3

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

examples/rich-text-editor/create_blog_post.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ insert into blog_posts (title, content)
22
values (:title, :content)
33
returning
44
'redirect' as component,
5-
'/' as link;
5+
'post?id=' || id as link;

examples/rich-text-editor/rich_text_editor.js

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ function initializeQuillEditor(editorDiv, toolbarOptions, initialValue) {
116116
toolbar: toolbarOptions,
117117
},
118118
formats: [
119-
'bold', 'italic', 'link',
120-
'header', 'list', 'blockquote',
121-
'code', 'code-block', 'image'
119+
"bold",
120+
"italic",
121+
"link",
122+
"header",
123+
"list",
124+
"blockquote",
125+
"code",
126+
"code-block",
127+
"image",
122128
],
123129
});
124130
if (initialValue) {
@@ -261,52 +267,49 @@ function deltaToMdast(delta) {
261267
let textBuffer = "";
262268

263269
for (const op of delta.ops) {
264-
if (op.delete || op.retain) {
265-
continue;
270+
if (isImageInsert(op)) {
271+
if (!currentParagraph) {
272+
currentParagraph = createParagraphNode();
273+
}
274+
currentParagraph.children.push(createImageNode(op));
266275
}
267-
268-
if (typeof op.insert === "string") {
269-
const text = op.insert;
270-
const attributes = op.attributes || {};
271-
272-
if (text === "\n") {
273-
processLineBreak(
274-
mdast,
275-
currentParagraph,
276-
attributes,
277-
textBuffer,
278-
currentList,
279-
);
280-
if (
281-
!attributes.list &&
282-
!attributes.blockquote &&
283-
!attributes["code-block"] &&
284-
!attributes.header
285-
) {
286-
currentList = null;
287-
}
288-
289-
// Reset paragraph and buffer after processing line break
290-
currentParagraph = null;
291-
textBuffer = "";
292-
continue;
276+
if (typeof op.insert !== "string") continue;
277+
278+
const text = op.insert;
279+
const attributes = op.attributes || {};
280+
281+
if (text === "\n") {
282+
processLineBreak(
283+
mdast,
284+
currentParagraph,
285+
attributes,
286+
textBuffer,
287+
currentList,
288+
);
289+
if (
290+
!attributes.list &&
291+
!attributes.blockquote &&
292+
!attributes["code-block"] &&
293+
!attributes.header
294+
) {
295+
currentList = null;
293296
}
294297

295-
// Process regular text
296-
const node = createTextNode(text, attributes);
298+
// Reset paragraph and buffer after processing line break
299+
currentParagraph = null;
300+
textBuffer = "";
301+
continue;
302+
}
297303

298-
if (!currentParagraph) {
299-
currentParagraph = createParagraphNode();
300-
}
304+
// Process regular text
305+
const node = createTextNode(text, attributes);
301306

302-
textBuffer += text;
303-
currentParagraph.children.push(node);
304-
} else if (isImageInsert(op)) {
305-
if (!currentParagraph) {
306-
currentParagraph = createParagraphNode();
307-
}
308-
currentParagraph.children.push(createImageNode(op));
307+
if (!currentParagraph) {
308+
currentParagraph = createParagraphNode();
309309
}
310+
311+
textBuffer += text;
312+
currentParagraph.children.push(node);
310313
}
311314

312315
if (currentParagraph) {
@@ -537,12 +540,19 @@ function processHeaderLineBreak(mdast, textBuffer, attributes) {
537540
* @returns {void}
538541
*/
539542
function processCodeBlockLineBreak(mdast, textBuffer, attributes) {
540-
mdast.children.push({
541-
type: "code",
542-
value: textBuffer,
543-
lang:
544-
attributes["code-block"] === "plain" ? null : attributes["code-block"],
545-
});
543+
const lang =
544+
attributes["code-block"] === "plain" ? null : attributes["code-block"];
545+
// Two code blocks in a row are merged into one
546+
const lastChild = mdast.children[mdast.children.length - 1];
547+
if (lastChild && lastChild.type === "code" && lastChild.lang === lang) {
548+
lastChild.value += `\n${textBuffer}`;
549+
} else {
550+
mdast.children.push({
551+
type: "code",
552+
value: textBuffer,
553+
lang,
554+
});
555+
}
546556
}
547557

548558
/**

0 commit comments

Comments
 (0)