mirror of
https://github.com/outline/outline.git
synced 2026-05-02 23:49:51 -05:00
bf45e97641
* Update types * fix circular dep * type imports * lint type imports and --fix
50 lines
1015 B
TypeScript
50 lines
1015 B
TypeScript
import type { Node as ProseMirrorNode } from "prosemirror-model";
|
|
|
|
/**
|
|
* Returns the text content between two positions.
|
|
*
|
|
* @param doc The Prosemirror document to use
|
|
* @param from A start point
|
|
* @param to An end point
|
|
* @returns A string of plain text
|
|
*/
|
|
export default function textBetween(
|
|
doc: ProseMirrorNode,
|
|
from: number,
|
|
to: number
|
|
): string {
|
|
let text = "";
|
|
let first = true;
|
|
const blockSeparator = "\n";
|
|
|
|
doc.nodesBetween(from, to, (node, pos) => {
|
|
let nodeText = "";
|
|
|
|
if (node.type.spec.leafText) {
|
|
nodeText += node.type.spec.leafText(node);
|
|
} else if (node.isText) {
|
|
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;
|
|
}
|