From e7c6d5b7cdc0693d69fa844d472206aea75b06ca Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Dec 2024 10:09:23 +0000 Subject: [PATCH] fix: textBetween line breaks --- shared/editor/lib/textBetween.ts | 36 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/shared/editor/lib/textBetween.ts b/shared/editor/lib/textBetween.ts index 4a3fda3011..89ba7db890 100644 --- a/shared/editor/lib/textBetween.ts +++ b/shared/editor/lib/textBetween.ts @@ -16,27 +16,37 @@ export default function textBetween( to: number, plainTextSerializers: Record ): string { - const blockSeparator = "\n\n"; let text = ""; - let separated = true; + let first = true; + const blockSeparator = "\n"; doc.nodesBetween(from, to, (node, pos) => { const toPlainText = plainTextSerializers[node.type.name]; + let nodeText = ""; if (toPlainText) { - if (node.isBlock && !separated) { - text += blockSeparator; - separated = true; - } - - text += toPlainText(node); + nodeText += toPlainText(node); } else if (node.isText) { - text += node.text?.slice(Math.max(from, pos) - pos, to - pos); - separated = false; - } else if (node.isBlock && !separated) { - text += blockSeparator; - separated = true; + nodeText += node.textBetween( + Math.max(from, pos) - pos, + to - pos, + blockSeparator + ); } + + if ( + node.isBlock && + ((node.isLeaf && nodeText) || node.isTextblock) && + blockSeparator + ) { + if (first) { + first = false; + } else { + text += blockSeparator; + } + } + + text += nodeText; }); return text;