Reference
chunkDocument
Chunks one document and returns its chunks, stats, and warnings.
Chunks one document.
function chunkDocument(source: DocumentSource, options?: ChunkOptions): Promise<ChunkResult>;Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
source |
DocumentSource |
The document: { path }, { bytes, filename?, format? }, or { content, format }. |
options |
ChunkOptions |
Optional. The strategy, its options, and the common options. Without options, the structure strategy is used with its defaults. |
Returns
Section titled “Returns”A promise of a ChunkResult:
| Field | Type | Description |
|---|---|---|
chunks |
Chunk[] |
The chunks, in document order. |
document |
DocumentInfo |
The document’s id, format, path, and Markdown. |
stats |
ChunkStats |
Chunk count, sizes, and time taken. |
warnings |
ChunkWarning[] |
Problems that didn’t stop chunking. See Warnings. |
Throws
Section titled “Throws”A DocchunkError with one of these codes:
| Code | When |
|---|---|
INVALID_OPTIONS |
An option or the source is invalid. |
UNSUPPORTED_FORMAT |
The format isn’t supported, or can’t be detected. |
FILE_NOT_FOUND |
The file at path can’t be read. |
CONVERSION_FAILED |
The file couldn’t be converted to Markdown. |
ABORTED |
options.signal was aborted. |
See Error codes for every case.
Example
Section titled “Example”import { chunkDocument } from "docchunk";
const { chunks, warnings } = await chunkDocument( { path: "handbook.pdf" }, { strategy: "structure", size: 1000 },);See also
Section titled “See also”- Chunk a document: a guide to sources, formats, the result, and errors.
- Strategies: the strategies and their options.