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;