fix - Fix normalizeWhitespace removing indents in nested lists

This commit is contained in:
2026-04-06 13:29:20 +05:30
parent ec71200f0e
commit 2c0ccee5e1
3 changed files with 387 additions and 141 deletions
+2 -18
View File
@@ -111,7 +111,7 @@ export class HTMLTextSplitter extends TextSplitter {
...fields,
chunkOverlap: 0,
keepSeparator: false,
lengthFunction: (text: string) => this.normalizeWhitespace(text).length,
lengthFunction: (text: string) => text.trim().length,
});
this.separators = fields?.separators ?? [];
@@ -229,22 +229,6 @@ export class HTMLTextSplitter extends TextSplitter {
return node.type === "tag" && BLOCKISH_TAGS.has(node.name);
}
/**
* Collapses internal whitespace and trims leading/trailing whitespace.
* Preserves newlines between block elements while normalizing spaces.
*
* Needed so text extraction can preserve raw adjacency first and normalize
* only once after boundary-aware joining.
*/
private normalizeWhitespace(text: string): string {
return text
.replace(/\n[ \t]+/g, "\n") // Remove leading spaces after newlines
.replace(/[ \t]+\n/g, "\n") // Remove trailing spaces before newlines
.replace(/\n{3,}/g, "\n\n") // Collapse 3+ newlines to 2
.replace(/[ \t]{2,}/g, " ") // Collapse multiple spaces/tabs to one
.trim();
}
/**
* Joins multiple nodes into the normalized text the splitter actually emits.
*
@@ -273,7 +257,7 @@ export class HTMLTextSplitter extends TextSplitter {
previousNode = node;
}
return this.normalizeWhitespace(result);
return result.trim();
}
/**