Core Concepts โ
Understanding how Incremark works helps you use and debug it better.
Incremental Parsing Flow โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Input Stream โ
โ "# Title" โ "\n\nCon" โ "tent\n" โ "\n## Sub" โ "title" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Buffer โ
โ "# Title\n\nContent\n\n## Subtitle" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Boundary Detection โ
โ Line-by-line scanning to identify block boundaries: โ
โ - Empty lines separate paragraphs โ
โ - Heading lines form independent blocks โ
โ - Code fences ``` must be paired โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
โผ โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Completed Blocks โ โ Pending Blocks โ
โ โข Never re-parsed โ โ โข Re-parsed on append โ
โ โข Nodes can be reused โ โ โข May be incomplete โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโBlock States โ
Each parsed block has three possible states:
| State | Description | Handling |
|---|---|---|
pending | Being received, may be incomplete | Re-parsed on each append |
stable | Possibly complete, but subsequent chunks may change it | Cached but not confirmed |
completed | Confirmed complete, won't change | Permanently cached, no further processing |
Boundary Detection Rules โ
Incremark uses heuristic rules to detect block boundaries:
Simple Blocks โ
- Empty lines - Separate paragraphs
- Headings (
#) - Form independent blocks - Thematic breaks (
---) - Form independent blocks
Blocks Requiring Closure โ
- Code blocks (
```) - Must wait for closing fence - Containers (
:::) - Must wait for closing marker
Nested Blocks โ
- Lists - Track indentation level
- Blockquotes (
>) - Track quote depth - Tables - Detect separator line
Context Tracking โ
To correctly handle nested structures, the parser maintains context state:
ts
interface BlockContext {
inFencedCode: boolean // Inside code block
fenceChar?: string // Code fence character
fenceLength?: number // Fence length
listDepth: number // List nesting depth
blockquoteDepth: number // Quote nesting depth
inContainer: boolean // Inside container
containerDepth: number // Container nesting depth
}AST Structure โ
Incremark generates standard MDAST format:
ts
interface Root {
type: 'root'
children: RootContent[]
}
// Block-level nodes
type RootContent =
| Heading
| Paragraph
| Code
| List
| Blockquote
| Table
| ThematicBreak
| ...Performance Optimization โ
Why Fast? โ
- Skip completed blocks - O(1) instead of O(n)
- Incremental line updates - Only process new lines
- Prefix sum optimization - O(1) line offset calculation
Complexity Comparison โ
| Operation | Traditional | Incremark |
|---|---|---|
| Append chunk | O(n) | O(k) |
| Total parsing | O(nยฒ) | O(n) |
| Memory | Repeated creation | Incremental reuse |
n = total chars, k = new chars
Next Steps โ
- Vue Integration - Vue deep dive
- Custom Components - Custom rendering