fix: textBetween line breaks

This commit is contained in:
Tom Moor committed 2024-12-26 10:09:23 +00:00
1 parent 3437bd3a6c
commit e7c6d5b7cd
1 file changed
+23 -13
+23 -13
View File
@@ -16,27 +16,37 @@ export default function textBetween(
to: number,
plainTextSerializers: Record<string, PlainTextSerializer | undefined>
): 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;