npm install @ferrow/text-chunkerBoundary-respecting text chunking for RAG pipelines — sentence, paragraph, or markdown-heading aware, configurable overlap, per-chunk metadata, zero runtime dependencies, strict TypeScript.
import { chunkText } from "text-chunker";
const doc = `# Getting Started
This library helps you chunk documents for RAG pipelines.
## Usage
Longer usage content goes here...`;
const chunks = chunkText(doc, {
boundary: "markdown",
targetSize: 300,
sizeUnit: "chars",
overlap: 30,
});
for (const c of chunks) {
console.log(c.index, c.headingPath, c.text.length, c.hardSplit);
}text: stringoptions.boundary?: "sentence" | "paragraph" | "markdown"— default"paragraph".options.targetSize?: number— default500.options.sizeUnit?: "chars" | "tokens"— default"chars".options.overlap?: number— overlap between consecutive chunks, insizeUnit. Default0.- Returns
Chunk[].
Each Chunk is:
interface Chunk {
text: string;
index: number;
start: number; // char offset in the source text
end: number; // char offset, exclusive
headingPath: string[]; // markdown heading stack; [] for non-markdown input
hardSplit: boolean; // true if a unit exceeded targetSize and was force-split
}Tiny built-in chars / 4 estimator used only when sizeUnit: "tokens". For
a fuller heuristic (CJK, code density, per-model calibration), see the
sibling token-estimator package.
- Chunks never split mid-sentence/paragraph/section unless a single unit
alone exceeds
targetSize— in that case it is hard-split and flaggedhardSplit: true. Set a generoustargetSizeif you never want hard splits. markdownboundary mode splits on heading lines only, not sub-paragraphs within a section — a whole section under a heading is one unit until it exceedstargetSize.- The token estimator is a rough
chars/4heuristic, not a real tokenizer. - Sentence splitting is regex-based (
./!/?boundaries) and does not handle abbreviations, decimal numbers, or non-Latin punctuation specially. - Overlap is measured in the same unit as
targetSizeand is built from whole boundary units carried from the tail of the previous chunk — it will not exceedoverlapbut may be smaller than requested if the last unit alone is larger thanoverlap.
Part of the ferrow-toolkit collection · Sponsored by Ferrow