Compare commits

...

2 Commits

Author SHA1 Message Date
Tom Moor 3139f9faf4 test 2024-12-26 10:31:20 +00:00
Tom Moor e7c6d5b7cd fix: textBetween line breaks 2024-12-26 10:09:23 +00:00
2 changed files with 28 additions and 27 deletions
+5 -14
View File
@@ -248,39 +248,30 @@ This is a new paragraph.
// Strip all formatting
expect(text).toEqual(`This is a test paragraph
A new link
list item 1
This is a new paragraph.
This is a placeholder
this is a highlight
checklist item 1
checklist item 2
checklist item 3
checklist item 4
checklist item 5
This
Is
Table
Multiple
Lines
In a cell
`);
});
});
+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;