Implementation:0xranx OpenContext Chunker
| Knowledge Sources | |
|---|---|
| Domains | Text_Processing, Indexing |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for splitting markdown documents into semantically coherent chunks with heading-aware boundaries, implemented in Rust as part of the OpenContext core search pipeline.
Description
The Chunker struct is a Rust implementation that uses the `pulldown-cmark` Markdown parser to walk the document's event stream and produce chunks that respect heading boundaries. Each chunk carries its heading path (e.g., "Architecture > Core > Search") for context.
Key characteristics:
- Unicode-safe sizing: Chunk size is measured in characters (using `.chars().count()`), not bytes, ensuring CJK and emoji content is handled correctly.
- Heading hierarchy tracking: A stack of `(HeadingLevel, String)` tracks the current heading path. When a new heading at level N is encountered, headings at level >= N are popped before pushing.
- Boundary splitting: When text exceeds `max_chunk_chars`, it is split at a natural boundary (paragraph end, sentence end, or word boundary) with overlap.
- Post-processing: Small chunks are merged with neighbors to reduce index fragmentation.
The chunker is called by the `Indexer` during the index build pipeline.
Usage
- Index build: Used internally by `Indexer::build_all_with_progress` to chunk documents before embedding.
- Configuration: Chunk size and overlap are configurable via `SearchConfig.search.chunk_size` and `chunk_overlap` (defaults: 1500 chars, 200 overlap).
Code Reference
Source Location
- Repository: OpenContext
- File: crates/opencontext-core/src/search/chunker.rs
Signature
pub struct Chunker {
max_chunk_chars: usize,
overlap_chars: usize,
}
impl Chunker {
/// Create a new chunker with custom settings (in characters)
pub fn new(max_chunk_chars: usize, overlap_chars: usize) -> Self;
/// Chunk a markdown document into semantic pieces
pub fn chunk(&self, content: &str, _file_path: &str) -> Vec<TextChunk>;
}
impl Default for Chunker {
fn default() -> Self {
Self {
max_chunk_chars: 1500,
overlap_chars: 200,
}
}
}
Import
use opencontext_core::search::chunker::Chunker;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| content | &str | Yes | Markdown document content |
| file_path | &str | Yes | File path (used for logging, not parsing) |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | Vec<TextChunk> | Array of text chunks |
| TextChunk.content | String | Chunk text content |
| TextChunk.heading_path | String | Heading hierarchy (e.g., "Overview > Architecture") |
| TextChunk.start_line | usize | Start line number in source |
| TextChunk.end_line | usize | End line number in source |
Usage Examples
Basic Chunking
use opencontext_core::search::chunker::Chunker;
let chunker = Chunker::default();
let content = r#"# Overview
This is the overview section.
## Architecture
The architecture consists of multiple modules."#;
let chunks = chunker.chunk(content, "docs/architecture.md");
for chunk in &chunks {
println!("[{}-{}] {} > {}",
chunk.start_line, chunk.end_line,
chunk.heading_path,
chunk.content.chars().take(50).collect::<String>()
);
}
Custom Chunk Size
use opencontext_core::search::chunker::Chunker;
// Smaller chunks for finer-grained search
let chunker = Chunker::new(800, 100);
let chunks = chunker.chunk(content, "docs/architecture.md");